@@ -97,32 +97,21 @@ export function urlEncode(object: { [key: string]: any }): string {
97
97
*
98
98
* @param value Initial source that we have to transform in order for it to be usable by the serializer
99
99
*/
100
- export function getWalkSource ( value : any ) : {
101
- [ key : string ] : any ;
100
+ export function convertToPlainObject ( value : unknown ) : {
101
+ [ key : string ] : unknown ;
102
102
} {
103
+ let newObj = value as {
104
+ [ key : string ] : unknown ;
105
+ } ;
106
+
103
107
if ( isError ( value ) ) {
104
- const error = value as ExtendedError ;
105
- const err : {
106
- [ key : string ] : any ;
107
- stack : string | undefined ;
108
- message : string ;
109
- name : string ;
110
- } = {
111
- message : error . message ,
112
- name : error . name ,
113
- stack : error . stack ,
108
+ newObj = {
109
+ message : value . message ,
110
+ name : value . name ,
111
+ stack : value . stack ,
112
+ ...getOwnProperties ( value as ExtendedError ) ,
114
113
} ;
115
-
116
- for ( const i in error ) {
117
- if ( Object . prototype . hasOwnProperty . call ( error , i ) ) {
118
- err [ i ] = error [ i ] ;
119
- }
120
- }
121
-
122
- return err ;
123
- }
124
-
125
- if ( isEvent ( value ) ) {
114
+ } else if ( isEvent ( value ) ) {
126
115
/**
127
116
* Event-like interface that's usable in browser and node
128
117
*/
@@ -133,49 +122,40 @@ export function getWalkSource(value: any): {
133
122
currentTarget ?: unknown ;
134
123
}
135
124
136
- const event = value as unknown as SimpleEvent ;
137
-
138
- const source : {
139
- [ key : string ] : any ;
140
- } = { } ;
141
-
142
- // Accessing event attributes can throw (see https://github.com/getsentry/sentry-javascript/issues/768 and
143
- // https://github.com/getsentry/sentry-javascript/issues/838), but accessing `type` hasn't been wrapped in a
144
- // try-catch in at least two years and no one's complained, so that's likely not an issue anymore
145
- source . type = event . type ;
146
-
147
- try {
148
- source . target = isElement ( event . target )
149
- ? htmlTreeAsString ( event . target )
150
- : Object . prototype . toString . call ( event . target ) ;
151
- } catch ( _oO ) {
152
- source . target = '<unknown>' ;
153
- }
125
+ const event = value as SimpleEvent ;
154
126
155
- try {
156
- source . currentTarget = isElement ( event . currentTarget )
157
- ? htmlTreeAsString ( event . currentTarget )
158
- : Object . prototype . toString . call ( event . currentTarget ) ;
159
- } catch ( _oO ) {
160
- source . currentTarget = '<unknown>' ;
161
- }
127
+ newObj = {
128
+ type : event . type ,
129
+ target : serializeEventTarget ( event . target ) ,
130
+ currentTarget : serializeEventTarget ( event . currentTarget ) ,
131
+ ...getOwnProperties ( event ) ,
132
+ } ;
162
133
163
134
if ( typeof CustomEvent !== 'undefined' && isInstanceOf ( value , CustomEvent ) ) {
164
- source . detail = event . detail ;
165
- }
166
-
167
- for ( const attr in event ) {
168
- if ( Object . prototype . hasOwnProperty . call ( event , attr ) ) {
169
- source [ attr ] = event [ attr ] ;
170
- }
135
+ newObj . detail = event . detail ;
171
136
}
137
+ }
138
+ return newObj ;
139
+ }
172
140
173
- return source ;
141
+ /** Creates a string representation of the target of an `Event` object */
142
+ function serializeEventTarget ( target : unknown ) : string {
143
+ try {
144
+ return isElement ( target ) ? htmlTreeAsString ( target ) : Object . prototype . toString . call ( target ) ;
145
+ } catch ( _oO ) {
146
+ return '<unknown>' ;
174
147
}
148
+ }
175
149
176
- return value as {
177
- [ key : string ] : any ;
178
- } ;
150
+ /** Filters out all but an object's own properties */
151
+ function getOwnProperties ( obj : { [ key : string ] : unknown } ) : { [ key : string ] : unknown } {
152
+ const extractedProps : { [ key : string ] : unknown } = { } ;
153
+ for ( const property in obj ) {
154
+ if ( Object . prototype . hasOwnProperty . call ( obj , property ) ) {
155
+ extractedProps [ property ] = obj [ property ] ;
156
+ }
157
+ }
158
+ return extractedProps ;
179
159
}
180
160
181
161
/**
@@ -185,7 +165,7 @@ export function getWalkSource(value: any): {
185
165
*/
186
166
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
187
167
export function extractExceptionKeysForMessage ( exception : any , maxLength : number = 40 ) : string {
188
- const keys = Object . keys ( getWalkSource ( exception ) ) ;
168
+ const keys = Object . keys ( convertToPlainObject ( exception ) ) ;
189
169
keys . sort ( ) ;
190
170
191
171
if ( ! keys . length ) {
0 commit comments