-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathextraerrordata.ts
More file actions
147 lines (127 loc) · 4.72 KB
/
extraerrordata.ts
File metadata and controls
147 lines (127 loc) · 4.72 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { DEBUG_BUILD } from '../debug-build';
import { defineIntegration } from '../integration';
import type { Contexts } from '../types-hoist/context';
import type { ExtendedError } from '../types-hoist/error';
import type { Event, EventHint } from '../types-hoist/event';
import type { IntegrationFn } from '../types-hoist/integration';
import { debug } from '../utils/debug-logger';
import { isError, isPlainObject } from '../utils/is';
import { normalize } from '../utils/normalize';
import { setSkipNormalizationHint } from '../utils/normalizationHints';
import { truncate } from '../utils/string';
const INTEGRATION_NAME = 'ExtraErrorData';
interface ExtraErrorDataOptions {
/**
* The object depth up to which to capture data on error objects.
*/
depth: number;
/**
* Whether to capture error causes. Defaults to true.
*
* More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
*/
captureErrorCause: boolean;
}
/**
* Extract additional data for from original exceptions.
*/
const _extraErrorDataIntegration = ((options: Partial<ExtraErrorDataOptions> = {}) => {
const { depth = 3, captureErrorCause = true } = options;
return {
name: INTEGRATION_NAME,
processEvent(event, hint, client) {
const { maxValueLength } = client.getOptions();
return _enhanceEventWithErrorData(event, hint, depth, captureErrorCause, maxValueLength);
},
};
}) satisfies IntegrationFn;
export const extraErrorDataIntegration = defineIntegration(_extraErrorDataIntegration);
function _enhanceEventWithErrorData(
event: Event,
hint: EventHint = {},
depth: number,
captureErrorCause: boolean,
maxValueLength: number | undefined,
): Event {
if (!hint.originalException || !isError(hint.originalException)) {
return event;
}
const exceptionName = (hint.originalException as ExtendedError).name || hint.originalException.constructor.name;
const errorData = _extractErrorData(hint.originalException as ExtendedError, captureErrorCause, maxValueLength);
if (errorData) {
const contexts: Contexts = {
...event.contexts,
};
const normalizedErrorData = normalize(errorData, depth);
if (isPlainObject(normalizedErrorData)) {
// We mark the error data as "already normalized" here, because we don't want other normalization procedures to
// potentially truncate the data we just already normalized, with a certain depth setting.
setSkipNormalizationHint(normalizedErrorData);
contexts[exceptionName] = normalizedErrorData;
}
return {
...event,
contexts,
};
}
return event;
}
/**
* Extract extra information from the Error object
*/
function _extractErrorData(
error: ExtendedError,
captureErrorCause: boolean,
maxValueLength: number | undefined,
): Record<string, unknown> | null {
// We are trying to enhance already existing event, so no harm done if it won't succeed
try {
const nativeKeys = [
'name',
'message',
'stack',
'line',
'column',
'fileName',
'lineNumber',
'columnNumber',
'toJSON',
];
const extraErrorInfo: Record<string, unknown> = {};
// We want only enumerable properties, thus `getOwnPropertyNames` is redundant here, as we filter keys anyway.
for (const key of Object.keys(error)) {
if (nativeKeys.indexOf(key) !== -1) {
continue;
}
const value = error[key];
extraErrorInfo[key] =
isError(value) || typeof value === 'string'
? maxValueLength
? truncate(`${value}`, maxValueLength)
: `${value}`
: value;
}
// Error.cause is a standard property that is non enumerable, we therefore need to access it separately.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
if (captureErrorCause && error.cause !== undefined) {
if (isError(error.cause)) {
const errorName = error.cause.name || error.cause.constructor.name;
extraErrorInfo.cause = { [errorName]: _extractErrorData(error.cause as ExtendedError, false, maxValueLength) };
} else {
extraErrorInfo.cause = error.cause;
}
}
// Check if someone attached `toJSON` method to grab even more properties (eg. axios is doing that)
if (typeof error.toJSON === 'function') {
const serializedError = error.toJSON() as Record<string, unknown>;
for (const key of Object.keys(serializedError)) {
const value = serializedError[key];
extraErrorInfo[key] = isError(value) ? value.toString() : value;
}
}
return extraErrorInfo;
} catch (oO) {
DEBUG_BUILD && debug.error('Unable to extract extra data from the Error object:', oO);
}
return null;
}