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.

On an XMLHttpRequest error, expose error message

See original GitHub issue

I am trying to get a more useful error message out of fetch when there are network errors…

For context: I am using react native, which uses this library internally for its fetch implementation. If I am connecting to a server whose cert is invalid, or have a typo in the server name, its nicer to expose a more descriptive error message to the user. The XMLHttpRequest that react native provides does this, but fetch ignores it and always sends back ‘Network request failed’.

I believe this is because: in the fetch constructor the onerror and ontimeout callbacks from the XMLHttpRequest object are wired up like this:

      xhr.onerror = function() {
        reject(new TypeError('Network request failed'))
      }

      xhr.ontimeout = function() {
        reject(new TypeError('Network request failed'))
      }

Sadly, no information about what the error was from the XMLHttpRequest object is actually exposed.

Could it be changed to something like this:

      xhr.onerror = function() {
        reject(new TypeError(xhr.responseText || 'Network request failed'))
      }

      xhr.ontimeout = function() {
        reject(new TypeError(xhr.responseText || 'Network request failed'))
      }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mislavcommented, Mar 29, 2018

@dewwwald Thanks for looking into this! I appreciate your notes.

1reaction
dewwwaldcommented, Mar 28, 2018

I am a fullstack developer. I work on IOS, Web and NodeJs platforms, I might be of assistance here. This implementation would also be a tremendous benefit to me. I believe errors should not be recreated and concealed as they are in this library, or at least not in the way that it has been done here. (debugging is really difficult if I don’t know what the cause of my error is)

I can do some digging and make a PR as soon as possible, I will be adding comments here. Would you guys mind checking my comments and telling me what you think while I work on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting the error text in an XMLHttpRequest error call back
When it runs, the getFailed() callback does indeed get called, but I can't find any information on how to determine what the error...
Read more >
XMLHttpRequest: error event - Web APIs | MDN
The error event is fired when the request encountered an error. Syntax. Use the event name in methods like addEventListener() , or set...
Read more >
118096 - XHR error message is not sent to error handler.
Convincing the XMLHttpRequest spec to change by exposing more information upon network errors, seems like the best next step here.
Read more >
XMLHttpRequest Standard
The request error steps for an XMLHttpRequest object xhr , event , and optionally exception are: Set xhr 's state to done. Unset...
Read more >
Flutter Web Http Client Issue & Resolution of XMLHttpRequest ...
Flutter Web Http Client throws XMLHttpRequest Error. Solution is to enable CORS support in your server side. Enabling CORS Support in Spring ...
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