我需要在 axios 响应拦截器中对一些基于 HTTP 响应状态代码的错误进行通用的 HTTP 错误处理。 拦截器中处理的错误不应进一步传播到源处的源 HTTP 调用处理程序。
我有一个类似于下面的拦截器。这里,http
是一个使用 axios.create()
-
http.interceptors.response.use(
(response: AxiosResponse) => {
return response;
},
(err: AxiosError) => {
// Below error should not propogate further to the HTTP call source handlers. That is, it should not invoke success/error (then/catch) handlersof HTTP calls.
if (err.response?.status === 401) {
alert("Unauthorized!");
} else {
// Below error should propogate further to the HTTP call source handlers (working fine)
return Promise.reject(err);
}
}
);
我发现有一个 Cancel
方法可以根据帖子 here 在 axios 上使用。
但是 Cancel
方法似乎奇怪地在 axios 实例上可用。
如果我们在 axios 实例上使用拦截器,如何实现?