1 | import { getCurrentHub } from '@sentry/hub';
|
2 | import { isError, isPlainObject, extractExceptionKeysForMessage, normalizeToSize, addExceptionTypeValue, addExceptionMechanism } from '@sentry/utils';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | function parseStackFrames(stackParser, error) {
|
8 | return stackParser(error.stack || '', 1);
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function exceptionFromError(stackParser, error) {
|
15 | var exception = {
|
16 | type: error.name || error.constructor.name,
|
17 | value: error.message,
|
18 | };
|
19 |
|
20 | var frames = parseStackFrames(stackParser, error);
|
21 | if (frames.length) {
|
22 | exception.stacktrace = { frames };
|
23 | }
|
24 |
|
25 | return exception;
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function eventFromUnknownInput(stackParser, exception, hint) {
|
33 | let ex = exception;
|
34 | var providedMechanism =
|
35 | hint && hint.data && (hint.data ).mechanism;
|
36 | var mechanism = providedMechanism || {
|
37 | handled: true,
|
38 | type: 'generic',
|
39 | };
|
40 |
|
41 | if (!isError(exception)) {
|
42 | if (isPlainObject(exception)) {
|
43 |
|
44 |
|
45 | var message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`;
|
46 |
|
47 | getCurrentHub().configureScope(scope => {
|
48 | scope.setExtra('__serialized__', normalizeToSize(exception));
|
49 | });
|
50 |
|
51 | ex = (hint && hint.syntheticException) || new Error(message);
|
52 | (ex ).message = message;
|
53 | } else {
|
54 |
|
55 |
|
56 | ex = (hint && hint.syntheticException) || new Error(exception );
|
57 | (ex ).message = exception ;
|
58 | }
|
59 | mechanism.synthetic = true;
|
60 | }
|
61 |
|
62 | var event = {
|
63 | exception: {
|
64 | values: [exceptionFromError(stackParser, ex )],
|
65 | },
|
66 | };
|
67 |
|
68 | addExceptionTypeValue(event, undefined, undefined);
|
69 | addExceptionMechanism(event, mechanism);
|
70 |
|
71 | return {
|
72 | ...event,
|
73 | event_id: hint && hint.event_id,
|
74 | };
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function eventFromMessage(
|
82 | stackParser,
|
83 | message,
|
84 | level = 'info',
|
85 | hint,
|
86 | attachStacktrace,
|
87 | ) {
|
88 | var event = {
|
89 | event_id: hint && hint.event_id,
|
90 | level,
|
91 | message,
|
92 | };
|
93 |
|
94 | if (attachStacktrace && hint && hint.syntheticException) {
|
95 | var frames = parseStackFrames(stackParser, hint.syntheticException);
|
96 | if (frames.length) {
|
97 | event.exception = {
|
98 | values: [
|
99 | {
|
100 | value: message,
|
101 | stacktrace: { frames },
|
102 | },
|
103 | ],
|
104 | };
|
105 | }
|
106 | }
|
107 |
|
108 | return event;
|
109 | }
|
110 |
|
111 | export { eventFromMessage, eventFromUnknownInput, exceptionFromError, parseStackFrames };
|
112 |
|