-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Capture server errors in NestJS (#53)
* Add test * Add NestJS exceptions filter
- Loading branch information
Showing
8 changed files
with
45 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,24 @@ | ||
import { ArgumentsHost, Catch, INestApplication } from "@nestjs/common"; | ||
import { BaseExceptionFilter } from "@nestjs/core"; | ||
import { Response } from "express"; | ||
|
||
import type { ApitallyConfig } from "../common/types.js"; | ||
import { useApitally as useApitallyExpress } from "../express/index.js"; | ||
export type { ApitallyConsumer } from "../common/types.js"; | ||
export { useApitally } from "../express/index.js"; | ||
|
||
export const useApitally = (app: INestApplication, config: ApitallyConfig) => { | ||
const httpAdapter = app.getHttpAdapter(); | ||
const expressInstance = httpAdapter.getInstance(); | ||
useApitallyExpress(expressInstance, config); | ||
app.useGlobalFilters(new AllExceptionsFilter(httpAdapter)); | ||
}; | ||
|
||
@Catch() | ||
class AllExceptionsFilter extends BaseExceptionFilter { | ||
catch(exception: unknown, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const res = ctx.getResponse<Response>(); | ||
res.locals.serverError = exception; | ||
super.catch(exception, host); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,6 @@ export class AppController { | |
|
||
@Get("/error") | ||
getError() { | ||
throw new Error("Error"); | ||
throw new Error("test"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters