Skip to content

Commit

Permalink
Merge pull request #216 from johanbook/unexpected-exception-logger
Browse files Browse the repository at this point in the history
Add logger for unexpected exceptions in comands and events
  • Loading branch information
johanbook authored Jul 26, 2023
2 parents 0be1130 + 6a09178 commit 3f8abbe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
18 changes: 9 additions & 9 deletions services/api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion services/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@fastify/static": "^6.10.0",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/cqrs": "^9.0.3",
"@nestjs/cqrs": "^10.0.3",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/platform-fastify": "^9.4.0",
"@nestjs/platform-socket.io": "^9.4.0",
Expand Down
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";

@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";

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

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

constructor(private readonly unhandledExceptionBus: UnhandledExceptionBus) {
this.unhandledExceptionBus.subscribe((exceptionInfo) => {
this.logger.error({
cause: exceptionInfo.cause,
error: exceptionInfo.exception,
msg: `Encountered unhandled exception ${exceptionInfo.exception.message}`,
});
});
}
}

0 comments on commit 3f8abbe

Please sign in to comment.