question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question: Custom error handler readme details

See original GitHub issue

Hi, 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:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:6

github_iconTop GitHub Comments

7reactions
divyanshu013commented, Feb 26, 2018

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.

2reactions
ibccommented, Feb 26, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found