UNPKG

2.38 kBJavaScriptView Raw
1import { CONSOLE_LEVELS, GLOBAL_OBJ, fill, severityLevelFromString, safeJoin } from '@sentry/utils';
2
3/** Send Console API calls as Sentry Events */
4class CaptureConsole {
5 /**
6 * @inheritDoc
7 */
8 static __initStatic() {this.id = 'CaptureConsole';}
9
10 /**
11 * @inheritDoc
12 */
13 __init() {this.name = CaptureConsole.id;}
14
15 /**
16 * @inheritDoc
17 */
18 __init2() {this._levels = CONSOLE_LEVELS;}
19
20 /**
21 * @inheritDoc
22 */
23 constructor(options = {}) {CaptureConsole.prototype.__init.call(this);CaptureConsole.prototype.__init2.call(this);
24 if (options.levels) {
25 this._levels = options.levels;
26 }
27 }
28
29 /**
30 * @inheritDoc
31 */
32 setupOnce(_, getCurrentHub) {
33 if (!('console' in GLOBAL_OBJ)) {
34 return;
35 }
36
37 this._levels.forEach((level) => {
38 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
39 if (!(level in (GLOBAL_OBJ ).console)) {
40 return;
41 }
42
43 // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
44 fill((GLOBAL_OBJ ).console, level, (originalConsoleMethod) => (...args) => {
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 });
55
56 let message = safeJoin(args, ' ');
57 if (level === 'assert') {
58 if (args[0] === false) {
59 message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
60 scope.setExtra('arguments', args.slice(1));
61 hub.captureMessage(message);
62 }
63 } else if (level === 'error' && args[0] instanceof Error) {
64 hub.captureException(args[0]);
65 } else {
66 hub.captureMessage(message);
67 }
68 });
69 }
70
71 // this fails for some browsers. :(
72 if (originalConsoleMethod) {
73 originalConsoleMethod.apply(GLOBAL_OBJ.console, args);
74 }
75 });
76 });
77 }
78} CaptureConsole.__initStatic();
79
80export { CaptureConsole };
81//# sourceMappingURL=captureconsole.js.map