aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJames Lal <james@lightsofapollo.com>2013-05-06 21:18:31 -0700
committerJames Lal <james@lightsofapollo.com>2013-05-06 21:18:31 -0700
commit0b4733ebb28368198b747079d51033a7eeb1f276 (patch)
tree0d48aca900256baf82f0aa599d455a978780fa19 /lib
parentc81e925aa6dada192db75dccd4287ab1e9e09ab2 (diff)
downloadjsCalDAV-0b4733ebb28368198b747079d51033a7eeb1f276.tar.gz
Initial error refactoring (make errors less HTTP specific)
Diffstat (limited to 'lib')
-rw-r--r--lib/caldav/errors.js40
-rw-r--r--lib/caldav/index.js1
-rw-r--r--lib/caldav/request/abstract.js30
-rw-r--r--lib/caldav/request/calendar_home.js31
-rw-r--r--lib/caldav/request/errors.js49
5 files changed, 87 insertions, 64 deletions
diff --git a/lib/caldav/errors.js b/lib/caldav/errors.js
new file mode 100644
index 0000000..1b58a6f
--- /dev/null
+++ b/lib/caldav/errors.js
@@ -0,0 +1,40 @@
+(function(module, ns) {
+
+ Errors = {};
+
+ /**
+ * Errors typically are for front-end routing purposes so the important
+ * part really is just the name and (maybe) the symbol... These are really
+ * intended to be consumed by name... So once a name has been assigned it
+ * should never be modified.
+ */
+ [
+ { symbol: 'Authentication', name: 'authentication' },
+ { symbol: 'InvalidEntrypoint', name: 'invalid-entrypoint' },
+ { symbol: 'ServerFailure', name: 'server-failure' },
+ { symbol: 'Unknown', name: 'unknown' }
+ ].forEach(function createError(def) {
+ var obj = Errors[def.symbol] = function(message) {
+ this.message = message;
+ this.name = 'caldav-' + def.name;
+
+ try {
+ throw new Error();
+ } catch (e) {
+ this.stack = e.stack;
+ }
+ };
+
+ // just so instanceof Error works
+ obj.prototype = Object.create(Error.prototype);
+ });
+
+ module.exports = Errors;
+
+}.apply(
+ this,
+ (this.Caldav) ?
+ [Caldav('errors'), Caldav] :
+ [module, require('./caldav')]
+));
+
diff --git a/lib/caldav/index.js b/lib/caldav/index.js
index 636fc18..3e3dd51 100644
--- a/lib/caldav/index.js
+++ b/lib/caldav/index.js
@@ -12,6 +12,7 @@
exports.Resources = ns.require('resources');
exports.Http = ns.require('http');
exports.OAuth2 = ns.require('oauth2');
+ exports.Errors = ns.require('errors');
}.apply(
this,
diff --git a/lib/caldav/request/abstract.js b/lib/caldav/request/abstract.js
index 4db3883..6f33af4 100644
--- a/lib/caldav/request/abstract.js
+++ b/lib/caldav/request/abstract.js
@@ -2,7 +2,20 @@
var SAX = ns.require('sax');
var XHR = ns.require('xhr');
- var Errors = ns.require('request/errors');
+ 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.
@@ -72,14 +85,19 @@
return callback(err);
}
+ // handle the success case
if (xhr.status > 199 && xhr.status < 300) {
- // success
self.sax.close();
- self._processResult(req, callback);
- } else {
- // fail
- callback(new Errors.CaldavHttpError(xhr.status));
+ return self._processResult(req, callback);
}
+
+ // probable error cases
+ callback(
+ determineHttpStatusError(xhr.status),
+ xhr
+ );
+
+
});
return req;
diff --git a/lib/caldav/request/calendar_home.js b/lib/caldav/request/calendar_home.js
index d0659bb..e7bed35 100644
--- a/lib/caldav/request/calendar_home.js
+++ b/lib/caldav/request/calendar_home.js
@@ -1,7 +1,7 @@
(function(module, ns) {
- var Errors = ns.require('request/errors');
-
+ var RequestErrors = ns.require('errors');
+
/**
* Creates a propfind request.
*
@@ -72,19 +72,32 @@
return;
}
- principal = findProperty('current-user-principal', data, true);
+ // some fairly dumb allowances
+ principal =
+ findProperty('current-user-principal', data, true) ||
+ findProperty('principal-URL', data, true);
if (!principal) {
- principal = findProperty('principal-URL', data, true);
+ return callback(new Errors.InvalidEntrypoint(
+ 'both current-user-principal and principal-URL are missing'
+ ));
}
+ // per http://tools.ietf.org/html/rfc6638 we get unauthenticated
if ('unauthenticated' in principal) {
- callback(new Errors.UnauthenticatedError());
- } else if (principal.href) {
- callback(null, principal.href);
- } else {
- callback(new Errors.CaldavHttpError(404));
+ return callback(
+ new Errors.Authentication('caldav response is unauthenticated')
+ );
+ }
+
+ // we might have both principal.href & unauthenticated
+ if (principal.href) {
+ return callback(null, principal.href);
}
+
+ callback(
+ new Errors.InvalidEntrypoint('no useful location information found')
+ );
});
},
diff --git a/lib/caldav/request/errors.js b/lib/caldav/request/errors.js
deleted file mode 100644
index 700cfba..0000000
--- a/lib/caldav/request/errors.js
+++ /dev/null
@@ -1,49 +0,0 @@
-(function(module, ns) {
-
- function CaldavHttpError(code) {
- this.code = code;
- var message;
- switch(this.code) {
- case 401:
- message = 'Wrong username or/and password';
- break;
- case 404:
- message = 'Url not found';
- break;
- case 500:
- message = 'Server error';
- break;
- default:
- message = this.code;
- }
- Error.call(this, message);
- }
-
- CaldavHttpError.prototype = {
- __proto__: Error.prototype,
- constructor: CaldavHttpError
- }
-
- // Unauthenticated error for
- // Google Calendar
- function UnauthenticatedError() {
- var message = "Wrong username or/and password";
- Error.call(this, message);
- }
-
- UnauthenticatedError.prototype = {
- __proto__: Error.prototype,
- constructor: UnauthenticatedError
- }
-
- module.exports = {
- CaldavHttpError: CaldavHttpError,
- UnauthenticatedError: UnauthenticatedError
- };
-
-}.apply(
- this,
- (this.Caldav) ?
- [Caldav('request/errors'), Caldav] :
- [module, require('../caldav')]
-));