将Nestjs与哨兵集成

时间:2019-10-04 15:28:42

标签: javascript nestjs

我想将哨兵与nest.js + express集成在一起,但是我刚刚找到了乌鸦版本,但是已经过时了。 我遵循哨兵文档与express集成,但是不知道如何处理“所有控制器都应住在这里”部分。

const express = require('express');
const app = express();
const Sentry = require('@sentry/node');

Sentry.init({ dsn: 'https://5265e36cb9104baf9b3109bb5da9423e@sentry.io/1768434' });

// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());

**// All controllers should live here
app.get('/', function rootHandler(req, res) {
  res.end('Hello world!');
});**

// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());

// Optional fallthrough error handler
app.use(function onError(err, req, res, next) {
  // The error id is attached to `res.sentry` to be returned
  // and optionally displayed to the user for support.
  res.statusCode = 500;
  res.end(res.sentry + "\n");
});

app.listen(3000);

1 个答案:

答案 0 :(得分:0)

对于将哨兵与nestjs集成在一起,我们需要执行以下步骤:

  1. 在巢式乌鸦上安装npm
  2. 在main.ts
async function bootstrap() {
  Sentry.init({ dsn: 'https://5265e36cb9104baf9b3109bb5da9423e@sentry.io/1768434' });
  const app = await NestFactory.create(AppModule);
  // middlewares
  await app.listen(3000);
}
  1. 用于app.module.ts中的所有控制器
@Module({
  imports: [
    RavenModule,...
  ],
  controllers: [],
  providers: [{
    provide: APP_INTERCEPTOR,
    useValue: new RavenInterceptor({
      filters: [
        // Filter exceptions of type HttpException. Ignore those that
        // have status code of less than 500
        { type: HttpException, filter: (exception: HttpException) => 500 > exception.getStatus() },
      ],
    }),
  }],
})

问题正在这里https://github.com/getsentry/sentry-javascript/issues/2269进行跟踪,您可以在该示例中进行跟踪。