Skip to content

Commit

Permalink
Fix promise being rejected on IE 9 when server response is invalid
Browse files Browse the repository at this point in the history
Reject all HTTP codes that are not in the 100..599 range.

When an empty reply is received from the server, `xhr.onerror` callback
should get triggered. IE 9, however, triggers the `onload` callback with
status code 12152 that stands for:

    ERROR_HTTP_INVALID_SERVER_RESPONSE
    The server response could not be parsed.

http://support.microsoft.com/kb/193625
  • Loading branch information
mislav committed Oct 31, 2014
1 parent eea8568 commit 5a2d5f8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,13 @@
var xhr = new XMLHttpRequest()

xhr.onload = function() {
var status = xhr.status
if (status < 100 || status > 599) {
reject()
return
}
var options = {
status: xhr.status,
status: status,
statusText: xhr.statusText,
headers: headers(xhr)
}
Expand Down
5 changes: 4 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ asyncTest('resolves promise on 500 error', 2, function() {
})

asyncTest('rejects promise for network error', 1, function() {
fetch('/error').catch(function() {
fetch('/error').then(function(response) {
ok(false, 'HTTP status ' + response.status + ' was treated as success')
start()
}).catch(function() {
ok(true)
start()
})
Expand Down

0 comments on commit 5a2d5f8

Please sign in to comment.