You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron应用集成Sentry:By in-app错误及源码未找到问题求助

解决Electron + Sentry集成中“Source code was not found”及无in_app帧问题

你的问题核心在于:当axios返回401这类预期错误时,错误的栈追踪几乎全是第三方库(axios)或Electron系统层的代码,没有包含你的业务代码帧。Sentry无法匹配到上传的Source Map,因此显示「Source code was not found」,同时提示「No in_app frames」和「Non contributing variant: exception of system take precedence」警告。

以下是具体修复建议:

  • 配置in_app代码范围
    在Sentry初始化时,明确标记哪些路径属于你的业务代码(inAppInclude),哪些是第三方/系统代码(inAppExclude),让Sentry能识别出有效业务帧:

    Sentry.init({
      // 其他原有配置
      inAppInclude: ['src/', 'app/'], // 替换成你的业务代码目录
      inAppExclude: ['node_modules/', 'electron/', 'Electron Framework/'] // 排除第三方及系统路径
    });
    

    注意Electron主进程和渲染进程的代码路径可能有差异,需要根据实际项目结构调整。

  • 过滤预期错误,避免无效上报
    既然401是预期错误,没必要上报给Sentry,直接在axios拦截器或Sentry配置中过滤:
    方法1:axios拦截器处理

    axios.interceptors.response.use(
      res => res,
      err => {
        if (err.response?.status === 401) {
          // 执行你的401逻辑(如跳转登录)
          return Promise.reject(err); // 若不想上报给Sentry,可直接返回空或处理后不reject
        }
        // 非预期错误才上报
        Sentry.captureException(err);
        return Promise.reject(err);
      }
    );
    

    方法2:Sentry初始化时配置忽略规则

    Sentry.init({
      ignoreErrors: [
        /Request failed with status code 401/ // 匹配401错误信息
      ]
    });
    
  • 校验Source Map上传的关键配置
    虽然你确认过配置,仍可再检查以下几点:

    • 上传时指定正确的--url-prefix,匹配Electron资源路径(比如渲染进程常用app://./dist):
      sentry-cli sourcemaps upload --url-prefix "app://./dist" ./dist
      
    • 确保Source Map上传时使用的release名称和应用上报时一致(你的release是desktop@3.29.29):
      sentry-cli releases new desktop@3.29.29
      sentry-cli releases files desktop@3.29.29 upload-sourcemaps ./dist
      
    • 检查编译后的JS文件末尾是否包含//# sourceMappingURL=xxx.js.map注释,确保Source Map关联正确。
  • 手动标记in_app帧(可选)
    若必须上报这类401错误,可在捕获时手动标记业务代码帧:

    Sentry.captureException(error, {
      contexts: {
        trace: {
          frames: error.stack?.split('\n').map(frame => ({
            filename: frame,
            in_app: frame.includes('src/') // 根据你的业务路径判断
          }))
        }
      }
    });
    

内容的提问来源于stack exchange,提问作者pierreaurelemartin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 21:00:58