Add a global error handler middleware
Create a middlewares folder containing a global errorHandler composant.
Add it in server.js setup file with app.use.
Remove the two useless functions that does a not working similar role in server.js (app.on('error' and .use(function (ctx, next)
This way all error handling can be improved:
- In handlers, replace
catchblocks contents withthrow errorto pass error through to errorHandler. - Remove all
try catchblocks inServicesfunctions.
It will:
- Improve error handling in the API responses
- Improve logging in console
- Improve code readability and reduce code duplication
See what has been done in gatekeeper repository to have a working exemple.