Skip to content

Commit

Permalink
Merge pull request JakeChampion#217 from bryanrsmith/fetch-reject-on-…
Browse files Browse the repository at this point in the history
…error

Reject the Promise returned by fetch() when Request ctor throws
  • Loading branch information
mislav committed Oct 10, 2015
2 parents ac12935 + e8d1235 commit bbb1a21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
14 changes: 7 additions & 7 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,14 @@
self.Response = Response;

self.fetch = function(input, init) {
var request
if (Request.prototype.isPrototypeOf(input) && !init) {
request = input
} else {
request = new Request(input, init)
}

return new Promise(function(resolve, reject) {
var request
if (Request.prototype.isPrototypeOf(input) && !init) {
request = input
} else {
request = new Request(input, init)
}

var xhr = new XMLHttpRequest()

function responseURL() {
Expand Down
13 changes: 10 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ test.skip('rejects promise for network error', function() {
})
})

test('rejects when Request constructor throws', function() {
return fetch('/request', { method: 'GET', body: 'invalid' }).then(function() {
assert(false, 'Invalid Request init was accepted')
}).catch(function(error) {
assert(error instanceof TypeError, 'Rejected with Error')
})
})

// https://fetch.spec.whatwg.org/#headers-class
suite('Headers', function() {
test('constructor copies headers', function() {
Expand Down Expand Up @@ -601,8 +609,7 @@ suite('Methods', function() {
})
})

// TODO: Waiting to verify behavior
test.skip('GET with body throws TypeError', function() {
test('GET with body throws TypeError', function() {
assert.throw(function() {
new Request('', {
method: 'get',
Expand All @@ -611,7 +618,7 @@ suite('Methods', function() {
}, TypeError)
})

test.skip('HEAD with body throws TypeError', function() {
test('HEAD with body throws TypeError', function() {
assert.throw(function() {
new Request('', {
method: 'head',
Expand Down

0 comments on commit bbb1a21

Please sign in to comment.