Skip to content

Commit

Permalink
Finnp patch 1 - Handle error events correctly (#164)
Browse files Browse the repository at this point in the history
* Handle error events correctly

The `onerror` and `ontimeout` handler are not calling back with errors but with error events (without any messages). Therefore the `errorFunc` is wrapping them as `Error: [ProgressEvent object]`. 

Here's my quick proposal on how to fix this. Just pass generic error messages to the `errorFunc`, then there will be no case where the error needs to be constructed there.

* fix error messages for tests

* remove new keywords and use .bind
  • Loading branch information
naugtur authored Jul 26, 2019
1 parent 62495d6 commit bb0ee82
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ function _createXHR(options) {

function errorFunc(evt) {
clearTimeout(timeoutTimer)
if(!(evt instanceof Error)){
evt = Error("" + (evt || "Unknown XMLHttpRequest Error") )
}
evt.statusCode = 0
return callback(evt, failureResponse)
}
Expand Down Expand Up @@ -150,11 +147,15 @@ function _createXHR(options) {

xhr.onreadystatechange = readystatechange
xhr.onload = loadFunc
xhr.onerror = errorFunc

xhr.onerror = errorFunc.bind({}, Error('Unknown XMLHttpRequest Error'))

xhr.onabort = function(){
aborted = true;
}
xhr.ontimeout = errorFunc

xhr.ontimeout = errorFunc.bind({}, Error('XMLHttpRequest Timeout'))

xhr.open(method, uri, !sync, options.username, options.password)
//has to be after open
if(!sync) {
Expand Down

0 comments on commit bb0ee82

Please sign in to comment.