Error handling
Error handling is one of the things that you must do in your API. What happens if something fails, there is a panic, or a route is not found?
In Echo, the policy is to have a centralized point for handling errors: HTTPErrorHandler. This handler allows any error in your API to be handled in the same place. By default, this behavior is ideal for API developers. It just returns some JSON with a message field. Here’s an example:
e.GET("/user/:id", func(c echo.Context) error {
return echo.NewHTTPError(http.StatusNotFound, "User not found")
})
The generated error would be as follows:
{"message": "User not found"}
However, even though this is very convenient, it doesn’t necessarily work for everybody, so you can decide how you want to handle your errors. Maybe you want to have custom errors and use extra information in the errors you return, such as an error code. In those cases, you can build an...