The errorhandler
Express middleware can be used to handle errors during app development.
In this article, we’ll look at how to use it to debug our code during development.
Why do we Need This Middleware?
We need this for debugging purposes. It shows the full error stack trace and internals of any object that’s passed to this module will be sent back to the client if an error occurs.
It’ll display as many details as possible about errors. Object details will be fully displayed. The data can be displayed as HTML, JSON, and plain text.
When the object is a standard Error
object, the string provided by the stack
property will be returned in HTML or text responses.
When an object is a non-error object, the result of util.inspect
will be returned in HTML or text format.
Error involving JSON responses will be an object with enumerable properties from the object in the response.
Installing the Package
We can install it by running:
npm install errorhandler
Options
The errorhandler
middleware takes a few options to control what to return.
log
The log
property lets us provide a function to be called with the error and a string representation of the error.
It can be used to write the error to any desired location or set to false
to only send the error back in the response.
The function has the signature (err, str, req, res)
where err
is the Error
object, str
is a string representation of the error, req
is the request object and res
is the response object.
This function is run after the response has been written.
The default value for this option is true
unless process.env.NODE_ENV
has the value 'test'
.
Other possible values include true
, which logs errors using console.error(str)
, or false
, which only sends the error back in the response.
Examples
We can use it as follows:
const express = require('express');
const express = require('express');
const bodyParser = require('body-parser');
const errorHandler = require('errorhandler');
const app = express();
app.use(bodyParser.json());
app.get('/', (req, res, next) => {
throw new Error('error');
res.send();
})
app.use(errorHandler());
app.listen(3000);
Since our route throws an error, we should get objects with the error string and object logged.
We should get something like::
Connect
500 Error: error
at app.get (/home/runner/index.js:9:9)
at Layer.handle [as handle_request] (/home/runner/node_modules/express/lib/router/layer.js:95:5)
at next (/home/runner/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/runner/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/runner/node_modules/express/lib/router/layer.js:95:5)
at /home/runner/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/runner/node_modules/express/lib/router/index.js:335:12)
at next (/home/runner/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/home/runner/node_modules/body-parser/lib/types/json.js:110:7)
at Layer.handle [as handle_request] (/home/runner/node_modules/express/lib/router/layer.js:95:5)
Displayed on our screen.
We can add our own custom logging function as follows:
const express = require('express');
const bodyParser = require('body-parser');
const errorHandler = require('errorhandler');
const app = express();
const errorNotification = (err, str, req) => {
console.log(str);
}
app.use(bodyParser.json());
app.get('/', (req, res, next) => {
throw new Error('error');
res.send();
})
app.use(errorHandler({ log: errorNotification }));
app.listen(3000);
In the code above, we added our own errorNotification
function. All we did was logging the error string with our errorNotification
handler function.
Then we should get:
Error: error
at app.get (/home/runner/index.js:12:9)
at Layer.handle [as handle_request] (/home/runner/node_modules/express/lib/router/layer.js:95:5)
at next (/home/runner/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/runner/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/runner/node_modules/express/lib/router/layer.js:95:5)
at /home/runner/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/runner/node_modules/express/lib/router/index.js:335:12)
at next (/home/runner/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/home/runner/node_modules/body-parser/lib/types/json.js:110:7)
at Layer.handle [as handle_request] (/home/runner/node_modules/express/lib/router/layer.js:95:5)
From the console.log
output in addition to what we see on the page.
Conclusion
With the errorhandler
middleware, we can debug our Express apps more easily because we can see more information about the error like the stack trace and the object with more error information.
Also, we can see the request and response object when the error occurred.
The error data can be displayed in HTML, JSON or plain text.
We can add it by including the middleware with use
, but we have to be careful not run use this in production since it shows internal data about the error.