|
1 | | -import type { EventProcessor, Hub, Integration } from '@sentry/types'; |
2 | | -import { CONSOLE_LEVELS, fill, GLOBAL_OBJ, safeJoin, severityLevelFromString } from '@sentry/utils'; |
| 1 | +import type { Hub, Integration } from '@sentry/types'; |
| 2 | +import { |
| 3 | + addInstrumentationHandler, |
| 4 | + CONSOLE_LEVELS, |
| 5 | + GLOBAL_OBJ, |
| 6 | + safeJoin, |
| 7 | + severityLevelFromString, |
| 8 | +} from '@sentry/utils'; |
3 | 9 |
|
4 | 10 | /** Send Console API calls as Sentry Events */ |
5 | 11 | export class CaptureConsole implements Integration { |
@@ -29,51 +35,50 @@ export class CaptureConsole implements Integration { |
29 | 35 | /** |
30 | 36 | * @inheritDoc |
31 | 37 | */ |
32 | | - public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { |
| 38 | + public setupOnce(_: unknown, getCurrentHub: () => Hub): void { |
33 | 39 | if (!('console' in GLOBAL_OBJ)) { |
34 | 40 | return; |
35 | 41 | } |
36 | 42 |
|
37 | | - this._levels.forEach((level: string) => { |
38 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any |
39 | | - if (!(level in (GLOBAL_OBJ as any).console)) { |
| 43 | + const levels = this._levels; |
| 44 | + |
| 45 | + addInstrumentationHandler('console', ({ args, level }: { args: unknown[]; level: string }) => { |
| 46 | + if (!levels.includes(level)) { |
40 | 47 | return; |
41 | 48 | } |
42 | 49 |
|
43 | | - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access |
44 | | - fill((GLOBAL_OBJ as any).console, level, (originalConsoleMethod: () => any) => (...args: any[]): void => { |
45 | | - const hub = getCurrentHub(); |
46 | | - |
47 | | - if (hub.getIntegration(CaptureConsole)) { |
48 | | - hub.withScope(scope => { |
49 | | - scope.setLevel(severityLevelFromString(level)); |
50 | | - scope.setExtra('arguments', args); |
51 | | - scope.addEventProcessor(event => { |
52 | | - event.logger = 'console'; |
53 | | - return event; |
54 | | - }); |
| 50 | + const hub = getCurrentHub(); |
55 | 51 |
|
56 | | - let message = safeJoin(args, ' '); |
57 | | - const error = args.find(arg => arg instanceof Error); |
58 | | - if (level === 'assert') { |
59 | | - if (args[0] === false) { |
60 | | - message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`; |
61 | | - scope.setExtra('arguments', args.slice(1)); |
62 | | - hub.captureMessage(message); |
63 | | - } |
64 | | - } else if (level === 'error' && error) { |
65 | | - hub.captureException(error); |
66 | | - } else { |
67 | | - hub.captureMessage(message); |
68 | | - } |
69 | | - }); |
70 | | - } |
| 52 | + if (!hub.getIntegration(CaptureConsole)) { |
| 53 | + return; |
| 54 | + } |
71 | 55 |
|
72 | | - // this fails for some browsers. :( |
73 | | - if (originalConsoleMethod) { |
74 | | - originalConsoleMethod.apply(GLOBAL_OBJ.console, args); |
75 | | - } |
76 | | - }); |
| 56 | + consoleHandler(hub, args, level); |
77 | 57 | }); |
78 | 58 | } |
79 | 59 | } |
| 60 | + |
| 61 | +function consoleHandler(hub: Hub, args: unknown[], level: string): void { |
| 62 | + hub.withScope(scope => { |
| 63 | + scope.setLevel(severityLevelFromString(level)); |
| 64 | + scope.setExtra('arguments', args); |
| 65 | + scope.addEventProcessor(event => { |
| 66 | + event.logger = 'console'; |
| 67 | + return event; |
| 68 | + }); |
| 69 | + |
| 70 | + let message = safeJoin(args, ' '); |
| 71 | + const error = args.find(arg => arg instanceof Error); |
| 72 | + if (level === 'assert') { |
| 73 | + if (args[0] === false) { |
| 74 | + message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`; |
| 75 | + scope.setExtra('arguments', args.slice(1)); |
| 76 | + hub.captureMessage(message); |
| 77 | + } |
| 78 | + } else if (level === 'error' && error) { |
| 79 | + hub.captureException(error); |
| 80 | + } else { |
| 81 | + hub.captureMessage(message); |
| 82 | + } |
| 83 | + }); |
| 84 | +} |
0 commit comments