feat(api): setup optional sentry error reporting

This commit is contained in:
Julian Tölle 2020-11-17 22:26:03 +01:00
parent d620c03219
commit 56db4cd2e1
5 changed files with 2201 additions and 71 deletions

2234
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -35,10 +35,12 @@
"@nestjs/swagger": "4.7.3", "@nestjs/swagger": "4.7.3",
"@nestjs/terminus": "7.0.1", "@nestjs/terminus": "7.0.1",
"@nestjs/typeorm": "7.1.4", "@nestjs/typeorm": "7.1.4",
"@sentry/node": "^5.27.4",
"class-transformer": "0.3.1", "class-transformer": "0.3.1",
"class-validator": "0.12.2", "class-validator": "0.12.2",
"cookie-parser": "1.4.5", "cookie-parser": "1.4.5",
"date-fns": "2.16.1", "date-fns": "2.16.1",
"nest-raven": "^7.0.0",
"nestjs-typeorm-paginate": "2.2.1", "nestjs-typeorm-paginate": "2.2.1",
"passport": "0.4.1", "passport": "0.4.1",
"passport-jwt": "4.0.0", "passport-jwt": "4.0.0",

View file

@ -1,6 +1,7 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { ScheduleModule } from "@nestjs/schedule"; import { ScheduleModule } from "@nestjs/schedule";
import { ServeStaticModule } from "@nestjs/serve-static"; import { ServeStaticModule } from "@nestjs/serve-static";
import { RavenModule } from "nest-raven";
import { join } from "path"; import { join } from "path";
import { AuthModule } from "./auth/auth.module"; import { AuthModule } from "./auth/auth.module";
import { ConfigModule } from "./config/config.module"; import { ConfigModule } from "./config/config.module";
@ -23,6 +24,7 @@ import { UsersModule } from "./users/users.module";
rootPath: join(__dirname, "..", "static"), rootPath: join(__dirname, "..", "static"),
exclude: ["/api*"], exclude: ["/api*"],
}), }),
RavenModule,
AuthModule, AuthModule,
UsersModule, UsersModule,
SourcesModule, SourcesModule,

View file

@ -35,6 +35,13 @@ import { ConfigModule as NestConfigModule } from "@nestjs/config";
DB_USERNAME: Joi.string().required(), DB_USERNAME: Joi.string().required(),
DB_PASSWORD: Joi.string().required(), DB_PASSWORD: Joi.string().required(),
DB_DATABASE: Joi.string().required(), DB_DATABASE: Joi.string().required(),
// Sentry (Optional)
SENTRY_ENABLED: Joi.boolean().default(false),
SENTRY_DSN: Joi.string().when("SENTRY_ENABLED", {
is: Joi.valid(true),
then: Joi.required(),
}),
}), }),
}), }),
], ],

View file

@ -1,8 +1,26 @@
import { ValidationPipe } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core"; import { NestFactory } from "@nestjs/core";
import { NestExpressApplication } from "@nestjs/platform-express"; import { NestExpressApplication } from "@nestjs/platform-express";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger"; import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { AppModule } from "./app.module"; import { AppModule } from "./app.module";
import { ValidationPipe } from "@nestjs/common"; import * as Sentry from "@sentry/node";
import { RavenInterceptor } from "nest-raven";
function setupSentry(
app: NestExpressApplication,
configService: ConfigService
) {
Sentry.init({
dsn: configService.get<string>("SENTRY_DSN"),
integrations: [
new Sentry.Integrations.OnUncaughtException(),
new Sentry.Integrations.OnUnhandledRejection(),
],
});
app.useGlobalInterceptors(new RavenInterceptor());
}
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule); const app = await NestFactory.create<NestExpressApplication>(AppModule);
@ -14,6 +32,12 @@ async function bootstrap() {
); );
app.enableShutdownHooks(); app.enableShutdownHooks();
const configService = app.get<ConfigService>(ConfigService);
if (configService.get<boolean>("SENTRY_ENABLED")) {
setupSentry(app, configService);
}
// Setup API Docs // Setup API Docs
const options = new DocumentBuilder() const options = new DocumentBuilder()
.setTitle("Listory") .setTitle("Listory")
@ -29,4 +53,5 @@ async function bootstrap() {
await app.listen(3000); await app.listen(3000);
} }
bootstrap(); bootstrap();