diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/caldav/errors.js | 40 | ||||
-rw-r--r-- | lib/caldav/index.js | 1 | ||||
-rw-r--r-- | lib/caldav/request/abstract.js | 30 | ||||
-rw-r--r-- | lib/caldav/request/calendar_home.js | 31 | ||||
-rw-r--r-- | lib/caldav/request/errors.js | 49 |
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')] -)); |