question: Custom error handler readme details
See original GitHub issueHi, thanks for this package! I’ve been trying to add a custom error handler following the readme guide:
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(401).send('invalid token...');
}
});
I noticed that I was only able to make it work after adding this middleware after my routes. Maybe this should be mentioned in the readme? Or perhaps I’m missing something here about the usage. Here’s how my main file looks like:
// imports
...
const app = express();
// middlewares
app.use(cors());
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// routes
app.use('/', something);
app.use('/else', else);
...
// where I add the custom handler
app.use((err, req, res) => {
if (err.name === 'UnauthorizedError') {
res.status(401).json({ message: 'Unauthorized. Invalid token!' });
}
});
app.listen(8000, () => {
console.log('Node middleware listening on port 8000!');
});
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:6
Top Results From Across the Web
Handle errors in ASP.NET Core | Microsoft Learn
To configure a custom error handling page for the Production environment, call UseExceptionHandler. This exception handling middleware:.
Read more >Custom error rendering in Slim 4 - Rob Allen's DevNotes
Separately, the error handler can log these details to the error log. Writing a custom HTML renderer. To write a new error renderer,...
Read more >Handling Error Conditions Using a Step Functions State ...
The context object returns the error message This is a custom error! . Choose Create function. After your Lambda function is created, copy...
Read more >Error Handling in Mule 4 - Mulesy.com
This tutorial explains the different ways to handler errors in Mule 4. ... You can override the error type with your custom error...
Read more >Introduction to Mule 4: Error Handlers | MuleSoft Documentation
A description of the problem. · A type that is used to characterize the problem. · A cause, the underlying Java Throwable that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Agreed it can’t be a full express tutorial, (and neither is the issue about it to be one) but just a line mentioning add this after your routes and other
app.use(...)
calls would be helpful.This is a Express core concept. The README of this library (
express-jwt
) cannot be a full Express tutorial:http://expressjs.com/en/guide/error-handling.html