最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sentry不会捕获nodejs环境中的所有错误

运维笔记admin9浏览0评论

Sentry不会捕获nodejs环境中的所有错误

Sentry不会捕获nodejs环境中的所有错误

我正在尝试部署一个哨兵安装来捕获我的应用程序中的错误,不知怎的,我真的不明白该怎么做。

我有这个示例应用程序:

const express = require('express');
const app = express();
var Raven = require('raven');
Raven.config('http://[email protected]/7').install();
app.use(Raven.requestHandler());

app.get('/', function mainHandler(req, res) {
        throw new Error('Broke!');
});
app.use(Raven.errorHandler());
app.use(function onError(err, req, res, next) {
    res.statusCode = 500;
    res.end(res.sentry + '\n');
});

const PORT = process.env.PORT || 443;

app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});

app.get('/OK', (req, res, next) => {
  res.send('route OK');
});

app.get('/KO', (req, res, next) => {
  res.send(blabla);
});

哨兵完全记录了/路线上的错误,但没有在/KO路线上。我想让它记录可能出现在节点控制台中的所有错误,而不使用throw error

我怎么做?

回答如下:

在所有路线之后放置app.use线,特别是onError处理程序。 Node的本机错误处理可能是在Sentry之前捕获它。

const express = require('express');
const app = express();
var Raven = require('raven');
Raven.config('http://[email protected]/7').install();
app.use(Raven.requestHandler());

app.get('/', function mainHandler(req, res) {
        throw new Error('Broke!');
});
const PORT = process.env.PORT || 443;

app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});

app.get('/OK', (req, res, next) => {
  res.send('route OK');
});

app.get('/KO', (req, res, next) => {
  res.send(blabla);
});

app.use(Raven.errorHandler());
app.use(function onError(err, req, res, next) {
    res.statusCode = 500;
    res.end(res.sentry + '\n');
});

披露:我在Sentry工作,但我没有维护我们的Node SDK。如果您想要更深入的答案,请在节点仓库中打开一个问题。

发布评论

评论列表(0)

  1. 暂无评论