在axios文档中,他们使用了这段代码
try {
const { data } = await axios.get('/user?ID=12345');
user = data.userDetails;
} catch (error) {
if (axios.isAxiosError(error)) {
handleAxiosError(error);
} else {
handleUnexpectedError(error);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用 axios,在错误中我还可以看到isAxiosError: true. 但所有错误都具有相同的属性。
当然,如果我这样做的话。isAxiosError那么就根本没有田地了。
axiosInstance.interceptors.response.use(
(response) => response,
(err) => {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
return Promise.reject(new Error("error");
}
);
Run Code Online (Sandbox Code Playgroud)
我的问题是:有没有什么情况isAxiosError: false
所有使用 axios 成功执行的调用,当失败时,将被识别为 axios 错误。
但在您的示例中,如果请求正确,但返回值为data,undefined您将收到cannot read property userDetails of undefined,这不会是 axios 错误。
如果仅包装 axios 请求,则可以假设您将收到 axios 错误。但如果包含其他逻辑,该逻辑将能够抛出与 axios 无关的错误。
因此它可用于将 axios 生成的错误与可能在同一 try/catch 中抛出的其他错误分开。
编辑:
想一想,您可能想知道是否isAxiosError定义了ever但设置为false?
我猜不会。由于该属性已被定义为可读取/查找,boolean因此 a 最有意义。
| 归档时间: |
|
| 查看次数: |
6949 次 |
| 最近记录: |