Sentry 捕获不到 Vue 3 项目中的异步错误怎么办?

令狐晶晶 阅读 27

我在 Vue 3 项目里接入了 Sentry,同步错误能正常上报,但像 axios 请求失败这类异步错误完全捕获不到。已经按文档加了 Sentry.initapp.use(Sentry.createTracingDirective()),是不是漏了什么配置?

比如下面这个请求错误就从来没在 Sentry 后台出现过:

axios.get('/api/user')
  .catch(err => {
    console.error('请求出错', err); // 这行能打出来
    throw err; // 但 Sentry 没收到
  });
我来解答 赞 3 收藏
二维码
手机扫码查看
1 条解答
IT人新杰
你这个问题很简单,axios 的 Promise 错误不会自动上报 Sentry,需要手动捕获或者用拦截器。

最直接的方案是在 axios 拦截器里统一处理:

import axios from 'axios';
import * as Sentry from '@sentry/vue';

axios.interceptors.response.use(
response => response,
error => {
Sentry.captureException(error);
return Promise.reject(error);
}
);


或者在每个 catch 里直接调 Sentry.captureException(err),你代码里 throw err 是没用的,Sentry 不会自动捕获。
点赞
2026-03-18 10:21