-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Introduce ErrorHandlerStrategy
This new strategy enables a unified, global way of responding to errors across the application, including server and worker.
- Loading branch information
1 parent
aeebbdd
commit 066e524
Showing
8 changed files
with
126 additions
and
4 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
12 changes: 9 additions & 3 deletions
12
packages/core/src/api/middleware/exception-logger.filter.ts
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { ArgumentsHost } from '@nestjs/common'; | ||
|
||
import { InjectableStrategy } from '../../common/index'; | ||
import { Job } from '../../job-queue/index'; | ||
|
||
/** | ||
* @description | ||
* This strategy defines logic for handling errors thrown during on both the server | ||
* and the worker. It can be used for additional logging & monitoring, or for sending error | ||
* reports to external services. | ||
* | ||
* :::info | ||
* | ||
* This is configured via the `systemOptions.errorHandlers` property of | ||
* your VendureConfig. | ||
* | ||
* ::: | ||
* | ||
* @example | ||
* ```ts | ||
* import { ArgumentsHost, ExecutionContext } from '\@nestjs/common'; | ||
* import { GqlContextType, GqlExecutionContext } from '\@nestjs/graphql'; | ||
* import { ErrorHandlerStrategy, I18nError, Injector, Job, LogLevel } from '\@vendure/core'; | ||
* | ||
* import { MonitoringService } from './monitoring.service'; | ||
* | ||
* export class CustomErrorHandlerStrategy implements ErrorHandlerStrategy { | ||
* private monitoringService: MonitoringService; | ||
* | ||
* init(injector: Injector) { | ||
* this.monitoringService = injector.get(MonitoringService); | ||
* } | ||
* | ||
* handleServerError(error: Error, { host }: { host: ArgumentsHost }) { | ||
* const errorContext: any = {}; | ||
* if (host?.getType<GqlContextType>() === 'graphql') { | ||
* const gqlContext = GqlExecutionContext.create(host as ExecutionContext); | ||
* const info = gqlContext.getInfo(); | ||
* errorContext.graphQlInfo = { | ||
* fieldName: info.fieldName, | ||
* path: info.path, | ||
* }; | ||
* } | ||
* this.monitoringService.captureException(error, errorContext); | ||
* } | ||
* | ||
* handleWorkerError(error: Error, { job }: { job: Job }) { | ||
* const errorContext = { | ||
* queueName: job.queueName, | ||
* jobId: job.id, | ||
* }; | ||
* this.monitoringService.captureException(error, errorContext); | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @since 2.2.0 | ||
* @docsCategory Errors | ||
*/ | ||
export interface ErrorHandlerStrategy extends InjectableStrategy { | ||
/** | ||
* @description | ||
* This method will be invoked for any error thrown during the execution of the | ||
* server. | ||
*/ | ||
handleServerError(exception: Error, context: { host: ArgumentsHost }): void | Promise<void>; | ||
|
||
/** | ||
* @description | ||
* This method will be invoked for any error thrown during the execution of a | ||
* job on the worker. | ||
*/ | ||
handleWorkerError(exception: Error, context: { job: Job }): void | Promise<void>; | ||
} |
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