Member-only story
Intro
Gin framework provides Error Handling Middleware that enables you to configure error handling centrally within your Go application, as opposed to explicitly handling exceptions.
Centralized error handling middleware gives the following benefits:
- Centralized location for handling errors
- Reduce boilerplate ‘error to response’ mappings in your request handlers/controller actions
- Helps protect yourself from inadvertently revealing errors to API consumers
However, in actual application environment, you may have customized response or error message to users and need to be handle as your own will. In that case, gin provided error handling middleware may restrict the capacity of the application.
So, in this session, I want to bring an idea of how to handle error in middleware and provide an example of it.
Example
We will use this example to demonstrate how you can handle an error in Gin middleware. In the example:
- If an Error without http code, we will add Internal Server Error to the response and construct an custom error response.
- If an Error is custom error type, then we will hide the error message in…