sentry.filter.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
  2. import { Catch, ExecutionContext } from '@nestjs/common';
  3. import { GqlContextType, GqlExecutionContext } from '@nestjs/graphql';
  4. import { setContext } from '@sentry/node';
  5. import { ExceptionLoggerFilter, ForbiddenError, I18nError, LogLevel } from '@vendure/core';
  6. import { SentryService } from './sentry.service';
  7. export class SentryExceptionsFilter extends ExceptionLoggerFilter {
  8. constructor(private readonly sentryService: SentryService) {
  9. super();
  10. }
  11. catch(exception: Error, host: ArgumentsHost) {
  12. const shouldLogError = exception instanceof I18nError ? exception.logLevel <= LogLevel.Warn : true;
  13. if (shouldLogError) {
  14. if (host.getType<GqlContextType>() === 'graphql') {
  15. const gqlContext = GqlExecutionContext.create(host as ExecutionContext);
  16. const info = gqlContext.getInfo();
  17. setContext('GraphQL Error Context', {
  18. fieldName: info.fieldName,
  19. path: info.path,
  20. });
  21. }
  22. const variables = (exception as any).variables;
  23. if (variables) {
  24. setContext('GraphQL Error Variables', variables);
  25. }
  26. this.sentryService.captureException(exception);
  27. }
  28. return super.catch(exception, host);
  29. }
  30. }