Custom error messages in Node Azure Web App

Stumbled upon an interesting issue with an Azure web application running Node with IIS and wanted to share because I couldn't find any information about it.

Problem:

My custom error messages weren't making it a client in my production app, but going through 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 happened through my web application as the default "400: Bad Request ..." message, not my custom error message. On the client, I was trying to do JSON.parse(err.responseText).error

, which didn't work like err.responseText

, was a string "Bad request..."

and not my JSON object.

+3


source to share


1 answer


Decision!

In your file web.config

add this line between <system.webServer> ... </system.webServer>

:

<httpErrors existingResponse="PassThrough" />



Hopefully no one else will face this problem like me. I'm sure it was a mixture of incorrect searching and inexperience with IIS, but I never found an answer on the internet, I came across several links that led me to the IIS error handling page .

Hope this helps someone else! There is a lot of good information in this document, if you are using an IIS web application I highly recommend reading it.

+7


source







All Articles