Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error Messages in Node Azure Web App

Stumbled across an interesting issue with an Azure Web App running Node with IIS and wanted to share because I couldn't find information on it.

The problem:

My custom error messages weren't making it down to the client on my production app but were coming through just fine locally.

Quick example:

app.post('/user', function(req, res, next) {
    // Parse out the user data
    // ...
    // Oh no! An error!
    if (invalidData) {
        return res.status(400).json({error: 'Invalid username, must be at least 4 characters.'});
    }
    // ...
});

This was coming through my Web App as the standard "400: Bad request..." message rather than my custom error message. On the client I was trying to do JSON.parse(err.responseText).error which wasn't working as err.responseText was the "Bad request..." string rather than my JSON object.

like image 885
Everett Carney Avatar asked Jul 14 '15 20:07

Everett Carney


1 Answers

The solution!

In your web.config file add this line between <system.webServer> ... </system.webServer>:

<httpErrors existingResponse="PassThrough" />

Hopefully nobody else hits this issue like I did. I'm sure it was a mix of incorrect googling and inexperience with IIS but I never actually found the answer online, I stumbled across a few links that led me to the IIS error handling page.

Hope this helps someone else! There's a ton of good info in that doc if you're running an IIS web app, I highly recommend reading through it.

like image 50
Everett Carney Avatar answered Nov 07 '22 16:11

Everett Carney