Boris K
Boris K

Reputation: 3580

ExpressJS: The custom error module does not recognize this error

I'm running an Express.JS backend, and have it set to return a 422 error with a custom message if a user attempts to sign up with an email that's already in use, like so:

const existingUser = await User.findOne({ email: email });
    if (existingUser) {
        return res.status(422).send({ error: 'Email is already in use' });
    }

However, the response I see on the front end is this:

"response":{"data":"The custom error module does not recognize this error.","status":422,"statusText":"Unprocessable Entity"

Something is, apparently, changing the error on the way to the front end.

I'm running this locally right now for development, but the production is deployed to Azure, so I have a Web.config file and an iisnode.yml one. I've added this code to the former, to make sure it's not causing the problem:

<system.webServer>
  <httpErrors existingResponse="PassThrough">
  </httpErrors>
</system.webServer>

How do I go about troubleshooting this thing?

Upvotes: 2

Views: 1218

Answers (2)

Aaron Chen
Aaron Chen

Reputation: 9950

It worked for me after setting <httpErrors> like below:

enter image description here

The browser output:

enter image description here

Upvotes: 2

Vipin Kumar
Vipin Kumar

Reputation: 6546

Not sure, but following should work.

return res.status(422).send('Email is already in use');

Upvotes: 1

Related Questions