55 * @param {string } path The JSON path for that this call is at
66 * @returns {string } The stringified results
77 */
8- export const stringify = ( obj , processed , path = '' ) => {
8+ export const stringify = ( obj , processed , path = '' , isError ) => {
99 try {
1010 if ( typeof obj !== 'object' ) return JSON . stringify ( obj ) ;
1111 // error doesnt stringify correctly, manually include key fields like being an error
12- if ( obj instanceof Error ) return `{"t":"e","v":{"name":${ JSON . stringify ( obj . name ) ?? 'null' } ,"message":${ JSON . stringify ( obj . message ) ?? 'null' } ,"stack":${ JSON . stringify ( obj . stack ) ?? 'null' } ,"cause":${ JSON . stringify ( obj . cause ) ?? 'null' } }}` ;
12+ if ( obj instanceof Error ) {
13+ const set = {
14+ name : obj . name ?? null ,
15+ message : obj . message ?? null ,
16+ stack : obj . stack ?? null ,
17+ cause : obj . cause ?? null ,
18+ ...obj
19+ }
20+ return stringify ( set , processed , path , true ) ;
21+ }
1322 if ( obj === null ) return 'null' ;
1423 processed ??= [ ] ;
1524 processed . push ( [ path , obj ] ) ;
@@ -25,7 +34,7 @@ export const stringify = (obj, processed, path = '') => {
2534 }
2635 str += stringify ( obj [ name ] , processed , `${ path } /${ name } ` ) ;
2736 }
28- return ( isArray ? '{"t":"a","v":[' : '{"t":"o","v":{' ) + str . slice ( 1 ) + ( isArray ? ']}' : '}}' ) ;
37+ return ( isArray ? '{"t":"a","v":[' : isError ? '{"t":"e","v":{' : '{"t":"o","v":{' ) + str . slice ( 1 ) + ( isArray ? ']}' : '}}' ) ;
2938 } catch ( err ) {
3039 console . warn ( 'Failed to process with error' , err ) ;
3140 return JSON . stringify ( obj ) ;
@@ -44,13 +53,16 @@ const _parseRecursive = (obj, processed, path) => {
4453 if ( obj . t === 'c' ) return processed [ obj . v ] ;
4554 // build up an error object
4655 if ( obj . t === 'e' ) {
56+ obj . t = 'o' ;
57+ const meta = _parseRecursive ( obj , processed , path )
4758 const err = new Error ( '__TEMPLATE__' ) ;
48- if ( obj . v . cause ) err . cause = obj . v . cause ;
49- if ( obj . v . message ) err . message = obj . v . message ;
59+ if ( meta . cause ) err . cause = meta . cause ;
60+ if ( meta . message ) err . message = meta . message ;
5061 // @todo different browsers use different trace shapes, and so
5162 // we need to use something like the log capture serialization here instead
52- if ( obj . v . stack ) err . stack = obj . v . stack ;
53- if ( obj . v . name ) err . name = obj . v . name ;
63+ if ( meta . stack ) err . stack = meta . stack ;
64+ if ( meta . name ) err . name = meta . name ;
65+ Object . assign ( err , meta ) ;
5466 return err ;
5567 }
5668 const result = obj . t === 'a' ? [ ] : { } ;
0 commit comments