Is it possible to centralize responses and call them from the handler function? #2319
-
I am new to Echo (coming over from Gin). Is it possible to pass the context to a function that is a child of the handler function and return your response from the child function? For example, with Gin, I had a common responses file where I could call
This was done to centralize error messaging and response structure. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Echo has global error handler that is best to be used for centralized error handling e.HTTPErrorHandler = func(err error, context echo.Context) {
// can call even default handler here e.DefaultHTTPErrorHandler(err, context)
} and for commond responses. You can create similar function to Gin func myMyResponse(c echo.Context, statusCode int, message string) error {
return c.JSON(statusCode, map[string]string{"message": message})
} and in handler call it (make sure to return - so upstream middlewares and global error handler can handle errors from that call) return myMyResponse(c, http.StatusInternalServerError, "ok") |
Beta Was this translation helpful? Give feedback.
Echo has global error handler that is best to be used for centralized error handling
and for commond responses. You can create similar function to Gin
and in handler call it (make sure to return - so upstream middlewares and global error handler can handle errors from that call)