@@ -74,20 +74,43 @@ export class ExtraErrorData implements Integration {
7474 /**
7575 * Extract extra information from the Error object
7676 */
77- private _extractErrorData ( error : ExtendedError ) : { [ key : string ] : unknown } | null {
77+ private _extractErrorData ( error : ExtendedError ) : Record < string , unknown > | null {
7878 // We are trying to enhance already existing event, so no harm done if it won't succeed
7979 try {
80- const nativeKeys = [ 'name' , 'message' , 'stack' , 'line' , 'column' , 'fileName' , 'lineNumber' , 'columnNumber' ] ;
81- const errorKeys = Object . getOwnPropertyNames ( error ) . filter ( key => nativeKeys . indexOf ( key ) === - 1 ) ;
80+ const nativeKeys = [
81+ 'name' ,
82+ 'message' ,
83+ 'stack' ,
84+ 'line' ,
85+ 'column' ,
86+ 'fileName' ,
87+ 'lineNumber' ,
88+ 'columnNumber' ,
89+ 'toJSON' ,
90+ ] ;
8291
83- if ( errorKeys . length ) {
84- const extraErrorInfo : { [ key : string ] : unknown } = { } ;
85- for ( const key of errorKeys ) {
86- const value = error [ key ] ;
92+ const extraErrorInfo : Record < string , unknown > = { } ;
93+
94+ // We want only enumerable properties, thus `getOwnPropertyNames` is redundant here, as we filter keys anyway.
95+ for ( const key of Object . keys ( error ) ) {
96+ if ( nativeKeys . indexOf ( key ) !== - 1 ) {
97+ continue ;
98+ }
99+ const value = error [ key ] ;
100+ extraErrorInfo [ key ] = isError ( value ) ? ( value as Error ) . toString ( ) : value ;
101+ }
102+
103+ // Check if someone attached `toJSON` method to grab even more properties (eg. axios is doing that)
104+ if ( typeof error . toJSON === 'function' ) {
105+ const serializedError = error . toJSON ( ) as Record < string , unknown > ;
106+
107+ for ( const key of Object . keys ( serializedError ) ) {
108+ const value = serializedError [ key ] ;
87109 extraErrorInfo [ key ] = isError ( value ) ? ( value as Error ) . toString ( ) : value ;
88110 }
89- return extraErrorInfo ;
90111 }
112+
113+ return extraErrorInfo ;
91114 } catch ( oO ) {
92115 logger . error ( 'Unable to extract extra data from the Error object:' , oO ) ;
93116 }
0 commit comments