-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
90 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...e2e-tests/test-applications/nestjs-with-submodules-decorator/src/example-global.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { captureException } from '@sentry/core'; | ||
import * as Sentry from '@sentry/node'; | ||
import { startSpan } from '@sentry/node'; | ||
import type { MonitorConfig } from '@sentry/types'; | ||
import { isExpectedError } from './helpers'; | ||
|
||
/** | ||
* A decorator wrapping the native nest Cron decorator, sending check-ins to Sentry. | ||
*/ | ||
export const SentryCron = (monitorSlug: string, monitorConfig?: MonitorConfig): MethodDecorator => { | ||
return (target: unknown, propertyKey, descriptor: PropertyDescriptor) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const originalMethod = descriptor.value as (...args: any[]) => Promise<any>; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
descriptor.value = function (...args: any[]) { | ||
return Sentry.withMonitor( | ||
monitorSlug, | ||
() => { | ||
return originalMethod.apply(this, args); | ||
}, | ||
monitorConfig, | ||
); | ||
}; | ||
return descriptor; | ||
}; | ||
}; | ||
|
||
/** | ||
* A decorator usable to wrap arbitrary functions with spans. | ||
*/ | ||
export function SentryTraced(op: string = 'function') { | ||
return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const originalMethod = descriptor.value as (...args: any[]) => Promise<any> | any; // function can be sync or async | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
descriptor.value = function (...args: any[]) { | ||
return startSpan( | ||
{ | ||
op: op, | ||
name: propertyKey, | ||
}, | ||
() => { | ||
return originalMethod.apply(this, args); | ||
}, | ||
); | ||
}; | ||
|
||
// preserve the original name on the decorated function | ||
Object.defineProperty(descriptor.value, 'name', { | ||
value: originalMethod.name, | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
}); | ||
|
||
return descriptor; | ||
}; | ||
} | ||
|
||
/** | ||
* A decorator to wrap user-defined exception filters and add Sentry error reporting. | ||
*/ | ||
export function SentryExceptionCaptured() { | ||
return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const originalCatch = descriptor.value as (exception: unknown, host: unknown, ...args: any[]) => void; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
descriptor.value = function (exception: unknown, host: unknown, ...args: any[]) { | ||
if (isExpectedError(exception)) { | ||
return originalCatch.apply(this, [exception, host, ...args]); | ||
} | ||
|
||
captureException(exception); | ||
return originalCatch.apply(this, [exception, host, ...args]); | ||
}; | ||
|
||
return descriptor; | ||
}; | ||
} | ||
|
||
/** | ||
* A decorator to wrap user-defined exception filters and add Sentry error reporting. | ||
*/ | ||
export const WithSentry = SentryExceptionCaptured; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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