[英]Angular $HTTP Error Handler
Below is an example of a pretty crude way i'm currently handling aa HTTP call which is taking too long. 以下是我目前正在处理耗时太长的HTTP调用的一种粗略方式的示例。
If it takes more than 3 seconds, re-fire, and if this fails 4 times than exit. 如果需要3秒钟以上,请重新触发,如果失败4次,则退出。
While it works, I don't wish to create every HTTP request like this as it is too much code for one call. 虽然有效,但我不希望像这样创建每个HTTP请求,因为对于一个调用来说这是太多代码。
Is there a way of applying this globally? 有没有一种在全球范围内应用的方法?
I'm not sure if an interceptor is suitable for this case, as I need the error handle to kick in after the HTTP call takes too long, rather than when it receives a bad response from the server. 我不确定拦截器是否适合这种情况,因为我需要在HTTP调用花费太长时间之后而不是在收到来自服务器的错误响应时启动错误句柄。
I would also need this to apply to requests for partial views. 我还需要将其应用于部分视图请求。
Code: 码:
var errorCount = 0;
function someFunction() {
var startTime = new Date().getTime();
var deferred = $q.defer();
var url = 'some url';
$http.get(url, {cache: true, timeout: 3000}).success(function(response) {
deferred.resolve(response);
}).error(function(result, status, header, config) {
var respTime = new Date().getTime() - startTime;
if( respTime >= config.timeout ){
errorCount +=1;
if ( errorCount > 3) {
errorCount = 0;
deferred.reject(result);
} else {
getCountries();
};
} else{
deferred.reject(error);
};
});
return deferred.promise;
};
Ok so this is what I ended up doing. 好的,这就是我最终要做的。
Probably not the best way but maybe someone will find it useful too! 可能不是最好的方法,但也许有人也会发现它有用!
So using an interceptor I configure all outgoing HTTP requests to abort after 3 seconds, and configure any responses which have a status of 'abort' (which is a status of '-1' I think) to retry 3 times. 因此,使用拦截器,我将所有传出的HTTP请求配置为在3秒后中止,并配置状态为“中止”(我认为状态为“ -1”)的所有响应重试3次。
Code: 码:
(function() {
'use strict';
angular
.module('InterceptModule', []);
angular
.module('InterceptModule')
.factory('myInterceptor', myInterceptor);
myInterceptor.$inject = ['$q', '$injector','$cookies']
function myInterceptor($q,$injector,$cookies) {
var errorCount = 0;
var service = {
responseError: responseError,
request : request
};
return service;
//////////
/* this function is where tell the request to re-fire if it
* has been aborted.
*
*/
function responseError(response) {
if ( response.status == -1 ) {
var deferred = $q.defer();
var firstResponse = response;
var url = response.config.url;
var $http = $injector.get('$http');
$http.get(url, {cache: true}).then(function(response) {
deferred.resolve(response);
}, function(result, status, header, config) {
if ( status == -1 ) {
errorCount += 1;
if ( errorCount > 3 ) {
errorCount = 0;
deferred.reject(result);
} else {
responseError(firstResponse);
}
};
deferred.reject(result);
});
return deferred.promise;
};/* if */
return $q.reject(response);
};
/**
* this function is where we configure our outgoing http requests
* to abort if they take longer than 3 seconds.
*
*/
function request(config) {
config.timeout = 3000;
return config;
};
}/* service */
})();
Inject our intercept module 注入我们的拦截模块
angular
.module('myApp', ['InterceptModule']);
Then push our interceptor in our config 然后在我们的配置中推送我们的拦截器
$httpProvider.interceptors.push('myInterceptor');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.