-
-
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(sentry-plugin): Use ErrorHandlerStrategy for better error coverage
Using the new ErrorHandlerStrategy allows us to collect errors from both the server and the worker. It also prevents the default exception filter from being overridden.
- Loading branch information
1 parent
066e524
commit 82ddf94
Showing
3 changed files
with
68 additions
and
28 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/sentry-plugin/src/sentry-error-handler-strategy.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ArgumentsHost, ExecutionContext } from '@nestjs/common'; | ||
import { GqlContextType, GqlExecutionContext } from '@nestjs/graphql'; | ||
import { setContext } from '@sentry/node'; | ||
import { ErrorHandlerStrategy, I18nError, Injector, Job, LogLevel } from '@vendure/core'; | ||
|
||
import { SentryService } from './sentry.service'; | ||
|
||
export class SentryErrorHandlerStrategy implements ErrorHandlerStrategy { | ||
private sentryService: SentryService; | ||
|
||
init(injector: Injector) { | ||
this.sentryService = injector.get(SentryService); | ||
} | ||
|
||
handleServerError(exception: Error, { host }: { host: ArgumentsHost }) { | ||
// We only care about errors which have at least a Warn log level | ||
const shouldLogError = exception instanceof I18nError ? exception.logLevel <= LogLevel.Warn : true; | ||
if (shouldLogError) { | ||
if (host?.getType<GqlContextType>() === 'graphql') { | ||
const gqlContext = GqlExecutionContext.create(host as ExecutionContext); | ||
const info = gqlContext.getInfo(); | ||
setContext('GraphQL Error Context', { | ||
fieldName: info.fieldName, | ||
path: info.path, | ||
}); | ||
} | ||
const variables = (exception as any).variables; | ||
if (variables) { | ||
setContext('GraphQL Error Variables', variables); | ||
} | ||
this.sentryService.captureException(exception); | ||
} | ||
} | ||
|
||
handleWorkerError(exception: Error, { job }: { job: Job }) { | ||
setContext('Worker Context', { | ||
queueName: job.queueName, | ||
jobId: job.id, | ||
}); | ||
this.sentryService.captureException(exception); | ||
} | ||
} |
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