使用 express 记录哨兵错误不会发送任何错误

时间:2021-07-29 12:20:00

标签: node.js express sentry

当我尝试为简单的 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)

1 个答案:

答案 0 :(得分:0)

就我而言,此代码是正确的,但问题是我在 VPN 上时在 wsl 中运行此脚本,并且请求已发送,但挂起。从实验上看,Sentry 在调试模式下尝试发送错误时似乎不会记录任何内容,因此您无法从中推断出任何内容。

要测试 Sentry 是否真的发送任何东西,请尝试更简单的版本

.env

如果脚本挂起(没有终止),这意味着对哨兵服务器的 http 请求没有完成。这可能是由于您这边的网络问题。

相关问题