@@ -7,16 +7,29 @@ import { DEBUG_BUILD } from './debug-build';
7
7
const INTEGRATION_NAME = 'ExtraErrorData' ;
8
8
9
9
interface ExtraErrorDataOptions {
10
+ /**
11
+ * The object depth up to which to capture data on error objects.
12
+ */
10
13
depth : number ;
14
+
15
+ /**
16
+ * Whether to capture error causes.
17
+ *
18
+ * More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
19
+ */
20
+ captureErrorCause : boolean ;
11
21
}
12
22
13
23
const extraErrorDataIntegration = ( ( options : Partial < ExtraErrorDataOptions > = { } ) => {
14
24
const depth = options . depth || 3 ;
15
25
26
+ // TODO(v8): Flip the default for this option to true
27
+ const captureErrorCause = options . captureErrorCause || false ;
28
+
16
29
return {
17
30
name : INTEGRATION_NAME ,
18
31
processEvent ( event , hint ) {
19
- return _enhanceEventWithErrorData ( event , hint , depth ) ;
32
+ return _enhanceEventWithErrorData ( event , hint , depth , captureErrorCause ) ;
20
33
} ,
21
34
} ;
22
35
} ) satisfies IntegrationFn ;
@@ -25,13 +38,18 @@ const extraErrorDataIntegration = ((options: Partial<ExtraErrorDataOptions> = {}
25
38
// eslint-disable-next-line deprecation/deprecation
26
39
export const ExtraErrorData = convertIntegrationFnToClass ( INTEGRATION_NAME , extraErrorDataIntegration ) ;
27
40
28
- function _enhanceEventWithErrorData ( event : Event , hint : EventHint = { } , depth : number ) : Event {
41
+ function _enhanceEventWithErrorData (
42
+ event : Event ,
43
+ hint : EventHint = { } ,
44
+ depth : number ,
45
+ captureErrorCause : boolean ,
46
+ ) : Event {
29
47
if ( ! hint . originalException || ! isError ( hint . originalException ) ) {
30
48
return event ;
31
49
}
32
50
const exceptionName = ( hint . originalException as ExtendedError ) . name || hint . originalException . constructor . name ;
33
51
34
- const errorData = _extractErrorData ( hint . originalException as ExtendedError ) ;
52
+ const errorData = _extractErrorData ( hint . originalException as ExtendedError , captureErrorCause ) ;
35
53
36
54
if ( errorData ) {
37
55
const contexts : Contexts = {
@@ -59,7 +77,7 @@ function _enhanceEventWithErrorData(event: Event, hint: EventHint = {}, depth: n
59
77
/**
60
78
* Extract extra information from the Error object
61
79
*/
62
- function _extractErrorData ( error : ExtendedError ) : Record < string , unknown > | null {
80
+ function _extractErrorData ( error : ExtendedError , captureErrorCause : boolean ) : Record < string , unknown > | null {
63
81
// We are trying to enhance already existing event, so no harm done if it won't succeed
64
82
try {
65
83
const nativeKeys = [
@@ -85,6 +103,12 @@ function _extractErrorData(error: ExtendedError): Record<string, unknown> | null
85
103
extraErrorInfo [ key ] = isError ( value ) ? value . toString ( ) : value ;
86
104
}
87
105
106
+ // Error.cause is a standard property that is non enumerable, we therefore need to access it separately.
107
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
108
+ if ( captureErrorCause && error . cause !== undefined ) {
109
+ extraErrorInfo . cause = isError ( error . cause ) ? error . cause . toString ( ) : error . cause ;
110
+ }
111
+
88
112
// Check if someone attached `toJSON` method to grab even more properties (eg. axios is doing that)
89
113
if ( typeof error . toJSON === 'function' ) {
90
114
const serializedError = error . toJSON ( ) as Record < string , unknown > ;
0 commit comments