|
| 1 | +import { captureException } from '@sentry/core'; |
| 2 | +import * as Sentry from '@sentry/node'; |
| 3 | +import { startSpan } from '@sentry/node'; |
| 4 | +import type { MonitorConfig } from '@sentry/types'; |
| 5 | +import { isExpectedError } from './helpers'; |
| 6 | + |
| 7 | +/** |
| 8 | + * A decorator wrapping the native nest Cron decorator, sending check-ins to Sentry. |
| 9 | + */ |
| 10 | +export const SentryCron = (monitorSlug: string, monitorConfig?: MonitorConfig): MethodDecorator => { |
| 11 | + return (target: unknown, propertyKey, descriptor: PropertyDescriptor) => { |
| 12 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 13 | + const originalMethod = descriptor.value as (...args: any[]) => Promise<any>; |
| 14 | + |
| 15 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 16 | + descriptor.value = function (...args: any[]) { |
| 17 | + return Sentry.withMonitor( |
| 18 | + monitorSlug, |
| 19 | + () => { |
| 20 | + return originalMethod.apply(this, args); |
| 21 | + }, |
| 22 | + monitorConfig, |
| 23 | + ); |
| 24 | + }; |
| 25 | + return descriptor; |
| 26 | + }; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * A decorator usable to wrap arbitrary functions with spans. |
| 31 | + */ |
| 32 | +export function SentryTraced(op: string = 'function') { |
| 33 | + return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) { |
| 34 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 35 | + const originalMethod = descriptor.value as (...args: any[]) => Promise<any> | any; // function can be sync or async |
| 36 | + |
| 37 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 38 | + descriptor.value = function (...args: any[]) { |
| 39 | + return startSpan( |
| 40 | + { |
| 41 | + op: op, |
| 42 | + name: propertyKey, |
| 43 | + }, |
| 44 | + () => { |
| 45 | + return originalMethod.apply(this, args); |
| 46 | + }, |
| 47 | + ); |
| 48 | + }; |
| 49 | + |
| 50 | + // preserve the original name on the decorated function |
| 51 | + Object.defineProperty(descriptor.value, 'name', { |
| 52 | + value: originalMethod.name, |
| 53 | + configurable: true, |
| 54 | + enumerable: true, |
| 55 | + writable: true, |
| 56 | + }); |
| 57 | + |
| 58 | + return descriptor; |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * A decorator to wrap user-defined exception filters and add Sentry error reporting. |
| 64 | + */ |
| 65 | +export function SentryExceptionCaptured() { |
| 66 | + return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) { |
| 67 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 68 | + const originalCatch = descriptor.value as (exception: unknown, host: unknown, ...args: any[]) => void; |
| 69 | + |
| 70 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 71 | + descriptor.value = function (exception: unknown, host: unknown, ...args: any[]) { |
| 72 | + if (isExpectedError(exception)) { |
| 73 | + return originalCatch.apply(this, [exception, host, ...args]); |
| 74 | + } |
| 75 | + |
| 76 | + captureException(exception); |
| 77 | + return originalCatch.apply(this, [exception, host, ...args]); |
| 78 | + }; |
| 79 | + |
| 80 | + return descriptor; |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * A decorator to wrap user-defined exception filters and add Sentry error reporting. |
| 86 | + */ |
| 87 | +export const WithSentry = SentryExceptionCaptured; |
0 commit comments