|
| 1 | +import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub'; |
| 2 | +import { Integration, SentryEvent, SentryEventHint } from '@sentry/types'; |
| 3 | +import { isError } from '@sentry/utils/is'; |
| 4 | +import { logger } from '../../../utils/logger'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Just an Error object with arbitrary attributes attached to it. |
| 8 | + */ |
| 9 | +interface ExtendedError extends Error { |
| 10 | + [key: string]: unknown; |
| 11 | +} |
| 12 | + |
| 13 | +/** Patch toString calls to return proper name for wrapped functions */ |
| 14 | +export class ExtraErrorData implements Integration { |
| 15 | + /** |
| 16 | + * @inheritDoc |
| 17 | + */ |
| 18 | + public name: string = ExtraErrorData.id; |
| 19 | + |
| 20 | + /** |
| 21 | + * @inheritDoc |
| 22 | + */ |
| 23 | + public static id: string = 'ExtraErrorData'; |
| 24 | + |
| 25 | + /** |
| 26 | + * @inheritDoc |
| 27 | + */ |
| 28 | + public setupOnce(): void { |
| 29 | + addGlobalEventProcessor(async (event: SentryEvent, hint?: SentryEventHint) => { |
| 30 | + const self = getCurrentHub().getIntegration(ExtraErrorData); |
| 31 | + |
| 32 | + if (!self || !hint || !hint.originalException) { |
| 33 | + return event; |
| 34 | + } |
| 35 | + |
| 36 | + return this.enhanceEventWithErrorData(event, hint.originalException); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Attaches extracted information from the Error object to extra field in the SentryEvent |
| 42 | + */ |
| 43 | + public enhanceEventWithErrorData(event: SentryEvent, error: Error): SentryEvent { |
| 44 | + const errorData = this.extractErrorData(error); |
| 45 | + |
| 46 | + if (errorData) { |
| 47 | + return { |
| 48 | + ...event, |
| 49 | + extra: { |
| 50 | + ...event.extra, |
| 51 | + ...errorData, |
| 52 | + }, |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + return event; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Extract extra information from the Error object |
| 61 | + */ |
| 62 | + private extractErrorData(error: ExtendedError): { [key: string]: unknown } | null { |
| 63 | + // We are trying to enhance already existing event, so no harm done if it won't succeed |
| 64 | + try { |
| 65 | + const nativeKeys = ['name', 'message', 'stack', 'line', 'column', 'fileName', 'lineNumber', 'columnNumber']; |
| 66 | + const name = error.name || error.constructor.name; |
| 67 | + const errorKeys = Object.keys(error).filter(key => nativeKeys.indexOf(key) === -1); |
| 68 | + |
| 69 | + if (errorKeys.length) { |
| 70 | + const extraErrorInfo: { [key: string]: unknown } = {}; |
| 71 | + for (const key of errorKeys) { |
| 72 | + let value = error[key]; |
| 73 | + if (isError(value)) { |
| 74 | + value = (value as Error).name || (value as Error).constructor.name; |
| 75 | + } |
| 76 | + extraErrorInfo[key] = value; |
| 77 | + } |
| 78 | + return { |
| 79 | + [name]: extraErrorInfo, |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + return null; |
| 84 | + } catch (oO) { |
| 85 | + logger.error('Unable to extract extra data from the Error object:', oO); |
| 86 | + return null; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments