Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #6 from michalbe/error-handler
Browse files Browse the repository at this point in the history
error handler
  • Loading branch information
michalbe committed Nov 9, 2012
2 parents f7cd3db + b742239 commit 042257f
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
data/
vendor/
sandbox/
.gitignore
test/servers/servers.json
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
72 changes: 64 additions & 8 deletions caldav.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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));
}
});
}
Expand Down Expand Up @@ -2941,7 +2988,8 @@ function write (chunk) {
content += this.filter.toString();
}

return this.template.render(content);
var out = this.template.render(content);
return out;
}

};
Expand All @@ -2956,6 +3004,8 @@ function write (chunk) {
));
(function(module, ns) {

var Errors = ns.require('request/errors');

/**
* Creates a propfind request.
*
Expand Down Expand Up @@ -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));
}

});
},

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/caldav/request/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -69,7 +69,7 @@
self._processResult(req, callback);
} else {
// fail
callback(new Error('http error code: ' + xhr.status));
callback(new Errors.CaldavHttpError(xhr.status));
}
});
}
Expand Down
15 changes: 12 additions & 3 deletions lib/caldav/request/calendar_home.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(function(module, ns) {

var Errors = ns.require('request/errors');

/**
* Creates a propfind request.
*
Expand Down Expand Up @@ -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));
}

});
},

Expand Down Expand Up @@ -109,7 +118,7 @@
self._findPrincipal(self.url, function(err, url) {

if (!url) {
callback(new Error('Cannot resolve principal url'));
callback(err);
return;
}

Expand Down
49 changes: 49 additions & 0 deletions lib/caldav/request/errors.js
Original file line number Diff line number Diff line change
@@ -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')]
));
2 changes: 0 additions & 2 deletions lib/caldav/sax/dav_response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test-agent/config.json
Original file line number Diff line number Diff line change
@@ -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"
]}
4 changes: 2 additions & 2 deletions test/caldav/request/abstract_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
24 changes: 22 additions & 2 deletions test/caldav/request/calendar_home_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ suite('caldav/request/propfind', function() {
var MockRequest;
var MockPropfind;
var Home;
var Errors;
var subject;
var con;

Expand All @@ -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() {
Expand Down Expand Up @@ -74,7 +76,7 @@ suite('caldav/request/propfind', function() {
response[url] = {
'current-user-principal': {
status: '200',
value: 'foo.com/'
value: { href:'foo.com/' }
}
};

Expand All @@ -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() {
Expand Down
6 changes: 4 additions & 2 deletions test/caldav/sax/dav_response_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ suite('caldav/sax/dav_response', function() {

'principal-URL': {
status: '200',
value: '/calendar/pinc/'
value: {
href: '/calendar/pinc/'
}
},

resourcetype: {
Expand All @@ -98,7 +100,7 @@ suite('caldav/sax/dav_response', function() {

'current-user-principal': {
status: '404',
value: null
value: {}
}
},

Expand Down

0 comments on commit 042257f

Please sign in to comment.