Skip to content
Discussion options

You must be logged in to vote

The business error is lost at this line:

return render(c, templates.Index("some stuff"))

If rendering succeeds, that returns nil, so Echo has no error to pass to your centralized handler. Return the business error from the route and let the centralized handler log it and choose the HTML or JSON response.

A useful arrangement is to mark your HTML routes with a context value such as c.Set("responseFormat", "html"). In the error handler:

  1. Log the original error once.
  2. If c.Response().Committed is true, stop: the response can no longer be replaced.
  3. Map the error to your public message and HTTP status.
  4. For HTML routes, render an error component with that status; for API routes, call c.JSON.

Bu…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by pidanou
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