Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add errorz package to combine errors as enum in there #47

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 strings.Contains(err.Error(), errorz.ERR_NO_SUCH_TABLE) {
nedimakar marked this conversation as resolved.
Show resolved Hide resolved
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
15 changes: 15 additions & 0 deletions src/jetstream/errorz/errorz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package errorz

import "errors"

const (
ERR_DIAL_TCP = "dial"
nedimakar marked this conversation as resolved.
Show resolved Hide resolved
ERR_NO_DATABASE_VERSIONS_FOUND = "no database versions found"
ERR_NO_SUCH_TABLE = "no such table"
ERR_X509_CERTIFICATE = "x509: certificate"
nedimakar marked this conversation as resolved.
Show resolved Hide resolved
)

var ErrDialTcp = errors.New(ERR_DIAL_TCP)
var ErrNoDatabaseVersionsFound = errors.New(ERR_NO_DATABASE_VERSIONS_FOUND)
var ErrNoSuchTable = errors.New(ERR_NO_SUCH_TABLE)
var Err509Certificate = errors.New(ERR_X509_CERTIFICATE)
18 changes: 12 additions & 6 deletions src/jetstream/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"database/sql"
"errors"
"fmt"
"net"
"net/http"
"os"
"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,12 +42,15 @@ 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") {
return api.NewHTTPShadowError(
http.StatusServiceUnavailable,
"Service is currently unavailable",
"Service is currently unavailable: %v", err,
)
var netOpErr *net.OpError
if errors.As(err, &netOpErr) {
if netOpErr.Op == errorz.ERR_DIAL_TCP {
return api.NewHTTPShadowError(
http.StatusServiceUnavailable,
"Service is currently unavailable",
"Service is currently unavailable: %v", err,
)
}
}

if doNotLog {
Expand Down
3 changes: 2 additions & 1 deletion src/jetstream/plugins/kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"errors"

"github.com/cloudfoundry-incubator/stratos/src/jetstream/api"
"github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz"
"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 strings.Contains(err.Error(), errorz.ERR_X509_CERTIFICATE) {
response.Status = http.StatusOK
response.Required = true
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package goosedbversion

import (
"database/sql"
"errors"
"fmt"

log "github.com/sirupsen/logrus"

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

const (
Expand Down Expand Up @@ -37,7 +37,7 @@ func (p *PostgresGooseDBVersionRepository) GetCurrentVersion() (api.GooseDBVersi

switch {
case err == sql.ErrNoRows:
return api.GooseDBVersionRecord{}, errors.New("No database versions found")
return api.GooseDBVersionRecord{}, errorz.ErrNoDatabaseVersionsFound
case err != nil:
return api.GooseDBVersionRecord{}, fmt.Errorf("Error trying to get current database version: %v", err)
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
. "github.com/smartystreets/goconvey/convey"

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

func TestPgSQLGooseDB(t *testing.T) {
Expand Down Expand Up @@ -63,9 +64,6 @@ func TestPgSQLGooseDB(t *testing.T) {
})

Convey("if one doesn't exist", func() {

expectedErrorMessage := "No database versions found"

// Database setup
rs := sqlmock.NewRows(rowFieldsForVersionID)
mock.ExpectQuery(selectFromDBVersionWhere).
Expand All @@ -74,7 +72,7 @@ func TestPgSQLGooseDB(t *testing.T) {
Convey("there should be an error", func() {
repository, _ := NewPostgresGooseDBVersionRepository(db)
_, err := repository.GetCurrentVersion()
So(err, ShouldResemble, errors.New(expectedErrorMessage))
So(err, ShouldResemble, errorz.ErrNoDatabaseVersionsFound)

dberr := mock.ExpectationsWereMet()
So(dberr, ShouldBeNil)
Expand Down
3 changes: 2 additions & 1 deletion src/jetstream/setup_console.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/cloudfoundry-incubator/stratos/src/jetstream/api"
"github.com/cloudfoundry-incubator/stratos/src/jetstream/api/config"
"github.com/cloudfoundry-incubator/stratos/src/jetstream/crypto"
"github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz"
"github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/console_config"
"github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/localusers"
)
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 strings.Contains(errInfo.Error(), errorz.ERR_X509_CERTIFICATE) {
return api.NewHTTPShadowError(
http.StatusBadRequest,
"Could not connect to the UAA - Certificate error - check Skip SSL validation setting",
Expand Down