@@ -228,4 +228,116 @@ at bar <stripped-path>bar.js:92\
228228 } )
229229 } )
230230 } )
231+
232+ context ( '.safeErrorSerialize' , ( ) => {
233+ it ( 'returns string as-is when error is already a string' , ( ) => {
234+ const stringError = 'Simple string error'
235+
236+ expect ( exception . safeErrorSerialize ( stringError ) ) . to . eq ( 'Simple string error' )
237+ } )
238+
239+ it ( 'serializes plain objects properly' , ( ) => {
240+ const objectError = {
241+ additionalData : { type : 'studio:panel:opened' } ,
242+ message : 'Something went wrong' ,
243+ code : 'TELEMETRY_ERROR' ,
244+ }
245+
246+ const result = exception . safeErrorSerialize ( objectError )
247+
248+ expect ( result ) . to . eq ( JSON . stringify ( objectError ) )
249+ } )
250+
251+ it ( 'handles circular reference objects safely without throwing' , ( ) => {
252+ // Create an object with circular reference
253+ const circularError = {
254+ message : 'Circular reference error' ,
255+ code : 'CIRCULAR_ERROR' ,
256+ }
257+
258+ circularError . self = circularError // Create circular reference
259+
260+ const result = exception . safeErrorSerialize ( circularError )
261+
262+ expect ( result ) . to . eq ( JSON . stringify ( {
263+ message : 'Circular reference error' ,
264+ code : 'CIRCULAR_ERROR' ,
265+ self : '[Circular]' ,
266+ } ) )
267+ } )
268+
269+ it ( 'handles Error objects correctly' , ( ) => {
270+ const error = new Error ( 'test error' )
271+
272+ error . code = 'TEST_CODE'
273+ error . errno = 123
274+
275+ const result = exception . safeErrorSerialize ( error )
276+
277+ // serializeError should preserve Error properties
278+ const parsed = JSON . parse ( result )
279+
280+ expect ( parsed . message ) . to . eq ( 'test error' )
281+ expect ( parsed . name ) . to . eq ( 'Error' )
282+ expect ( parsed . code ) . to . eq ( 'TEST_CODE' )
283+ expect ( parsed . errno ) . to . eq ( 123 )
284+ } )
285+
286+ it ( 'handles null and undefined gracefully' , ( ) => {
287+ expect ( exception . safeErrorSerialize ( null ) ) . to . eq ( 'null' )
288+ expect ( exception . safeErrorSerialize ( undefined ) ) . to . eq ( 'undefined' )
289+ } )
290+
291+ it ( 'handles primitive types' , ( ) => {
292+ expect ( exception . safeErrorSerialize ( 42 ) ) . to . eq ( '42' )
293+ expect ( exception . safeErrorSerialize ( true ) ) . to . eq ( 'true' )
294+ expect ( exception . safeErrorSerialize ( false ) ) . to . eq ( 'false' )
295+ } )
296+
297+ it ( 'provides fallback for non-serializable objects' , ( ) => {
298+ // Create an object that might cause issues
299+ const problematicObject = {
300+ get value ( ) {
301+ throw new Error ( 'Cannot access value' )
302+ } ,
303+ }
304+
305+ const result = exception . safeErrorSerialize ( problematicObject )
306+
307+ expect ( result ) . to . match ( / ^ \[ N o n - s e r i a l i z a b l e o b j e c t : / )
308+ } )
309+
310+ it ( 'handles deeply nested objects' , ( ) => {
311+ const deepObject = {
312+ level1 : {
313+ level2 : {
314+ level3 : {
315+ level4 : {
316+ message : 'Deep error' ,
317+ data : [ 1 , 2 , 3 , { nested : true } ] ,
318+ } ,
319+ } ,
320+ } ,
321+ } ,
322+ }
323+
324+ const result = exception . safeErrorSerialize ( deepObject )
325+
326+ expect ( result ) . to . eq ( JSON . stringify ( deepObject ) )
327+ } )
328+
329+ it ( 'handles arrays with mixed content' , ( ) => {
330+ const arrayError = [
331+ 'string' ,
332+ 42 ,
333+ { message : 'object in array' } ,
334+ null ,
335+ undefined ,
336+ ]
337+
338+ const result = exception . safeErrorSerialize ( arrayError )
339+
340+ expect ( result ) . to . eq ( JSON . stringify ( arrayError ) )
341+ } )
342+ } )
231343} )
0 commit comments