Rob*_*ion 14 angularjs angular-http angular-http-interceptors
有时,200 ok
即使出现错误,我正在使用的API也会返回.响应JSON对象将类似于:
{
error: true
}
Run Code Online (Sandbox Code Playgroud)
我已经构建了一个$ http response
拦截器,只是检查这个错误并拒绝它.我希望它然后跳进我的responseError
功能:
$httpProvider.interceptors.push(function($q) {
return {
response: function (response) {
if (response.data.error) {
// There has been an error, reject this response
return $q.reject(response);
}
return response;
},
responseError: function(rejection) {
// Display an error message to the user
...
return $q.reject(rejection);
}
}
});
Run Code Online (Sandbox Code Playgroud)
问题是,即使在拒绝响应之后,我的responseError
函数也没有被调用.它被调用500
错误等,所以我知道它正在工作.我希望拒绝做同样的事情.
来自文档:
responseError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.
Run Code Online (Sandbox Code Playgroud)
关于什么缺失的任何想法?
看起来这是不可能的.要减少重复代码,只需单独声明错误处理函数并在response和responseError函数中重用它.
$httpProvider.interceptors.push(function($q) {
var handleError = function (rejection) { ... }
return {
response: function (response) {
if (response.data.error) {
return handleError(response);
}
return response;
},
responseError: handleError
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6334 次 |
最近记录: |