组件版本说明:
Sentry:9.1.2 Koa:2.13.4
Node.js 依赖说明:
sentry/node:7.41.0
阅读 Sentry 官方文档: Koa | Sentry Documentation
示例代码:
import Koa from "koa";
import * as Sentry from "@sentry/node";
// or using CommonJS
// const Koa = require('koa');
// const Sentry = require('@sentry/node');
const app = new Koa();
Sentry.init({ dsn: "https://examplePublicKey@o0.ingest.sentry.io/0" });
app.on("error", (err, ctx) => {
Sentry.withScope(scope => {
scope.setSDKProcessingMetadata({ request: ctx.request });
Sentry.captureException(err);
});
});
app.listen(3000);
尝试之后,未能成功实现,在 Sentry 中未能查看到捕获的异常。
暂未能定位到异常错误,故查阅资料寻求解决方案。
解决方案 1:
阅读 Sentry 官网资料,Node.js | Sentry Documentation — Legacy Clients Node.js ,现已停止维护的 Node.js 依赖,raven 进行解决。
依赖如下:
"dependencies": {
"@koa/router": "^12.0.0",
"@sentry/node": "^7.40.0",
"@sentry/tracing": "^7.40.0",
"koa": "^2.14.1",
"raven": "^2.6.4"
}
实现示例:
/*
这里是成功向 sentry 汇报的案例,在2023.3.6 根据sentry 官网案例,并没有成功。而是使用旧版成功了!
// or using CommonJS
const Raven = require('raven')
const Koa = require('koa');
const Sentry = require('@sentry/node');
const app = new Koa();
Raven.config('http://xxxxxx').install()
app.on('error', (err) => {
Raven.captureException(err, (err, eventId) => {
console.log('Reported error ' + eventId)
})
})
app.use((ctx) => {
throw new Error('test')
})
app.listen(3000)*/
解决方案 2:
尝试更换 Sentry‘s Node SDK 版本解决问题!
在本文测试过程中,降低至"@sentry/node": "^7.0.0" 仍未实现。故向 6.x.x 版本进行尝试。最后,将 "@sentry/node": "^7.40.0" 降低至 "@sentry/node": "6.19.7" 得以实现。
解决问题!