From e95521f05a802db374201441f68067bf99551cf4 Mon Sep 17 00:00:00 2001 From: Nedim Akar Date: Tue, 17 Oct 2023 14:55:22 +0200 Subject: [PATCH] feat: add errorz package to combine errors as enum in there --- src/jetstream/datastore/datastore.go | 7 +++++-- src/jetstream/errorz/errorz.go | 8 ++++++++ src/jetstream/middleware.go | 4 +++- src/jetstream/plugins/kubernetes/main.go | 5 +++-- src/jetstream/setup_console.go | 5 +++-- 5 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/jetstream/errorz/errorz.go diff --git a/src/jetstream/datastore/datastore.go b/src/jetstream/datastore/datastore.go index 37f0aaa620..1b4ee5b4b8 100644 --- a/src/jetstream/datastore/datastore.go +++ b/src/jetstream/datastore/datastore.go @@ -2,6 +2,7 @@ package datastore import ( "database/sql" + "errors" "fmt" "os" "path" @@ -9,6 +10,8 @@ import ( "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" @@ -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) diff --git a/src/jetstream/errorz/errorz.go b/src/jetstream/errorz/errorz.go new file mode 100644 index 0000000000..0dc53d2f79 --- /dev/null +++ b/src/jetstream/errorz/errorz.go @@ -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") diff --git a/src/jetstream/middleware.go b/src/jetstream/middleware.go index 41d2dd6ad0..0bfa7f9288 100644 --- a/src/jetstream/middleware.go +++ b/src/jetstream/middleware.go @@ -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" @@ -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", diff --git a/src/jetstream/plugins/kubernetes/main.go b/src/jetstream/plugins/kubernetes/main.go index 4026dd49dd..19616ef35b 100644 --- a/src/jetstream/plugins/kubernetes/main.go +++ b/src/jetstream/plugins/kubernetes/main.go @@ -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" @@ -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 { diff --git a/src/jetstream/setup_console.go b/src/jetstream/setup_console.go index 5416b0b805..120980dd37 100644 --- a/src/jetstream/setup_console.go +++ b/src/jetstream/setup_console.go @@ -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" @@ -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",