On an XMLHttpRequest error, expose error message
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
@dewwwald Thanks for looking into this! I appreciate your notes.
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?