diff --git a/internal/services/configstore/api/api.go b/internal/services/configstore/api/api.go index e8ff513..79f8a08 100644 --- a/internal/services/configstore/api/api.go +++ b/internal/services/configstore/api/api.go @@ -49,6 +49,10 @@ func ErrorResponseFromError(err error) *ErrorResponse { var cerr *util.ErrUnauthorized errors.As(err, &cerr) aerr = cerr + case errors.Is(err, &util.ErrInternal{}): + var cerr *util.ErrInternal + errors.As(err, &cerr) + aerr = cerr } if aerr != nil { @@ -83,6 +87,9 @@ func httpError(w http.ResponseWriter, err error) bool { case errors.Is(err, &util.ErrUnauthorized{}): w.WriteHeader(http.StatusUnauthorized) w.Write(resj) + case errors.Is(err, &util.ErrInternal{}): + w.WriteHeader(http.StatusInternalServerError) + w.Write(resj) default: w.WriteHeader(http.StatusInternalServerError) w.Write(resj) diff --git a/internal/services/gateway/api/api.go b/internal/services/gateway/api/api.go index a0d88b9..14cb219 100644 --- a/internal/services/gateway/api/api.go +++ b/internal/services/gateway/api/api.go @@ -50,6 +50,10 @@ func ErrorResponseFromError(err error) *ErrorResponse { var cerr *util.ErrUnauthorized errors.As(err, &cerr) aerr = cerr + case errors.Is(err, &util.ErrInternal{}): + var cerr *util.ErrInternal + errors.As(err, &cerr) + aerr = cerr } if aerr != nil { @@ -84,6 +88,9 @@ func httpError(w http.ResponseWriter, err error) bool { case errors.Is(err, &util.ErrUnauthorized{}): w.WriteHeader(http.StatusUnauthorized) w.Write(resj) + case errors.Is(err, &util.ErrInternal{}): + w.WriteHeader(http.StatusInternalServerError) + w.Write(resj) default: w.WriteHeader(http.StatusInternalServerError) w.Write(resj) diff --git a/internal/services/runservice/api/api.go b/internal/services/runservice/api/api.go index d39af4d..661d22e 100644 --- a/internal/services/runservice/api/api.go +++ b/internal/services/runservice/api/api.go @@ -66,6 +66,10 @@ func ErrorResponseFromError(err error) *ErrorResponse { var cerr *util.ErrUnauthorized errors.As(err, &cerr) aerr = cerr + case errors.Is(err, &util.ErrInternal{}): + var cerr *util.ErrInternal + errors.As(err, &cerr) + aerr = cerr } if aerr != nil { @@ -100,6 +104,9 @@ func httpError(w http.ResponseWriter, err error) bool { case errors.Is(err, &util.ErrUnauthorized{}): w.WriteHeader(http.StatusUnauthorized) w.Write(resj) + case errors.Is(err, &util.ErrInternal{}): + w.WriteHeader(http.StatusInternalServerError) + w.Write(resj) default: w.WriteHeader(http.StatusInternalServerError) w.Write(resj) diff --git a/internal/util/errors.go b/internal/util/errors.go index 5cdf7bc..cac2dc8 100644 --- a/internal/util/errors.go +++ b/internal/util/errors.go @@ -131,3 +131,21 @@ func (*ErrUnauthorized) Is(err error) bool { _, ok := err.(*ErrUnauthorized) return ok } + +type ErrInternal struct { + Err error +} + +// ErrInternal represent an internal error that should be returned to the user +func (e *ErrInternal) Error() string { + return e.Err.Error() +} + +func NewErrInternal(err error) *ErrInternal { + return &ErrInternal{Err: err} +} + +func (*ErrInternal) Is(err error) bool { + _, ok := err.(*ErrInternal) + return ok +}