-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcaptureconsole.ts
More file actions
91 lines (77 loc) · 2.09 KB
/
Copy pathcaptureconsole.ts
File metadata and controls
91 lines (77 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type { EventProcessor, Hub, Integration } from '@sentry/types';
import {
addExceptionMechanism,
addInstrumentationHandler,
CONSOLE_LEVELS,
GLOBAL_OBJ,
safeJoin,
severityLevelFromString,
} from '@sentry/utils';
/** Send Console API calls as Sentry Events */
export class CaptureConsole implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'CaptureConsole';
/**
* @inheritDoc
*/
public name: string;
/**
* @inheritDoc
*/
private readonly _levels: readonly string[];
/**
* @inheritDoc
*/
public constructor(options: { levels?: string[] } = {}) {
this.name = CaptureConsole.id;
this._levels = options.levels || CONSOLE_LEVELS;
}
/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
if (!('console' in GLOBAL_OBJ)) {
return;
}
const levels = this._levels;
addInstrumentationHandler('console', ({ args, level }: { args: unknown[]; level: string }) => {
if (!levels.includes(level)) {
return;
}
const hub = getCurrentHub();
if (!hub.getIntegration(CaptureConsole)) {
return;
}
consoleHandler(hub, args, level);
});
}
}
function consoleHandler(hub: Hub, args: unknown[], level: string): void {
hub.withScope(scope => {
scope.setLevel(severityLevelFromString(level));
scope.setExtra('arguments', args);
scope.addEventProcessor(event => {
event.logger = 'console';
addExceptionMechanism(event, {
handled: false,
type: 'console',
});
return event;
});
let message = safeJoin(args, ' ');
const error = args.find(arg => arg instanceof Error);
if (level === 'assert') {
if (args[0] === false) {
message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
scope.setExtra('arguments', args.slice(1));
hub.captureMessage(message);
}
} else if (level === 'error' && error) {
hub.captureException(error);
} else {
hub.captureMessage(message);
}
});
}