Skip to content

Is it possible to centralize responses and call them from the handler function? #2319

Answered by aldas
chrislentz asked this question in Q&A
Discussion options

You must be logged in to vote

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")

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by chrislentz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants