$ npm i @sentry/node nest-raven
$ npm i @slack/client
Sentry 웹 → Project → Create a new Project → Node 선택 → 프로젝트 생성후 DSN 복사
Slack API 홈페이지 → Create New App → 이름 및 workspace 지정 → Feature/Incoming Webhooks 메뉴 → Activate Incoming Webhooks 를 On → Webhook URL .env 파일에 저장
main
에 sentry 연결import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as Sentry from '@sentry/node';
import { WebhookInterceptor } from './common/webhook.interceptor';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
Sentry.init({
dsn: process.env.SENTRY_DSN,
});
app.useGlobalInterceptors(new WebhookInterceptor());
await app.listen(3000);
}
bootstrap();
webhook.interceptor
import {CallHandler, ExecutionContext, Injectable, NestInterceptor} from '@nestjs/common';
import {catchError} from 'rxjs/operators';
import {IncomingWebhook} from '@slack/client';
import slackConfig from '../config/slack.config';
import {of} from 'rxjs';
import * as Sentry from '@sentry/minimal';
@Injectable()
export class SentryInterceptor implements NestInterceptor {
intercept(_: ExecutionContext, next: CallHandler) {
return next.handle().pipe(
catchError(error => {
Sentry.captureException(error);
const webhook = new IncomingWebhook(SentryConfig.webhook);
webhook.send({
attachments: [
{
color: 'danger',
text: '🚨ah-ha-api-server 버그 발생🚨',
fields: [
{
title: `Request Message: ${error.message}`,
value: error.stack,
short: false,
},
],
ts: Math.floor(new Date().getTime() / 1000).toString(),
},
],
});
return of(error);
}),
);
}
}
Sentry.captureException(error);
만 있으면 된다.main
에 sentry 연결import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as Sentry from '@sentry/node';
import { SentryInterceptor } from './common/sentry.interceptor';
import { WebhookInterceptor } from './common/webhook.interceptor';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
Sentry.init({
dsn: process.env.SENTRY_DSN,
});
app.useGlobalInterceptors(new SentryInterceptor());
await app.listen(3000);
}
bootstrap();
sentry.interceptor
import {
ExecutionContext,
Injectable,
NestInterceptor,
CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import * as Sentry from '@sentry/minimal';
@Injectable()
export class SentryInterceptor implements NestInterceptor {
intercept(_: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
catchError((error) => {
Sentry.captureException(error);
return null;
}),
);
}
}
🍪 https://github.com/hocaron/nestJS-study/tree/main/nest-sentry
class SentryInterceptor 에서 return이 Observable 이라
return of(null);
수정해야 할 것 같습니다.