aboutsummaryrefslogtreecommitdiffstats
path: root/src/services.js
diff options
context:
space:
mode:
authorMisko Hevery2010-05-19 11:51:17 -0700
committerMisko Hevery2010-05-19 11:51:17 -0700
commit0f73084e9d21cea99f0535e6ca30a1341b7047dc (patch)
treee4586731808a708ec0a8ce137c30e99e3cb7201b /src/services.js
parent1bdcf72e456c74256b14f98b26e969b9de637614 (diff)
downloadangular.js-0f73084e9d21cea99f0535e6ca30a1341b7047dc.tar.bz2
added error handler to xhr requests
Diffstat (limited to 'src/services.js')
-rw-r--r--src/services.js30
1 files changed, 24 insertions, 6 deletions
diff --git a/src/services.js b/src/services.js
index 55e3b2f6..f1dc5c28 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){
+angularService('$xhr', function($browser, $error){
var self = this;
return function(method, url, post, callback){
if (isFunction(post)) {
@@ -211,15 +211,27 @@ angularService('$xhr', function($browser){
if (isString(response) && /^\s*[\[\{]/.exec(response) && /[\}\]]\s*$/.exec(response)) {
response = fromJson(response);
}
- callback(code, response);
+ if (code == 200) {
+ callback(code, response);
+ } else {
+ $error(
+ {method: method, url:url, data:post, callback:callback},
+ {status: code, body:response});
+ }
} finally {
self.$eval();
}
});
};
-}, {inject:['$browser']});
+}, {inject:['$browser', '$xhr.error']});
+
+angularService('$xhr.error', function($log){
+ return function(request, response){
+ $log.error(response);
+ };
+}, {inject:['$log']});
-angularService('$xhr.bulk', function($xhr){
+angularService('$xhr.bulk', function($xhr, $error){
var requests = [],
callbacks = [],
scope = this;
@@ -254,7 +266,13 @@ angularService('$xhr.bulk', function($xhr){
$xhr('POST', url, {requests:currentRequests}, function(code, response){
foreach(response, function(response, i){
try {
- (currentCallbacks[i] || noop)(response.status, response.response);
+ if (response.status == 200) {
+ (currentCallbacks[i] || noop)(response.status, response.response);
+ } else {
+ $error(
+ extend({}, currentRequests[i], {callback: currentCallbacks[i]}),
+ {status: response.status, body:response.response});
+ }
} catch(e) {
scope.$log.error(e);
}
@@ -267,7 +285,7 @@ angularService('$xhr.bulk', function($xhr){
};
this.$onEval(PRIORITY_LAST, bulkXHR.flush);
return bulkXHR;
-}, {inject:['$xhr']});
+}, {inject:['$xhr', '$xhr.error']});
angularService('$xhr.cache', function($xhr){
var inflight = {}, self = this;;