aboutsummaryrefslogtreecommitdiffstats
path: root/lib/caldav/xhr.js
diff options
context:
space:
mode:
authorJames Lal <james@lightsofapollo.com>2013-05-10 17:25:19 -0700
committerJames Lal <james@lightsofapollo.com>2013-05-10 17:25:19 -0700
commit456913dc8b057cc44df753223e31800da62d8e98 (patch)
treed1accd27e745c36ecbe80f94375b49bc3b1b8646 /lib/caldav/xhr.js
parent9b5dd29c45ea08ff362198ffb984492df1ebadb1 (diff)
downloadjsCalDAV-456913dc8b057cc44df753223e31800da62d8e98.tar.gz
Rework http error validation so it can be reused r=kgrandon
Diffstat (limited to 'lib/caldav/xhr.js')
-rw-r--r--lib/caldav/xhr.js28
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/caldav/xhr.js b/lib/caldav/xhr.js
index 6c8351f..42e0c32 100644
--- a/lib/caldav/xhr.js
+++ b/lib/caldav/xhr.js
@@ -3,6 +3,7 @@
*/
(function(module, ns) {
var Native;
+ var Errors = ns.require('errors');
if (typeof(window) === 'undefined') {
Native = require('xmlhttprequest').XMLHttpRequest;
@@ -10,6 +11,19 @@
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
@@ -51,6 +65,7 @@
password: null,
url: null,
streaming: true,
+ validateStatus: false,
headers: {},
data: null,
@@ -168,7 +183,18 @@
}
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));