Errors
This boilerplate automates error handling and error responses.
First, let's talk about logging errors. When using proper logging mechanics and log levels, you can then leave all your logs in the code and have them printed depending on their severity. The package is shipped with the function helpers.Error()
, a wrapper that's intended to log an error and return it. These logs will only be visible if the LOG_LEVEL
env var permits. Avoid using fmt.Println()
at all times, instead, use logger.Debug()
or if you're within a handler, you can use the c.Logger().Debug()
helper.
Given that each handler can return an error, the router is configured to handle the error using pkg/api/handlers/errors/automatedHttpError.go
which will unwrap the error and match it with a list of registered errors under pkg/api/handlers/errors/errors.go
. Finally, it will construct an error response and respond to that request.
Validation errors are no different, except they're unwrapped further and sent to the user as individual form inputs so they can be displayed.
You're encouraged to register and maintain as many errors as you can in the same way. It's useful to have a specific error code mapped to each error, that way we can determine exactly what went wrong in each user flow.