1
- import type { Contexts , Event , EventHint , EventProcessor , ExtendedError , Hub , Integration } from '@sentry/types' ;
1
+ import type { Contexts , Event , EventHint , ExtendedError , Integration } from '@sentry/types' ;
2
2
import { addNonEnumerableProperty , isError , isPlainObject , logger , normalize } from '@sentry/utils' ;
3
3
4
4
/** JSDoc */
5
5
interface ExtraErrorDataOptions {
6
- depth ? : number ;
6
+ depth : number ;
7
7
}
8
8
9
9
/** Patch toString calls to return proper name for wrapped functions */
@@ -24,7 +24,7 @@ export class ExtraErrorData implements Integration {
24
24
/**
25
25
* @inheritDoc
26
26
*/
27
- public constructor ( options ?: ExtraErrorDataOptions ) {
27
+ public constructor ( options ?: Partial < ExtraErrorDataOptions > ) {
28
28
this . name = ExtraErrorData . id ;
29
29
30
30
this . _options = {
@@ -36,94 +36,99 @@ export class ExtraErrorData implements Integration {
36
36
/**
37
37
* @inheritDoc
38
38
*/
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 ) ;
47
46
}
48
47
49
48
/**
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.
51
52
*/
52
53
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
+ }
57
57
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 ;
59
63
60
- if ( errorData ) {
61
- const contexts : Contexts = {
62
- ...event . contexts ,
63
- } ;
64
+ const errorData = _extractErrorData ( hint . originalException as ExtendedError ) ;
64
65
65
- const normalizedErrorData = normalize ( errorData , this . _options . depth ) ;
66
+ if ( errorData ) {
67
+ const contexts : Contexts = {
68
+ ...event . contexts ,
69
+ } ;
66
70
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 ) ;
73
72
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 ;
78
78
}
79
79
80
- return event ;
80
+ return {
81
+ ...event ,
82
+ contexts,
83
+ } ;
81
84
}
82
85
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 ;
110
113
}
114
+ const value = error [ key ] ;
115
+ extraErrorInfo [ key ] = isError ( value ) ? value . toString ( ) : value ;
116
+ }
111
117
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 > ;
115
121
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 ;
120
125
}
121
-
122
- return extraErrorInfo ;
123
- } catch ( oO ) {
124
- __DEBUG_BUILD__ && logger . error ( 'Unable to extract extra data from the Error object:' , oO ) ;
125
126
}
126
127
127
- return null ;
128
+ return extraErrorInfo ;
129
+ } catch ( oO ) {
130
+ __DEBUG_BUILD__ && logger . error ( 'Unable to extract extra data from the Error object:' , oO ) ;
128
131
}
132
+
133
+ return null ;
129
134
}
0 commit comments