diff options
author | Michal Budzynski <michal@virtualdesign.pl> | 2012-11-06 11:54:07 -0800 |
---|---|---|
committer | Michal Budzynski <michal@virtualdesign.pl> | 2012-11-08 16:16:07 -0800 |
commit | b74223999c5a240306132704177eb9cbb1dc9143 (patch) | |
tree | f2980f8379b9abfaebf2fe7d41d0a9415cd13517 | |
parent | 84c07224a04189d42abbcd2dbfaf541fa53a90ff (diff) | |
download | jsCalDAV-b74223999c5a240306132704177eb9cbb1dc9143.tar.gz |
error handler
a
error-handling
a
err
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | caldav.js | 74 | ||||
-rw-r--r-- | lib/caldav/request/abstract.js | 4 | ||||
-rw-r--r-- | lib/caldav/request/calendar_home.js | 15 | ||||
-rw-r--r-- | lib/caldav/request/errors.js | 49 | ||||
-rw-r--r-- | lib/caldav/sax/dav_response.js | 2 | ||||
-rw-r--r-- | test-agent/config.json | 2 | ||||
-rw-r--r-- | test/caldav/request/abstract_test.js | 4 | ||||
-rw-r--r-- | test/caldav/request/calendar_home_test.js | 24 | ||||
-rw-r--r-- | test/caldav/sax/dav_response_test.js | 6 |
11 files changed, 159 insertions, 24 deletions
@@ -3,3 +3,5 @@ node_modules data/ vendor/ sandbox/ +.gitignore +test/servers/servers.json
\ No newline at end of file @@ -30,6 +30,7 @@ package: test-agent-config cat $(LIB_ROOT)/sax/base.js >> $(WEB_FILE) cat $(LIB_ROOT)/sax/calendar_data_handler.js >> $(WEB_FILE) cat $(LIB_ROOT)/sax/dav_response.js >> $(WEB_FILE) + cat $(LIB_ROOT)/request/errors.js >> $(WEB_FILE) cat $(LIB_ROOT)/request/abstract.js >> $(WEB_FILE) cat $(LIB_ROOT)/request/asset.js >> $(WEB_FILE) cat $(LIB_ROOT)/request/propfind.js >> $(WEB_FILE) @@ -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, @@ -2559,9 +2557,58 @@ function write (chunk) { )); (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) { diff --git a/lib/caldav/request/abstract.js b/lib/caldav/request/abstract.js index 04101b5..41429c6 100644 --- a/lib/caldav/request/abstract.js +++ b/lib/caldav/request/abstract.js @@ -2,7 +2,7 @@ var SAX = ns.require('sax'); var XHR = ns.require('xhr'); - + var Errors = ns.require('request/errors'); /** * Creates an (Web/Cal)Dav request. @@ -69,7 +69,7 @@ self._processResult(req, callback); } else { // fail - callback(new Error('http error code: ' + xhr.status)); + callback(new Errors.CaldavHttpError(xhr.status)); } }); } diff --git a/lib/caldav/request/calendar_home.js b/lib/caldav/request/calendar_home.js index 5ededb5..24b27bc 100644 --- a/lib/caldav/request/calendar_home.js +++ b/lib/caldav/request/calendar_home.js @@ -1,5 +1,7 @@ (function(module, ns) { + var Errors = ns.require('request/errors'); + /** * Creates a propfind request. * @@ -71,8 +73,15 @@ 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)); + } + }); }, @@ -109,7 +118,7 @@ self._findPrincipal(self.url, function(err, url) { if (!url) { - callback(new Error('Cannot resolve principal url')); + callback(err); return; } diff --git a/lib/caldav/request/errors.js b/lib/caldav/request/errors.js new file mode 100644 index 0000000..c3c166e --- /dev/null +++ b/lib/caldav/request/errors.js @@ -0,0 +1,49 @@ +(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')] +)); diff --git a/lib/caldav/sax/dav_response.js b/lib/caldav/sax/dav_response.js index 6953161..7eb1da0 100644 --- a/lib/caldav/sax/dav_response.js +++ b/lib/caldav/sax/dav_response.js @@ -134,8 +134,6 @@ '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, diff --git a/test-agent/config.json b/test-agent/config.json index 4cf307a..0453099 100644 --- a/test-agent/config.json +++ b/test-agent/config.json @@ -1,3 +1,3 @@ {"tests": [ -"/test/caldav/connection_test.js","/test/caldav/index_test.js","/test/caldav/query_builder_test.js","/test/caldav/request/abstract_test.js","/test/caldav/request/asset_test.js","/test/caldav/request/calendar_home_test.js","/test/caldav/request/calendar_query_test.js","/test/caldav/request/propfind_test.js","/test/caldav/request/resources_test.js","/test/caldav/resources/calendar_test.js","/test/caldav/sax/base_test.js","/test/caldav/sax/calendar_data_handler_test.js","/test/caldav/sax/dav_response_test.js","/test/caldav/sax_test.js","/test/caldav/template_test.js","/test/caldav/xhr_test.js","/test/servers/calendar_home_test.js" +"/test/caldav/connection_test.js","/test/caldav/index_test.js","/test/caldav/query_builder_test.js","/test/caldav/request/abstract_test.js","/test/caldav/request/asset_test.js","/test/caldav/request/calendar_home_test.js","/test/caldav/request/calendar_query_test.js","/test/caldav/request/propfind_test.js","/test/caldav/request/resources_test.js","/test/caldav/resources/calendar_test.js","/test/caldav/sax/base_test.js","/test/caldav/sax/calendar_data_handler_test.js","/test/caldav/sax/dav_response_test.js","/test/caldav/sax_test.js","/test/caldav/template_test.js","/test/caldav/xhr_test.js","/test/servers/home_test.js","/test/servers/query_test.js","/test/servers/resources_test.js" ]} diff --git a/test/caldav/request/abstract_test.js b/test/caldav/request/abstract_test.js index 88d8b2b..ee70b67 100644 --- a/test/caldav/request/abstract_test.js +++ b/test/caldav/request/abstract_test.js @@ -83,9 +83,9 @@ suite('caldav/request/abstract.js', function() { xhr = getXhr(); xhr.respond('NOT XML <div>', 500); }); - + test('on response', function() { - assert.match(calledWith[0].message, /http error/); + assert.equal(calledWith[0].code, 500); }); }); diff --git a/test/caldav/request/calendar_home_test.js b/test/caldav/request/calendar_home_test.js index 88eb905..13483ad 100644 --- a/test/caldav/request/calendar_home_test.js +++ b/test/caldav/request/calendar_home_test.js @@ -9,6 +9,7 @@ suite('caldav/request/propfind', function() { var MockRequest; var MockPropfind; var Home; + var Errors; var subject; var con; @@ -19,6 +20,7 @@ suite('caldav/request/propfind', function() { Connection = Caldav.require('connection'); Home = Caldav.require('request/calendar_home'); MockRequest = Caldav.require('support/mock_request'); + Errors = Caldav.require('request/errors'); }); suiteSetup(function() { @@ -74,7 +76,7 @@ suite('caldav/request/propfind', function() { response[url] = { 'current-user-principal': { status: '200', - value: 'foo.com/' + value: { href:'foo.com/' } } }; @@ -94,7 +96,7 @@ suite('caldav/request/propfind', function() { }, 'principal-URL': { status: '200', - value: 'bar.com/' + value: { href: 'bar.com/' } } }; @@ -102,6 +104,24 @@ suite('caldav/request/propfind', function() { assert.equal(data, 'bar.com/'); }); + + test('unauthenticated', function() { + var req = request('_findPrincipal'); + + response[url] = { + 'principal-URL': { + status: '200', + value: { + unauthenticated: {} + } + } + }; + + req.respond(null, response); + + assert.equal(true, err instanceof Errors.UnauthenticatedError); + }); + }); suite('#_findCalendarHome', function() { diff --git a/test/caldav/sax/dav_response_test.js b/test/caldav/sax/dav_response_test.js index 926f975..d435ef8 100644 --- a/test/caldav/sax/dav_response_test.js +++ b/test/caldav/sax/dav_response_test.js @@ -85,7 +85,9 @@ suite('caldav/sax/dav_response', function() { 'principal-URL': { status: '200', - value: '/calendar/pinc/' + value: { + href: '/calendar/pinc/' + } }, resourcetype: { @@ -98,7 +100,7 @@ suite('caldav/sax/dav_response', function() { 'current-user-principal': { status: '404', - value: null + value: {} } }, |