Skip to content

Commit

Permalink
Merge pull request #190 from retail-ai-inc/feature/improve-error-resp…
Browse files Browse the repository at this point in the history
…onse
  • Loading branch information
tanvir-retailai authored May 21, 2024
2 parents 6ea7a3f + 4a73a04 commit 6af994d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func NewEcho() *echo.Echo {
func (b *Bean) ServeAt(host, port string) {
b.Echo.Logger.Info("Starting " + b.Config.Environment + " " + b.Config.ProjectName + " at " + host + ":" + port + "...🚀")

b.UseErrorHandlerFuncs(berror.DefaultErrorHanderFunc)
b.UseErrorHandlerFuncs(berror.DefaultErrorHandlerFunc)
b.Echo.HTTPErrorHandler = b.DefaultHTTPErrorHandler()

b.Echo.Validator = &validator.DefaultValidator{Validator: b.validate}
Expand Down
13 changes: 7 additions & 6 deletions error/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,27 @@ import (
type errorResp struct {
ErrorCode ErrorCode `json:"errorCode"`
ErrorMsg interface{} `json:"errorMsg"`
Errors interface{} `json:"errors"`
}

type ErrorHandlerFunc func(err error, c echo.Context) (bool, error)

func ValidationErrorHanderFunc(e error, c echo.Context) (bool, error) {
func ValidationErrorHandlerFunc(e error, c echo.Context) (bool, error) {
he, ok := e.(*validator.ValidationError)
if !ok {
return false, nil
}
err := c.JSON(http.StatusBadRequest, errorResp{
ErrorCode: API_DATA_VALIDATION_FAILED,
ErrorMsg: he.ErrCollection(),
Errors: he.ErrCollection(),
})

return ok, err
}

// Default JSON API error handler. The response pattern is like below:
// `{"errorCode": "1000001", "errorMsg": "some message"}`. You can override this error handler from `start.go`
func APIErrorHanderFunc(e error, c echo.Context) (bool, error) {
func APIErrorHandlerFunc(e error, c echo.Context) (bool, error) {
he, ok := e.(*APIError)
if !ok {
return false, nil
Expand All @@ -82,7 +83,7 @@ func APIErrorHanderFunc(e error, c echo.Context) (bool, error) {
return ok, err
}

func HTTPErrorHanderFunc(e error, c echo.Context) (bool, error) {
func HTTPErrorHandlerFunc(e error, c echo.Context) (bool, error) {
he, ok := e.(*echo.HTTPError)
if !ok {
return false, nil
Expand Down Expand Up @@ -206,9 +207,9 @@ func HTTPErrorHanderFunc(e error, c echo.Context) (bool, error) {
return ok, err
}

// If any other error handler doesn't catch the error then finally `DefaultErrorHanderFunc` will
// If any other error handler doesn't catch the error then finally `DefaultErrorHandlerFunc` will
// cactch the error and treat all those errors as `http.StatusInternalServerError`.
func DefaultErrorHanderFunc(err error, c echo.Context) (bool, error) {
func DefaultErrorHandlerFunc(err error, c echo.Context) (bool, error) {
// Send error event to sentry if configured.
if viper.GetBool("sentry.on") {
if hub := sentryecho.GetHubFromContext(c); hub != nil {
Expand Down
16 changes: 8 additions & 8 deletions error/error_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func TestValidationErrorHanderFunc(t *testing.T) {
c.SetResponse(echo.NewResponse(dummyWriter{io.Discard}, e))

fakeErr := &fakeError{"fake"}
got, err := ValidationErrorHanderFunc(fakeErr, c)
got, err := ValidationErrorHandlerFunc(fakeErr, c)
assert.NoError(t, err)
assert.Equal(t, false, got)

validateErr := &validator.ValidationError{Err: validatorV10.ValidationErrors{}}
got, err = ValidationErrorHanderFunc(validateErr, c)
got, err = ValidationErrorHandlerFunc(validateErr, c)
assert.NoError(t, err)
assert.Equal(t, true, got)

Expand All @@ -75,12 +75,12 @@ func TestAPIErrorHanderFunc(t *testing.T) {
c.SetResponse(echo.NewResponse(dummyWriter{io.Discard}, e))

fakeErr := &fakeError{"fake"}
got, err := APIErrorHanderFunc(fakeErr, c)
got, err := APIErrorHandlerFunc(fakeErr, c)
assert.NoError(t, err)
assert.Equal(t, false, got)

apiErr := NewAPIError(http.StatusInternalServerError, INTERNAL_SERVER_ERROR, errors.New("internal"))
got, err = APIErrorHanderFunc(apiErr, c)
got, err = APIErrorHandlerFunc(apiErr, c)
assert.NoError(t, err)
assert.Equal(t, true, got)

Expand All @@ -103,12 +103,12 @@ func TestHTTPErrorHanderFunc(t *testing.T) {
c.SetResponse(echo.NewResponse(dummyWriter{io.Discard}, e))

fakeErr := &fakeError{"fake"}
got, err := HTTPErrorHanderFunc(fakeErr, c)
got, err := HTTPErrorHandlerFunc(fakeErr, c)
assert.NoError(t, err)
assert.Equal(t, false, got)

echoHTTPErr := echo.NewHTTPError(http.StatusInternalServerError, "internal")
got, err = HTTPErrorHanderFunc(echoHTTPErr, c)
got, err = HTTPErrorHandlerFunc(echoHTTPErr, c)
assert.NoError(t, err)
assert.Equal(t, true, got)

Expand All @@ -131,12 +131,12 @@ func TestDefaultErrorHanderFunc(t *testing.T) {
c.SetResponse(echo.NewResponse(dummyWriter{io.Discard}, e))

fakeErr := &fakeError{"fake"}
got, err := DefaultErrorHanderFunc(fakeErr, c)
got, err := DefaultErrorHandlerFunc(fakeErr, c)
assert.NoError(t, err)
assert.Equal(t, true, got)

anyErr := echo.NewHTTPError(http.StatusInternalServerError, "internal")
got, err = DefaultErrorHanderFunc(anyErr, c)
got, err = DefaultErrorHandlerFunc(anyErr, c)
assert.NoError(t, err)
assert.Equal(t, true, got)

Expand Down

0 comments on commit 6af994d

Please sign in to comment.