【发布时间】:2021-10-05 02:32:00
【问题描述】:
当我尝试将错误发送到简单的 node.js Express 服务器的 Sentry 时,我没有在 Sentry 本身中记录任何错误。没有设置入站过滤器,它不会报告过去 30 天内过滤的任何内容。允许的域设置为*,我认为这是默认设置。我使用的代码或多或少与the documentation 中的示例代码相同。但是,当调用端点时,Sentry 中不会出现任何内容,并且调试行即使发送也不会显示任何有关错误的信息。
如何让 Sentry 正确捕获错误?
这是 test.js
'use strict';
const express = require('express');
const Sentry = require('@sentry/node');
const app = express();
// TODO dotenv setting
const SENTRY_NODE_DSN = process.env.SENTRY_NODE_DSN || 'the ingest url from settings > client keys';
console.log(`Started logging errors at sentry on DSN ${SENTRY_NODE_DSN}`);
// Sentry
Sentry.init({
dsn: SENTRY_NODE_DSN,
debug: true,
});
// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());
const port = process.argv[2];
if (port === undefined) {
console.log(`Please specify a port as first argument`);
process.exit(0);
}
app.get('/test', () => {
throw new Error('test');
});
// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler({
shouldHandleError(error) {
return true;
}
}));
app.listen(port);
我们以node ./test.js 3000开头
然后我们从另一个窗口执行wget -- localhost:3000/test,它会给出以下输出。
--2021-07-29 14:03:01-- http://localhost:3000/test
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:3000... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2021-07-29 14:03:01 ERROR 500: Internal Server Error.
快递服务器的输出是这样的:
$ node ./test.js 3000
Started logging errors at sentry on DSN same url as in the code above
Sentry Logger [Log]: Integration installed: InboundFilters
Sentry Logger [Log]: Integration installed: FunctionToString
Sentry Logger [Log]: Integration installed: Console
Sentry Logger [Log]: Integration installed: Http
Sentry Logger [Log]: Integration installed: OnUncaughtException
Sentry Logger [Log]: Integration installed: OnUnhandledRejection
Sentry Logger [Log]: Integration installed: LinkedErrors
Error: test
at /var/www/vhosts/myproject/test.js:28:8
at Layer.handle [as handle_request] (/var/www/vhosts/myproject/node_modules/express/lib/router/layer.js:95:5)
at next (/var/www/vhosts/myproject/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/www/vhosts/myproject/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/www/vhosts/myproject/node_modules/express/lib/router/layer.js:95:5)
at /var/www/vhosts/myproject/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/var/www/vhosts/myproject/node_modules/express/lib/router/index.js:335:12)
at next (/var/www/vhosts/myproject/node_modules/express/lib/router/index.js:275:10)
at Domain.<anonymous> (/var/www/vhosts/myproject/node_modules/@sentry/node/dist/handlers.js:321:13)
at Domain.run (domain.js:370:15)
【问题讨论】: