Skip to content

Commit

Permalink
feat: add errorz package to combine errors as enum in there
Browse files Browse the repository at this point in the history
  • Loading branch information
Nedim Akar authored and Nedim Akar committed Oct 17, 2023
1 parent dbe4bff commit e95521f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/jetstream/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package datastore

import (
"database/sql"
"errors"
"fmt"
"os"
"path"
"regexp"
"strings"
"time"

"github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz"

goosedbversion "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/goose-db-version"
"github.com/govau/cf-common/env"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -301,9 +304,9 @@ func WaitForMigrations(db *sql.DB) error {
databaseVersionRec, err := dbVersionRepo.GetCurrentVersion()
if err != nil {
var errorMsg = err.Error()
if strings.Contains(err.Error(), "no such table") {
if errors.Is(err, errorz.ErrNoSuchTable) {
errorMsg = "Waiting for versions table to be created"
} else if strings.Contains(err.Error(), "No database versions found") {
} else if errors.Is(err, errorz.ErrNoDatabaseVersionsFound) {
errorMsg = "Versions table is empty - waiting for migrations"
}
log.Infof("Database schema check: %s", errorMsg)
Expand Down
8 changes: 8 additions & 0 deletions src/jetstream/errorz/errorz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package errorz

import "errors"

var ErrDialTcp = errors.New("dial tcp")
var ErrNoSuchTable = errors.New("no such table")
var ErrNoDatabaseVersionsFound = errors.New("no database versions found")
var Err509Certificate = errors.New("x509: certificate")
4 changes: 3 additions & 1 deletion src/jetstream/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz"

"github.com/gorilla/context"
"github.com/govau/cf-common/env"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -39,7 +41,7 @@ const APIKeyAuthScheme = "Bearer"
func handleSessionError(config api.PortalConfig, c echo.Context, err error, doNotLog bool, msg string) error {
log.Debug("handleSessionError")

if strings.Contains(err.Error(), "dial tcp") {
if errors.Is(err, errorz.ErrDialTcp) {
return api.NewHTTPShadowError(
http.StatusServiceUnavailable,
"Service is currently unavailable",
Expand Down
5 changes: 3 additions & 2 deletions src/jetstream/plugins/kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"net/http"
"net/url"
"strconv"
"strings"

"errors"

"github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz"

"github.com/cloudfoundry-incubator/stratos/src/jetstream/api"
"github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -288,7 +289,7 @@ func (c *KubernetesSpecification) RequiresCert(ec echo.Context) error {
Message string
}
if err != nil {
if strings.Contains(err.Error(), "x509: certificate") {
if errors.Is(err, errorz.Err509Certificate) {
response.Status = http.StatusOK
response.Required = true
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/jetstream/setup_console.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"net/url"
"regexp"
"strconv"
"strings"

"github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz"

"github.com/govau/cf-common/env"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -93,7 +94,7 @@ func (p *portalProxy) setupGetAvailableScopes(c echo.Context) error {
errInfo, ok := err.(api.ErrHTTPRequest)
if ok {
if errInfo.Status == 0 {
if strings.Contains(errInfo.Error(), "x509: certificate") {
if errors.Is(errInfo, errorz.Err509Certificate) {
return api.NewHTTPShadowError(
http.StatusBadRequest,
"Could not connect to the UAA - Certificate error - check Skip SSL validation setting",
Expand Down

0 comments on commit e95521f

Please sign in to comment.