2

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?

Boris K
  • 3,442
  • 9
  • 48
  • 87
  • Is the issue related to expressjs, and how? The message you receive seems to come from some IIS. Have youy read this, already: https://stackoverflow.com/questions/12908764/iis-7-5-sending-http-status-code-422-with-custom-errors-on – p-a-o-l-o Nov 20 '17 at 08:20
  • Yes, read it and added the suggested code to Web.config. The issue persists. – Boris K Nov 20 '17 at 08:26
  • Sorry for bothering, but I ask you again: how the issue is related to express? – p-a-o-l-o Nov 20 '17 at 08:28
  • I am using Express for my backend and returning errors from there. I do not know why the errors are being transformed on their way to the front end. – Boris K Nov 20 '17 at 08:29

2 Answers2

2

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

enter image description here

The browser output:

enter image description here

Aaron Chen
  • 9,835
  • 1
  • 16
  • 28
1

Not sure, but following should work.

return res.status(422).send('Email is already in use');
Vipin Kumar
  • 6,441
  • 1
  • 19
  • 25