aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisko Hevery2010-05-19 12:00:44 -0700
committerMisko Hevery2010-05-19 12:00:44 -0700
commit31b35b141f52e6f5d3805d6ca4f2702aee05d61d (patch)
tree665d1098e07ede09890e3f2a437ca3ffcb2fe67d
parentf2abbfd394691f87860d5c8dc28c2c1d0310c90f (diff)
downloadangular.js-31b35b141f52e6f5d3805d6ca4f2702aee05d61d.tar.bz2
added exception handling to $xhr
-rw-r--r--src/services.js6
-rw-r--r--test/servicesSpec.js27
2 files changed, 23 insertions, 10 deletions
diff --git a/src/services.js b/src/services.js
index 99e05018..aa749e32 100644
--- a/src/services.js
+++ b/src/services.js
@@ -196,7 +196,7 @@ angularService('$route', function(location, params){
return $route;
}, {inject: ['$location']});
-angularService('$xhr', function($browser, $error){
+angularService('$xhr', function($browser, $error, $log){
var self = this;
return function(method, url, post, callback){
if (isFunction(post)) {
@@ -218,12 +218,14 @@ angularService('$xhr', function($browser, $error){
{method: method, url:url, data:post, callback:callback},
{status: code, body:response});
}
+ } catch (e) {
+ $log.error(e);
} finally {
self.$eval();
}
});
};
-}, {inject:['$browser', '$xhr.error']});
+}, {inject:['$browser', '$xhr.error', '$log']});
angularService('$xhr.error', function($log){
return function(request, response){
diff --git a/test/servicesSpec.js b/test/servicesSpec.js
index 4e144dd1..60b465d2 100644
--- a/test/servicesSpec.js
+++ b/test/servicesSpec.js
@@ -1,10 +1,12 @@
describe("service", function(){
- var scope, xhrErrorHandler;
+ var scope, $xhrError, $log;
beforeEach(function(){
- xhrErrorHandler = jasmine.createSpy('$xhr.error');
+ $xhrError = jasmine.createSpy('$xhr.error');
+ $log = {};
scope = createScope(null, angularService, {
- '$xhr.error': xhrErrorHandler
+ '$xhr.error': $xhrError,
+ '$log': $log
});
});
@@ -201,13 +203,22 @@ describe("service", function(){
xhr.expectPOST('/req', 'MyData').respond(500, 'MyError');
scope.$xhr('POST', '/req', 'MyData', callback);
xhr.flush();
- var cb = xhrErrorHandler.mostRecentCall.args[0].callback;
+ var cb = $xhrError.mostRecentCall.args[0].callback;
expect(typeof cb).toEqual('function');
- expect(xhrErrorHandler).wasCalledWith(
+ expect($xhrError).wasCalledWith(
{url:'/req', method:'POST', data:'MyData', callback:cb},
{status:500, body:'MyError'});
});
+ it('should handle exceptions in callback', function(){
+ $log.error = jasmine.createSpy('$log.error');
+ xhr.expectGET('/reqGET').respond('first');
+ scope.$xhr('GET', '/reqGET', null, function(){ throw "MyException"; });
+ xhr.flush();
+
+ expect($log.error).wasCalledWith("MyException");
+ });
+
describe('bulk', function(){
it('should collect requests', function(){
scope.$xhr.bulk.urls["/"] = {match:/.*/};
@@ -241,10 +252,10 @@ describe("service", function(){
scope.$xhr.bulk.flush(function(){ log += 'DONE';});
xhr.flush();
- expect(xhrErrorHandler).wasCalled();
- var cb = xhrErrorHandler.mostRecentCall.args[0].callback;
+ expect($xhrError).wasCalled();
+ var cb = $xhrError.mostRecentCall.args[0].callback;
expect(typeof cb).toEqual('function');
- expect(xhrErrorHandler).wasCalledWith(
+ expect($xhrError).wasCalledWith(
{url:'/req1', method:'GET', data:null, callback:cb},
{status:404, body:'NotFound'});