@@ -92,50 +92,32 @@ export function urlEncode(object: { [key: string]: any }): string {
9292}
9393
9494/**
95- * Transforms any object into an object literal with all its attributes
96- * attached to it .
95+ * Transforms any `Error` or `Event` into a plain object with all of their enumerable properties, and some of their
96+ * non-enumerable properties attached .
9797 *
9898 * @param value Initial source that we have to transform in order for it to be usable by the serializer
99+ * @returns An Event or Error turned into an object - or the value argurment itself, when value is neither an Event nor
100+ * an Error.
99101 */
100- export function convertToPlainObject ( value : unknown ) : {
101- [ key : string ] : unknown ;
102- } {
103- let newObj = value as {
104- [ key : string ] : unknown ;
105- } ;
106-
102+ export function convertToPlainObject < V extends unknown > ( value : V ) : Record < string , unknown > | V {
107103 if ( isError ( value ) ) {
108- newObj = {
104+ return {
109105 message : value . message ,
110106 name : value . name ,
111107 stack : value . stack ,
112- ...getOwnProperties ( value as ExtendedError ) ,
108+ ...getOwnProperties ( value ) ,
113109 } ;
114110 } else if ( isEvent ( value ) ) {
115- /**
116- * Event-like interface that's usable in browser and node
117- */
118- interface SimpleEvent {
119- [ key : string ] : unknown ;
120- type : string ;
121- target ?: unknown ;
122- currentTarget ?: unknown ;
123- }
124-
125- const event = value as SimpleEvent ;
126-
127- newObj = {
128- type : event . type ,
129- target : serializeEventTarget ( event . target ) ,
130- currentTarget : serializeEventTarget ( event . currentTarget ) ,
131- ...getOwnProperties ( event ) ,
111+ return {
112+ type : value . type ,
113+ target : serializeEventTarget ( value . target ) ,
114+ currentTarget : serializeEventTarget ( value . currentTarget ) ,
115+ ...getOwnProperties ( value ) ,
116+ detail : typeof CustomEvent !== 'undefined' && isInstanceOf ( value , CustomEvent ) ? value . detail : undefined ,
132117 } ;
133-
134- if ( typeof CustomEvent !== 'undefined' && isInstanceOf ( value , CustomEvent ) ) {
135- newObj . detail = event . detail ;
136- }
118+ } else {
119+ return value ;
137120 }
138- return newObj ;
139121}
140122
141123/** Creates a string representation of the target of an `Event` object */
@@ -148,23 +130,26 @@ function serializeEventTarget(target: unknown): string {
148130}
149131
150132/** 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 ] ;
133+ function getOwnProperties ( obj : unknown ) : { [ key : string ] : unknown } {
134+ if ( typeof obj === 'object' && obj !== null ) {
135+ const extractedProps : { [ key : string ] : unknown } = { } ;
136+ for ( const property in obj ) {
137+ if ( Object . prototype . hasOwnProperty . call ( obj , property ) ) {
138+ extractedProps [ property ] = ( obj as Record < string , unknown > ) [ property ] ;
139+ }
156140 }
141+ return extractedProps ;
142+ } else {
143+ return { } ;
157144 }
158- return extractedProps ;
159145}
160146
161147/**
162148 * Given any captured exception, extract its keys and create a sorted
163149 * and truncated list that will be used inside the event message.
164150 * eg. `Non-error exception captured with keys: foo, bar, baz`
165151 */
166- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
167- export function extractExceptionKeysForMessage ( exception : any , maxLength : number = 40 ) : string {
152+ export function extractExceptionKeysForMessage ( exception : Record < string , unknown > , maxLength : number = 40 ) : string {
168153 const keys = Object . keys ( convertToPlainObject ( exception ) ) ;
169154 keys . sort ( ) ;
170155
0 commit comments