-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(errhandler):support custom http error handler
- Loading branch information
1 parent
3acd662
commit 0afcbaf
Showing
7 changed files
with
200 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/cutedogspark/echo-custom-context" | ||
"github.com/labstack/echo" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func HTTPErrorHandler(err error, c echo.Context) { | ||
if he, ok := err.(*ctx.GErrCall); ok { | ||
err = errors.WithStack(he) | ||
b, _ := json.Marshal(he.ResponseParams) | ||
c.JSONBlob(he.HttpStatus, b) | ||
} else if he, ok := err.(*echo.HTTPError); ok { | ||
// warp echo error struct | ||
err = errors.WithStack(he) | ||
gCtx := ctx.CustomCtx{} | ||
gErrs := gCtx.GResp().Errors(&ctx.GError{ | ||
Code: uint(he.Code), | ||
Message: fmt.Sprintf("%+v", he.Message), | ||
}) | ||
b, _ := json.Marshal(gErrs.ResponseParams) | ||
c.JSONBlob(he.Code, b) | ||
} else { | ||
// define unknown error message | ||
err = errors.New("unknown error") | ||
gCtx := ctx.CustomCtx{} | ||
gErrs := gCtx.GResp().Errors(&ctx.GError{ | ||
Code: http.StatusInternalServerError, | ||
Message: err.Error(), | ||
}) | ||
b, _ := json.Marshal(gErrs.ResponseParams) | ||
c.JSONBlob(he.Code, b) | ||
} | ||
c.Logger().Error(err) | ||
} | ||
|
||
func main() { | ||
|
||
e := echo.New() | ||
e.HideBanner = true | ||
e.HTTPErrorHandler = HTTPErrorHandler | ||
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
return next(ctx.CustomCtx{c}) | ||
} | ||
}) | ||
|
||
e.GET("/", func(c echo.Context) error { | ||
// no use error handler function | ||
return c.(ctx.CustomCtx).GResp(http.StatusOK).Data("Service").Out() | ||
}) | ||
|
||
e.GET("/gerr", func(c echo.Context) error { | ||
gerrs := ctx.NewGErrors().Append(&ctx.GError{ | ||
Code: 40000001, | ||
Domain: "Calendar", | ||
Reason: "ResourceNotFoundException", | ||
Message: "Resources is not exist", | ||
LocationType: "database query", | ||
Location: "query", | ||
ExtendedHelp: "http://help-link", | ||
SendReport: "http://report.dajui.com/", | ||
}).Append(&ctx.GError{ | ||
Code: 40000002, | ||
Domain: "global", | ||
Reason: "required", | ||
Message: "Required parameter: part", | ||
LocationType: "parameter", | ||
Location: "part", | ||
}) | ||
return c.(ctx.CustomCtx).GResp().Errors(*gerrs...).Do() | ||
}) | ||
|
||
e.GET("/echo-error", func(c echo.Context) error { | ||
return echo.NewHTTPError(http.StatusBadRequest, "default echo error handler") | ||
}) | ||
|
||
e.GET("/unknown-error", func(c echo.Context) error { | ||
return errors.New("Goodbye") | ||
}) | ||
|
||
// Start server | ||
e.Logger.Fatal(e.Start(":1234")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.