1- import type { Contexts , Event , EventHint , EventProcessor , ExtendedError , Hub , Integration } from '@sentry/types' ;
1+ import type { Contexts , Event , EventHint , ExtendedError , Integration } from '@sentry/types' ;
22import { addNonEnumerableProperty , isError , isPlainObject , logger , normalize } from '@sentry/utils' ;
33
44/** JSDoc */
55interface ExtraErrorDataOptions {
6- depth ? : number ;
6+ depth : number ;
77}
88
99/** Patch toString calls to return proper name for wrapped functions */
@@ -24,7 +24,7 @@ export class ExtraErrorData implements Integration {
2424 /**
2525 * @inheritDoc
2626 */
27- public constructor ( options ?: ExtraErrorDataOptions ) {
27+ public constructor ( options ?: Partial < ExtraErrorDataOptions > ) {
2828 this . name = ExtraErrorData . id ;
2929
3030 this . _options = {
@@ -36,94 +36,99 @@ export class ExtraErrorData implements Integration {
3636 /**
3737 * @inheritDoc
3838 */
39- public setupOnce ( addGlobalEventProcessor : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
40- addGlobalEventProcessor ( ( event : Event , hint : EventHint ) => {
41- const self = getCurrentHub ( ) . getIntegration ( ExtraErrorData ) ;
42- if ( ! self ) {
43- return event ;
44- }
45- return self . enhanceEventWithErrorData ( event , hint ) ;
46- } ) ;
39+ public setupOnce ( _addGlobaleventProcessor : unknown , _getCurrentHub : unknown ) : void {
40+ // noop
41+ }
42+
43+ /** @inheritDoc */
44+ public processEvent ( event : Event , hint : EventHint ) : Event {
45+ return this . enhanceEventWithErrorData ( event , hint ) ;
4746 }
4847
4948 /**
50- * Attaches extracted information from the Error object to extra field in the Event
49+ * Attaches extracted information from the Error object to extra field in the Event.
50+ *
51+ * TODO (v8): Drop this public function.
5152 */
5253 public enhanceEventWithErrorData ( event : Event , hint : EventHint = { } ) : Event {
53- if ( ! hint . originalException || ! isError ( hint . originalException ) ) {
54- return event ;
55- }
56- const exceptionName = ( hint . originalException as ExtendedError ) . name || hint . originalException . constructor . name ;
54+ return _enhanceEventWithErrorData ( event , hint , this . _options . depth ) ;
55+ }
56+ }
5757
58- const errorData = this . _extractErrorData ( hint . originalException as ExtendedError ) ;
58+ function _enhanceEventWithErrorData ( event : Event , hint : EventHint = { } , depth : number ) : Event {
59+ if ( ! hint . originalException || ! isError ( hint . originalException ) ) {
60+ return event ;
61+ }
62+ const exceptionName = ( hint . originalException as ExtendedError ) . name || hint . originalException . constructor . name ;
5963
60- if ( errorData ) {
61- const contexts : Contexts = {
62- ...event . contexts ,
63- } ;
64+ const errorData = _extractErrorData ( hint . originalException as ExtendedError ) ;
6465
65- const normalizedErrorData = normalize ( errorData , this . _options . depth ) ;
66+ if ( errorData ) {
67+ const contexts : Contexts = {
68+ ...event . contexts ,
69+ } ;
6670
67- if ( isPlainObject ( normalizedErrorData ) ) {
68- // We mark the error data as "already normalized" here, because we don't want other normalization procedures to
69- // potentially truncate the data we just already normalized, with a certain depth setting.
70- addNonEnumerableProperty ( normalizedErrorData , '__sentry_skip_normalization__' , true ) ;
71- contexts [ exceptionName ] = normalizedErrorData ;
72- }
71+ const normalizedErrorData = normalize ( errorData , depth ) ;
7372
74- return {
75- ...event ,
76- contexts,
77- } ;
73+ if ( isPlainObject ( normalizedErrorData ) ) {
74+ // We mark the error data as "already normalized" here, because we don't want other normalization procedures to
75+ // potentially truncate the data we just already normalized, with a certain depth setting.
76+ addNonEnumerableProperty ( normalizedErrorData , '__sentry_skip_normalization__' , true ) ;
77+ contexts [ exceptionName ] = normalizedErrorData ;
7878 }
7979
80- return event ;
80+ return {
81+ ...event ,
82+ contexts,
83+ } ;
8184 }
8285
83- /**
84- * Extract extra information from the Error object
85- */
86- private _extractErrorData ( error : ExtendedError ) : Record < string , unknown > | null {
87- // We are trying to enhance already existing event, so no harm done if it won't succeed
88- try {
89- const nativeKeys = [
90- 'name' ,
91- 'message' ,
92- 'stack' ,
93- 'line ',
94- 'column ',
95- 'fileName ',
96- 'lineNumber ',
97- 'columnNumber ',
98- 'toJSON ',
99- ] ;
100-
101- const extraErrorInfo : Record < string , unknown > = { } ;
102-
103- // We want only enumerable properties, thus `getOwnPropertyNames` is redundant here, as we filter keys anyway.
104- for ( const key of Object . keys ( error ) ) {
105- if ( nativeKeys . indexOf ( key ) !== - 1 ) {
106- continue ;
107- }
108- const value = error [ key ] ;
109- extraErrorInfo [ key ] = isError ( value ) ? value . toString ( ) : value ;
86+ return event ;
87+ }
88+
89+ /**
90+ * Extract extra information from the Error object
91+ */
92+ function _extractErrorData ( error : ExtendedError ) : Record < string , unknown > | null {
93+ // We are trying to enhance already existing event, so no harm done if it won't succeed
94+ try {
95+ const nativeKeys = [
96+ 'name ',
97+ 'message ',
98+ 'stack ',
99+ 'line ',
100+ 'column ',
101+ 'fileName ',
102+ 'lineNumber' ,
103+ 'columnNumber' ,
104+ 'toJSON' ,
105+ ] ;
106+
107+ const extraErrorInfo : Record < string , unknown > = { } ;
108+
109+ // We want only enumerable properties, thus `getOwnPropertyNames` is redundant here, as we filter keys anyway.
110+ for ( const key of Object . keys ( error ) ) {
111+ if ( nativeKeys . indexOf ( key ) !== - 1 ) {
112+ continue ;
110113 }
114+ const value = error [ key ] ;
115+ extraErrorInfo [ key ] = isError ( value ) ? value . toString ( ) : value ;
116+ }
111117
112- // Check if someone attached `toJSON` method to grab even more properties (eg. axios is doing that)
113- if ( typeof error . toJSON === 'function' ) {
114- const serializedError = error . toJSON ( ) as Record < string , unknown > ;
118+ // Check if someone attached `toJSON` method to grab even more properties (eg. axios is doing that)
119+ if ( typeof error . toJSON === 'function' ) {
120+ const serializedError = error . toJSON ( ) as Record < string , unknown > ;
115121
116- for ( const key of Object . keys ( serializedError ) ) {
117- const value = serializedError [ key ] ;
118- extraErrorInfo [ key ] = isError ( value ) ? value . toString ( ) : value ;
119- }
122+ for ( const key of Object . keys ( serializedError ) ) {
123+ const value = serializedError [ key ] ;
124+ extraErrorInfo [ key ] = isError ( value ) ? value . toString ( ) : value ;
120125 }
121-
122- return extraErrorInfo ;
123- } catch ( oO ) {
124- __DEBUG_BUILD__ && logger . error ( 'Unable to extract extra data from the Error object:' , oO ) ;
125126 }
126127
127- return null ;
128+ return extraErrorInfo ;
129+ } catch ( oO ) {
130+ __DEBUG_BUILD__ && logger . error ( 'Unable to extract extra data from the Error object:' , oO ) ;
128131 }
132+
133+ return null ;
129134}
0 commit comments