Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle any error Nodejs and don't return a stack trace

The question is simple, but I cannot find a solution that will work when tested against Passport.

Let's say I have a callback route like https://localhost/auth/google/callback

Right now I'm getting a stack trace if I throw garbage parameters here that then shows information like directory structures, etc, which is obviously not appropriate. I cannot blindly depend on every new route to handle errors appropriately.

Is there any way for me to disable a stack trace response given ANY error in NodeJS?

I tried the following:

app.use((err, req, res, next) => { 
    if (! err) {
        return next();
    }    

    res.status(500);
    res.send('500: Internal server error');
});

And this did not work for TokenError: Code was already redeemed. used in PassportJS.

Is this even possible? Obviously this would be for a production environment only.

like image 577
Alexander Kleinhans Avatar asked Aug 05 '26 19:08

Alexander Kleinhans


1 Answers

You need to set NODE_ENV to production to disable stacktrace, you can change it as an enviroment variable.

set NODE_ENV=production

Or if it's inconvenient do it in the program itself like this:

process.env.NODE_ENV = 'production';
like image 65
Kalana Demel Avatar answered Aug 08 '26 18:08

Kalana Demel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!