Skip to content

Commit

Permalink
chore(api): add unhandled exception logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Book authored and Johan Book committed Jul 26, 2023
1 parent 671eead commit 6a09178
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion services/api/src/core/logging/logging.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { CqrsModule } from "@nestjs/cqrs";

import { CommandLogger } from "./command.logger";
import { EventLogger } from "./event.logger";
import { UnhandledExceptionLogger } from "./unhandled-exception.logger";

Check warning on line 6 in services/api/src/core/logging/logging.module.ts

View check run for this annotation

Codecov / codecov/patch

services/api/src/core/logging/logging.module.ts#L6

Added line #L6 was not covered by tests

@Module({
exports: [],
imports: [CqrsModule],
controllers: [],
providers: [CommandLogger, EventLogger],
providers: [CommandLogger, EventLogger, UnhandledExceptionLogger],
})
export class LoggingModule {}
19 changes: 19 additions & 0 deletions services/api/src/core/logging/unhandled-exception.logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable } from "@nestjs/common";
import { UnhandledExceptionBus } from "@nestjs/cqrs";

Check warning on line 2 in services/api/src/core/logging/unhandled-exception.logger.ts

View check run for this annotation

Codecov / codecov/patch

services/api/src/core/logging/unhandled-exception.logger.ts#L1-L2

Added lines #L1 - L2 were not covered by tests

import { Logger } from "./domain/services/logger.service";

Check warning on line 4 in services/api/src/core/logging/unhandled-exception.logger.ts

View check run for this annotation

Codecov / codecov/patch

services/api/src/core/logging/unhandled-exception.logger.ts#L4

Added line #L4 was not covered by tests

@Injectable()
export class UnhandledExceptionLogger {
private logger = new Logger(UnhandledExceptionLogger.name);

Check warning on line 8 in services/api/src/core/logging/unhandled-exception.logger.ts

View check run for this annotation

Codecov / codecov/patch

services/api/src/core/logging/unhandled-exception.logger.ts#L7-L8

Added lines #L7 - L8 were not covered by tests

constructor(private readonly unhandledExceptionBus: UnhandledExceptionBus) {
this.unhandledExceptionBus.subscribe((exceptionInfo) => {
this.logger.error({

Check warning on line 12 in services/api/src/core/logging/unhandled-exception.logger.ts

View check run for this annotation

Codecov / codecov/patch

services/api/src/core/logging/unhandled-exception.logger.ts#L10-L12

Added lines #L10 - L12 were not covered by tests
cause: exceptionInfo.cause,
error: exceptionInfo.exception,
msg: `Encountered unhandled exception ${exceptionInfo.exception.message}`,
});
});
}
}

0 comments on commit 6a09178

Please sign in to comment.