Error-handling middleware is different from the rest of the middleware because it takes four arguments (error
, request
, response
, next
) instead of three.
Another thing we should take into consideration is that the custom error-handling middleware should be placed after the rest of the middleware. If we don't do the placement, when we call next(err)
, Express will not find any custom error handler (because the middleware is loaded in order) and will default to the built-in error handler.
Now that we know this, let's create a custom error handler that we can use during development to show us the relevant stack trace information. It should exclude core native method calls and also the NPM dependencies (node_modules
). The handler should display the source code for each method call that caused the error.
We will use the stack-trace
module to parse the stack trace (get line number, file name, and so on) and the async-each
module to handle reading the files and...