Published on

Intercepting Expressjs Errors and Outputting a Stacktrace

Authors

Today I was debugging an express app where I kept getting an error which hit the codes error handler middleware:

//this is at the bottom of the expressjs use blocks to make sure it catches all errors
app.use(function(err, req,res,next){
...

}):

After trying to JSON.stringify(err) which gave me a circular reference error I tried using util.inspect(err) which worked but this gave me a huge payload which was hard to loom through. After Googling a bit more I found the solution:

//this is at the bottom of the expressjs use blocks to make sure it catches all errors
app.use(function(err, req,res,next){
...
console.log(`Error message: [${err.message}], stack trace: [${err.stack}]`);
}):