gpt4 book ai didi

javascript - 如何从 $httpProvider 拦截器的 'reponse' 函数抛出错误

转载 作者:行者123 更新时间:2023-12-01 03:42:11 24 4
gpt4 key购买 nike

我正在用 Angular 制作一个http拦截器,我想从$httpProvider拦截器的response抛出错误。

根据文档:

response: interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new response object.

responseError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.

我想执行上面引用中的粗体部分,以便将状态为 200 的响应(我有一个条件)路由到 responseError ,我将在其中处理错误。不返回响应会引发以下错误:

Cannot read property 'data' of undefined

我不想返回响应,但想将其传递给下一个处理程序,即 responseError

我怎样才能做到这一点?我希望我说清楚了。谢谢。

更新(下面的代码):

app.config(['$httpProvider', function($httpProvider) {
interceptor.$inject = ['$q', '$rootScope'];
$httpProvider.interceptors.push(interceptor);

function interceptor($q, $rootScope) {
return {
response: response,
responseError: responseError
};

function response(response) {
if (response.data.rs == "F") {
// I want to route it responseError -->
} else {
return response;
}
}

function responseError(response) {
// I want to handle that error here
}
}

}]);

最佳答案

用途:

return $q.reject(response);

还要确保您返回:(阅读相关内容 here )

返回响应|| $q.when(响应);

而不是:

return response;

关于javascript - 如何从 $httpProvider 拦截器的 'reponse' 函数抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43799161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com