question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Receiving Custom error messages from fetch

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
dougwilsoncommented, Jan 6, 2017

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' })
})
0reactions
dougwilsoncommented, Jan 6, 2017

Closing to move the conversation to the support forum.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript fetch api use custom error message - Stack Overflow
So I want to use the response body text as a the error. I tried different ways, but I can't get the response...
Read more >
Fetch error message javascript - MailSlurp
When using Javascript fetch to make HTTP requests an exception is thrown for any response with a code of 400 or more. This...
Read more >
Fetch - Error Handling for Failed HTTP Responses and ...
A quick example of how to handle both network errors and HTTP errors (4xx or 5xx) for fetch requests in a single catch...
Read more >
Handling Failed HTTP Responses With fetch() - TJ VanToll
Per MDN, the fetch() API only rejects a promise when a “network error is encountered, although this usually means permissions issues or similar....
Read more >
Error handling while using native fetch API in JavaScript
First lets see, without handling errors,. fetch(url) .then(response => ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found