UNPKG

3.14 kBJavaScriptView Raw
1import { extractExceptionKeysForMessage, isEvent, normalizeToSize } from '@sentry/utils';
2import { computeStackTrace } from './tracekit';
3var STACKTRACE_LIMIT = 50;
4/**
5 * This function creates an exception from an TraceKitStackTrace
6 * @param stacktrace TraceKitStackTrace that will be converted to an exception
7 * @hidden
8 */
9export function exceptionFromStacktrace(stacktrace) {
10 var frames = prepareFramesForEvent(stacktrace.stack);
11 var exception = {
12 type: stacktrace.name,
13 value: stacktrace.message,
14 };
15 if (frames && frames.length) {
16 exception.stacktrace = { frames: frames };
17 }
18 if (exception.type === undefined && exception.value === '') {
19 exception.value = 'Unrecoverable error caught';
20 }
21 return exception;
22}
23/**
24 * @hidden
25 */
26export function eventFromPlainObject(exception, syntheticException, rejection) {
27 var event = {
28 exception: {
29 values: [
30 {
31 type: isEvent(exception) ? exception.constructor.name : rejection ? 'UnhandledRejection' : 'Error',
32 value: "Non-Error " + (rejection ? 'promise rejection' : 'exception') + " captured with keys: " + extractExceptionKeysForMessage(exception),
33 },
34 ],
35 },
36 extra: {
37 __serialized__: normalizeToSize(exception),
38 },
39 };
40 if (syntheticException) {
41 var stacktrace = computeStackTrace(syntheticException);
42 var frames_1 = prepareFramesForEvent(stacktrace.stack);
43 event.stacktrace = {
44 frames: frames_1,
45 };
46 }
47 return event;
48}
49/**
50 * @hidden
51 */
52export function eventFromStacktrace(stacktrace) {
53 var exception = exceptionFromStacktrace(stacktrace);
54 return {
55 exception: {
56 values: [exception],
57 },
58 };
59}
60/**
61 * @hidden
62 */
63export function prepareFramesForEvent(stack) {
64 if (!stack || !stack.length) {
65 return [];
66 }
67 var localStack = stack;
68 var firstFrameFunction = localStack[0].func || '';
69 var lastFrameFunction = localStack[localStack.length - 1].func || '';
70 // If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
71 if (firstFrameFunction.indexOf('captureMessage') !== -1 || firstFrameFunction.indexOf('captureException') !== -1) {
72 localStack = localStack.slice(1);
73 }
74 // If stack ends with one of our internal API calls, remove it (ends, meaning it's the bottom of the stack - aka top-most call)
75 if (lastFrameFunction.indexOf('sentryWrapped') !== -1) {
76 localStack = localStack.slice(0, -1);
77 }
78 // The frame where the crash happened, should be the last entry in the array
79 return localStack
80 .slice(0, STACKTRACE_LIMIT)
81 .map(function (frame) { return ({
82 colno: frame.column === null ? undefined : frame.column,
83 filename: frame.url || localStack[0].url,
84 function: frame.func || '?',
85 in_app: true,
86 lineno: frame.line === null ? undefined : frame.line,
87 }); })
88 .reverse();
89}
90//# sourceMappingURL=parsers.js.map
\No newline at end of file