Express: Receiving Custom error messages from fetch

Created on 5 Jan 2017  路  9Comments  路  Source: expressjs/express

Couldn't receive custom error messages that is responded by express server

when I use fetch, however jQuery.ajax.fail() could receive it

http://expressjs.com/en/api.html#res.send
http://expressjs.com/en/api.html#res.json

Still I get the default statusText in response

For example,
res.status(400).send({ message: 'Password reset token is invalid or has expired' });
I get statusText=Bad request and I don't receive message

4.x question

All 9 comments

@p10ns11y what Node and Express version are you using? Can you share the simplest example which reproduces the behavior?

@hacksparrow :
node: 6.2.0
express: 4.14.0

Client:

fetch('api/any', {
  method: 'POST',
  body: JSON.stringify({ post: 'box' })
}).then((response) => {
   /* 
   response => {
     body: (...)
     bodyUsed : false
     headers : Headers
     ok: false
     status : 400
     statusText: "Bad Request"
     type: "basic"
     url: "https://localhost:8443/api/any"
   }
   */ 
})

Server:

res.status(400).send({ message: 'I have nothing for you' });

Is it fetch ignoring the message upon seeing error status?

I was expecting the simplest server code :)

@peramsathyam @p10ns11y The statusText property is a HTTP property that gets sent along with the HTTP status code. What you're sending is a JSON payload. If you really wanted to change statusText, you'd need to set it: https://nodejs.org/api/http.html#http_response_statusmessage.

HTTP/1.1 200 OK

@blakeembrey is correct. I was thinking you were getting something else in the response body.

@hacksparrow @blakeembrey

I was aware of statusMessage. But, basically I wanted to understand how
$.ajax().fail() could throw the custom error message

https://runkit.com/peramanathan/server-test-fetch-ajax/1.0.1

Express does not provide an API for this because not all of the supported Node.js versions of Express support this functionality, and it's not recommended you do this, because your user may have a proxy between them and your server that assumes the status message is not custom, breaking your expectations in the browser.

If you really want to do this, you'll need to use the raw Node.js directly: https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_response_statusmessage and be using Node.js 0.12+ and have no intermediate proxies between your Node.js server and the browser that rewrite that line (it's very common for that to happen, which is another reason we don't even bother to offer an Express API).

You use Node.js APIs in Express directly:

app.use(function (req, res) {
  res.status(400)
  res.statusMessage ='Nothing'
  res.send({ message: 'I have nothing for you' })
})

@peramsathyam In that case, this isn't really a question of Express but fetch. You should probably check out the documentation (e.g. https://developer.mozilla.org/en/docs/Web/API/Fetch_API) - JSON payloads are not automatically parsed in the response object. What you want to do is fetch().then(res => res.json()).then(data => console.log(data)).

Closing to move the conversation to the support forum.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Sunriselegacy picture Sunriselegacy  路  3Comments

afanasy picture afanasy  路  3Comments

nove1398 picture nove1398  路  3Comments

extensionsapp picture extensionsapp  路  3Comments

HafidAbnaou picture HafidAbnaou  路  3Comments