使用express的前哨错误日志记录不会发送任何错误
提问开始:
当我试图将错误发送到一个简单的node.js Express服务器的前哨时,我没有得到任何错误记录在前哨本身。没有设置入站筛选器,并且它不会报告过去30天内筛选的任何内容。Allowed domains设置为*
,我认为这是默认设置。我使用的代码与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.
express服务器的输出如下所示:
$ 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)
回答开始:得票数 0 在我的例子中,这段代码是正确的,但问题是我是在VPN上的wsl中运行这个脚本,请求正在发送,但是挂起了。从实验上看,在调试模式下尝试发送错误时,Sentry似乎不会记录任何内容,因此您无法从中推断任何内容。
要测试Sentry是否真的发送任何内容,请尝试更简单的版本
const Sentry = require('@sentry/node');
const SENTRY_NODE_DSN = 'the ingest url';
Sentry.init({
dsn: SENTRY_NODE_DSN,
debug: true,
});
Sentry.captureException(new Error('test exception'));
如果脚本挂起(不终止),这意味着对哨兵服务器的http请求没有完成。这可能是由于你方的网络问题造成的。
总结以上是真正的电脑专家为你收集整理的使用express的前哨错误日志记录不会发送任何错误的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得真正的电脑专家网站内容还不错,欢迎将真正的电脑专家推荐给好友。