Skip to content

Commit

Permalink
Merge pull request #92 from Raynos/distinct-errors
Browse files Browse the repository at this point in the history
#89 improved for compatibility
  • Loading branch information
naugtur committed Sep 14, 2015
2 parents bc9674d + 4611898 commit 7fd6764
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function createXHR(options, callback) {
function errorFunc(evt) {
clearTimeout(timeoutTimer)
if(!(evt instanceof Error)){
evt = new Error("" + (evt || "unknown") )
evt = new Error("" + (evt || "Unknown XMLHttpRequest Error") )
}
evt.statusCode = 0
callback(evt, failureResponse)
Expand Down Expand Up @@ -153,7 +153,9 @@ function createXHR(options, callback) {
timeoutTimer = setTimeout(function(){
aborted=true//IE9 may still call readystatechange
xhr.abort("timeout")
errorFunc();
var e = new Error("XMLHttpRequest timeout")
e.code = "ETIMEDOUT"
errorFunc(e)
}, options.timeout )
}

Expand Down
4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ test("[func] Times out to an error ", function (assert) {
uri: "/tests-bundle.js?should-take-a-bit-to-parse=1&" + (new Array(300)).join("cachebreaker=" + Math.random().toFixed(5) + "&")
}, function (err, resp, body) {
assert.ok(err instanceof Error, "should return error")
assert.equal(err.message, "XMLHttpRequest timeout")
assert.equal(err.code, "ETIMEDOUT")
assert.equal(resp.statusCode, 0)
assert.end()
})
Expand Down Expand Up @@ -110,7 +112,7 @@ test("XDR usage (run on IE8 or 9)", function (assert) {
test("handles errorFunc call with no arguments provided", function (assert) {
var req = xhr({}, function (err) {
assert.ok(err instanceof Error, "callback should get an error")
assert.equal(err.message, "unknown", "error message should say 'unknown'")
assert.equal(err.message, "Unknown XMLHttpRequest Error", "error message incorrect")
})
assert.doesNotThrow(function () {
req.onerror()
Expand Down

0 comments on commit 7fd6764

Please sign in to comment.