How can I create a route that will handle 403 errors from Express? I have default routes to catch 404/500 but it seems to stop before ever going to the router. Just stack dumps to screen.
To catch errors in express, use middleware that has four arguments:
app.use(handleErrors);
function handleErrors(err, req, res, next) {
res.send('This is your custom error page.');
}
To ensure the error is a 403 error, you can do something like:
app.use(handle403);
function handle403(err, req, res, next) {
if (err.status !== 403) return next();
res.send('403 error');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With