Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api-observability): log unhandled exceptions caught in exceptions… #653

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { Logger } from '@nestjs/common';
import * as Sentry from '@sentry/node';

import { PresentableException } from '@kordis/api/shared';

import { SentryExceptionsFilter } from './sentry-exceptions.filter';

describe('ExceptionsFilter', () => {
describe('SentryExceptionsFilter', () => {
let sentryExceptionsFilter: SentryExceptionsFilter;
let addBreadcrumbMock: jest.Mock;
let captureExceptionMock: jest.Mock;

beforeEach(() => {
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;

sentryExceptionsFilter = new SentryExceptionsFilter();
sentryExceptionsFilter = new SentryExceptionsFilter(logger);
});

afterEach(() => {
Expand All @@ -36,15 +39,11 @@ describe('ExceptionsFilter', () => {

sentryExceptionsFilter.catch(presentableException);

expect(addBreadcrumbMock).toHaveBeenCalledTimes(1);
expect(addBreadcrumbMock).toHaveBeenCalledWith({
level: 'error',
message: 'message',
data: {
name: 'Error',
code: 'code',
stack: expect.any(String),
},
expect(logger.log).toHaveBeenCalledTimes(1);
expect(logger.log).toHaveBeenCalledWith('message', {
name: 'Error',
code: 'code',
stack: expect.any(String),
});
expect(captureExceptionMock).not.toHaveBeenCalled();
});
Expand All @@ -58,6 +57,14 @@ describe('ExceptionsFilter', () => {
expect(captureExceptionMock).toHaveBeenCalledWith(exception, {
level: 'error',
});
expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenCalledWith(
'Caught unhandled exception that was not presentable',
undefined,
{
exception,
},
);
expect(addBreadcrumbMock).not.toHaveBeenCalled();
});
});
30 changes: 21 additions & 9 deletions libs/api/observability/src/lib/filters/sentry-exceptions.filter.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { Catch, ExceptionFilter } from '@nestjs/common';
import { Catch, ExceptionFilter, Logger } from '@nestjs/common';
import * as Sentry from '@sentry/node';

import { PresentableException } from '@kordis/api/shared';

import { KordisLogger } from '../services/kordis-logger.interface';

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

constructor(_logger: Logger) {
this.logger = _logger;
}

catch(exception: unknown): void {
if (exception instanceof PresentableException) {
// if this is a presentable error, such as a validation error, we don't want to log it as an error but rather as an information to have the context for possible future debugging
Sentry.addBreadcrumb({
level: 'error',
message: exception.message,
data: {
code: exception.code,
name: exception.name,
stack: exception.stack,
},
this.logger.log(exception.message, {
code: exception.code,
name: exception.name,
stack: exception.stack,
});
} else {
this.logger.error(
'Caught unhandled exception that was not presentable',
undefined,
{
exception,
},
);

Sentry.captureException(exception, {
level: 'error',
});
Expand Down