Permalink
Browse files

error handler

a

error-handling

a

err
  • Loading branch information...
1 parent 84c0722 commit b74223999c5a240306132704177eb9cbb1dc9143 @michalbe michalbe committed Nov 6, 2012
View
@@ -3,3 +3,5 @@ node_modules
data/
vendor/
sandbox/
+.gitignore
+test/servers/servers.json
View
@@ -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)
View
@@ -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) {
@@ -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));
}
});
}
@@ -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;
}
@@ -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')]
+));
@@ -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,
View
@@ -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"
]}
@@ -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);
});
});
@@ -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,14 +96,32 @@ suite('caldav/request/propfind', function() {
},
'principal-URL': {
status: '200',
- value: 'bar.com/'
+ value: { href: 'bar.com/' }
}
};
req.respond(null, response);
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() {
@@ -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: {}
}
},

0 comments on commit b742239

Please sign in to comment.