Skip to content

Commit

Permalink
fix(api-observability): sentry exception filter logger not provided c…
Browse files Browse the repository at this point in the history
…orrectly
  • Loading branch information
timonmasberg committed Mar 13, 2024
1 parent 0544f21 commit 9745db0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { Logger } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as Sentry from '@sentry/node';

import { PresentableException } from '@kordis/api/shared';
Expand All @@ -11,15 +12,22 @@ describe('SentryExceptionsFilter', () => {
let addBreadcrumbMock: jest.Mock;
let captureExceptionMock: jest.Mock;
let logger: DeepMocked<Logger>;

beforeEach(async () => {
addBreadcrumbMock = jest.fn();
captureExceptionMock = jest.fn();
logger = createMock<Logger>();

(Sentry.addBreadcrumb as jest.Mock) = addBreadcrumbMock;
(Sentry.captureException as jest.Mock) = captureExceptionMock;
logger = createMock<Logger>();

const moduleRef = await Test.createTestingModule({
providers: [SentryExceptionsFilter],
}).compile();

sentryExceptionsFilter = new SentryExceptionsFilter(logger);
moduleRef.useLogger(logger);
sentryExceptionsFilter = moduleRef.get<SentryExceptionsFilter>(
SentryExceptionsFilter,
);
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { KordisLogger } from '../services/kordis-logger.interface';

@Catch()
export class SentryExceptionsFilter implements ExceptionFilter {
readonly logger: KordisLogger;

constructor(_logger: Logger) {
this.logger = _logger;
}
private readonly logger: KordisLogger = new Logger();

catch(exception: unknown): void {
if (exception instanceof PresentableException) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger, Module, OnModuleInit } from '@nestjs/common';
import { Module, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { APP_FILTER, APP_INTERCEPTOR, ModulesContainer } from '@nestjs/core';
import { init as initSentry } from '@sentry/node';
Expand All @@ -7,7 +7,6 @@ import { SentryExceptionsFilter } from './filters/sentry-exceptions.filter';
import { SentryOTelUserContextInterceptor } from './interceptors/sentry-otel-user-context.interceptor';
import oTelSDK from './oTelSdk';
import { KORDIS_LOGGER_SERVICE } from './services/kordis-logger-service.interface';
import { KordisLogger } from './services/kordis-logger.interface';
import { SentryLogger } from './services/sentry-logger.service';
import { wrapProvidersWithTracingSpans } from './trace-wrapper';

Expand All @@ -30,10 +29,6 @@ import { wrapProvidersWithTracingSpans } from './trace-wrapper';
exports: [KORDIS_LOGGER_SERVICE],
})
export class SentryObservabilityModule implements OnModuleInit {
private readonly logger: KordisLogger = new Logger(
SentryObservabilityModule.name,
);

constructor(
private readonly config: ConfigService,
private readonly modulesContainer: ModulesContainer,
Expand All @@ -49,7 +44,5 @@ export class SentryObservabilityModule implements OnModuleInit {
});
wrapProvidersWithTracingSpans(this.modulesContainer);
oTelSDK.start();

this.logger.log('Sentry initialized');
}
}

0 comments on commit 9745db0

Please sign in to comment.