Replies: 1 comment 1 reply
-
You could add middleware to func main() {
e := echo.New()
e.HTTPErrorHandler = func(err error, c echo.Context) {
if c.Response().Committed {
return
}
log.Printf("inside custom textual error andler, err: %v", err)
c.String(http.StatusInternalServerError, "error text")
}
apiErrorHandlerMiddleware := func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
err := next(c)
if err == nil {
return nil
}
// add your custom logic here
e.DefaultHTTPErrorHandler(err, c)
return nil // should return nil as we have "handled" the error here
}
}
g := e.Group("/api", apiErrorHandlerMiddleware)
g.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "OK")
})
if err := e.Start(":8080"); err != http.ErrServerClosed {
log.Fatal(err)
}
} NB: this does not consider panics (recover middleware) and when you are using (request)logger middleware what errors these will receive. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there,
You can set a custom error handler by setting
Echo.HTTPErrorHandler
.It would be nice to have different error handlers for groups of routes, as
Group.HTTPErrorHandler
My usecase is to use JSON for the/api
group and Text/HTML for other routes.Beta Was this translation helpful? Give feedback.
All reactions