next.js - 在 Axios 拦截器下捕获 400 和 500 用于哨兵
问题描述
通过显示错误弹出窗口,使用 Axios 拦截器以通用方式处理 400 和 500。通常,当自定义 _error.js 页面由于 JS 错误而呈现时,会触发 Sentry 调用。如何在哨兵中记录 API 调用错误?
解决方案
您可以使用 axios 拦截器,也可以将其写入 axios 调用本身的 catch() 中。
拦截器
axios.interceptors.response.use(
(response: AxiosResponse) => response,
(error: AxiosError) => {
Sentry.captureException(error);
return Promise.reject(error);
},
);
Axios 调用
axios({
url,
method,
...
})
.then(async (response: AxiosResponse) => {
resolve(response.data);
})
.catch(async (error: AxiosError) => {
Sentry.captureException(error);
reject(error);
});
推荐阅读
- ios - react-native-audio-recorder-player:无法读取未定义的属性“startRecorder”
- php - 如果包含视频,如何均衡 card-img 大小
- vue.js - 在 Vue 应用程序中应该在哪里加载路由元数据?
- google-maps - MapController 和谷歌地图标记不起作用
- c# - TextMeshProUGUI 在播放模式下取消分配
- javascript - 不同屏幕分辨率的java脚本函数
- spring-boot - Spring Cloud Messaging Source 未向 Kafka 代理发送消息
- media-queries - 当我调整大小时,文本之间有间隙?
- c++ - 如何将 QueryThreadCycleTime() 转换为秒?
- reactjs - 哪个是 useEffect 获取多个数据的最佳用途?