From b74223999c5a240306132704177eb9cbb1dc9143 Mon Sep 17 00:00:00 2001 From: Michal Budzynski Date: Tue, 6 Nov 2012 11:54:07 -0800 Subject: error handler a error-handling a err --- caldav.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 10 deletions(-) (limited to 'caldav.js') diff --git a/caldav.js b/caldav.js index 3c13c54..98bcf87 100644 --- a/caldav.js +++ b/caldav.js @@ -2464,8 +2464,6 @@ function write (chunk) { 'DAV:/status': HttpStatusHandler, 'DAV:/resourcetype': ArrayHandler, 'DAV:/current-user-privilege-set': PrivilegeSet, - 'DAV:/principal-URL': HrefHandler, - 'DAV:/current-user-principal': HrefHandler, 'urn:ietf:params:xml:ns:caldav/calendar-data': CalendarDataHandler, 'DAV:/value': TextHandler, 'DAV:/owner': HrefHandler, @@ -2557,11 +2555,60 @@ function write (chunk) { [Caldav('sax/dav_response'), Caldav] : [module, require('../caldav')] )); +(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')] +)); (function(module, ns) { var SAX = ns.require('sax'); var XHR = ns.require('xhr'); - + var Errors = ns.require('request/errors'); /** * Creates an (Web/Cal)Dav request. @@ -2628,7 +2675,7 @@ function write (chunk) { self._processResult(req, callback); } else { // fail - callback(new Error('http error code: ' + xhr.status)); + callback(new Errors.CaldavHttpError(xhr.status)); } }); } @@ -2941,7 +2988,8 @@ function write (chunk) { content += this.filter.toString(); } - return this.template.render(content); + var out = this.template.render(content); + return out; } }; @@ -2956,6 +3004,8 @@ function write (chunk) { )); (function(module, ns) { + var Errors = ns.require('request/errors'); + /** * Creates a propfind request. * @@ -3027,8 +3077,14 @@ function write (chunk) { if (!principal) { principal = findProperty('principal-URL', data, true); } - - callback(null, principal); + if ('unauthenticated' in principal) { + callback(new Errors.UnauthenticatedError()); + } else if (principal.href){ + callback(null, principal.href); + } else { + callback(new Errors.CaldavHttpError(404)); + } + }); }, @@ -3065,7 +3121,7 @@ function write (chunk) { self._findPrincipal(self.url, function(err, url) { if (!url) { - callback(new Error('Cannot resolve principal url')); + callback(err); return; } @@ -3116,9 +3172,7 @@ function write (chunk) { for (url in root) { collection = root[url]; - resources = collection.resourcetype; - if (resources.value.forEach) { resources.value.forEach(function(type) { -- cgit