diff options
author | James Lal <james@lightsofapollo.com> | 2013-05-10 17:33:00 -0700 |
---|---|---|
committer | James Lal <james@lightsofapollo.com> | 2013-05-10 17:33:00 -0700 |
commit | 09d9ad3f92edec992bd1ab2e6a1c505f16bf22de (patch) | |
tree | d8c5b306760c9de5359aafe428753a4f3326c7a5 /caldav.js | |
parent | 9b5dd29c45ea08ff362198ffb984492df1ebadb1 (diff) | |
parent | 5f35b858c31d455894f3c700eade1d1ca0ca4d3a (diff) | |
download | jsCalDAV-09d9ad3f92edec992bd1ab2e6a1c505f16bf22de.tar.gz |
Merge pull request #17 from lightsofapollo/asset-http-status-checks
Rework http error validation so it can be reused r=kgrandon
Diffstat (limited to 'caldav.js')
-rw-r--r-- | caldav.js | 62 |
1 files changed, 33 insertions, 29 deletions
@@ -2107,6 +2107,7 @@ function write (chunk) { */ (function(module, ns) { var Native; + var Errors = ns.require('errors'); if (typeof(window) === 'undefined') { Native = require('xmlhttprequest').XMLHttpRequest; @@ -2114,6 +2115,19 @@ function write (chunk) { Native = window.XMLHttpRequest; } + function determineHttpStatusError(status) { + var message = 'Cannot handle request due to server response'; + var err = 'Unknown'; + + if (status === 500) + err = 'ServerFailure'; + + if (status === 401) + err = 'Authentication'; + + return new Errors[err](message); + } + /** * Creates a XHR wrapper. * Depending on the platform this is loaded @@ -2155,6 +2169,7 @@ function write (chunk) { password: null, url: null, streaming: true, + validateStatus: false, headers: {}, data: null, @@ -2272,7 +2287,18 @@ function write (chunk) { } this.waiting = false; - callback(null, this.xhr); + + if ( + !this.validateStatus || + ( + this.xhr.status > 199 && + this.xhr.status < 300 + ) + ) { + return callback(null, this.xhr); + } + + callback(determineHttpStatusError(this.xhr.status), this.xhr); } }.bind(this)); @@ -2581,7 +2607,8 @@ function write (chunk) { } BasicAuth.prototype = { - __proto__: XHR.prototype + __proto__: XHR.prototype, + validateStatus: true }; @@ -2640,6 +2667,8 @@ function write (chunk) { Oauth2.prototype = { __proto__: XHR.prototype, + validateStatus: true, + _sendXHR: function(xhr) { xhr.setRequestHeader( 'Authorization', 'Bearer ' + this.connection.oauth.access_token @@ -3166,20 +3195,6 @@ function write (chunk) { var SAX = ns.require('sax'); var XHR = ns.require('xhr'); - var Errors = ns.require('errors'); - - function determineHttpStatusError(status) { - var message = 'Cannot handle request due to server response'; - var err = 'Unknown'; - - if (status === 500) - err = 'ServerFailure'; - - if (status === 401) - err = 'Authentication'; - - return new Errors[err](message); - } /** * Creates an (Web/Cal)Dav request. @@ -3249,19 +3264,8 @@ function write (chunk) { return callback(err); } - // handle the success case - if (xhr.status > 199 && xhr.status < 300) { - self.sax.close(); - return self._processResult(req, callback); - } - - // probable error cases - callback( - determineHttpStatusError(xhr.status), - xhr - ); - - + self.sax.close(); + return self._processResult(req, callback); }); return req; |