6

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.

Everett Carney
  • 260
  • 1
  • 12

1 Answers1

10

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.

Everett Carney
  • 260
  • 1
  • 12