@@ -328,99 +328,109 @@ export function trustedReplaceArgument(
328328 let isMatchingSuspended = false ;
329329
330330 const applyWrapper = ( target : Function , thisArg : any , argumentsList : unknown [ ] ) => {
331- if ( isMatchingSuspended ) {
332- isMatchingSuspended = false ;
333- return Reflect . apply ( target , thisArg , argumentsList ) ;
334- }
335- isMatchingSuspended = true ;
331+ try {
332+ if ( isMatchingSuspended ) {
333+ return Reflect . apply ( target , thisArg , argumentsList ) ;
334+ }
335+ isMatchingSuspended = true ;
336336
337- // Log the original arguments before modification
338- if ( verbose === 'true' ) {
339- const formattedMessage = createFormattedMessage ( argumentsList ) ;
340- logMessage ( source , formattedMessage ) ;
341- }
337+ // Log the original arguments before modification
338+ if ( verbose === 'true' ) {
339+ const formattedMessage = createFormattedMessage ( argumentsList ) ;
340+ logMessage ( source , formattedMessage ) ;
341+ }
342342
343- // If we only need to log the arguments, skip further processing
344- if ( SHOULD_LOG_ONLY ) {
345- isMatchingSuspended = false ;
346- return Reflect . apply ( target , thisArg , argumentsList ) ;
347- }
343+ // If we only need to log the arguments, skip further processing
344+ if ( SHOULD_LOG_ONLY ) {
345+ isMatchingSuspended = false ;
346+ return Reflect . apply ( target , thisArg , argumentsList ) ;
347+ }
348348
349- const argumentToReplace = argumentsList [ Number ( argumentIndex ) ] ;
349+ const argumentToReplace = argumentsList [ Number ( argumentIndex ) ] ;
350350
351- const shouldSetArgument = checkArgument ( argumentToReplace ) ;
351+ const shouldSetArgument = checkArgument ( argumentToReplace ) ;
352352
353- if ( ! shouldSetArgument ) {
354- isMatchingSuspended = false ;
355- return Reflect . apply ( target , thisArg , argumentsList ) ;
356- }
353+ if ( ! shouldSetArgument ) {
354+ isMatchingSuspended = false ;
355+ return Reflect . apply ( target , thisArg , argumentsList ) ;
356+ }
357357
358- if ( typeof argumentToReplace === 'string' && shouldReplaceArgument ) {
359- argumentsList [ Number ( argumentIndex ) ] = argumentToReplace
360- . replace ( replaceRegexValue , constantValue as string ) ;
361- } else {
362- argumentsList [ Number ( argumentIndex ) ] = constantValue ;
363- }
358+ if ( typeof argumentToReplace === 'string' && shouldReplaceArgument ) {
359+ argumentsList [ Number ( argumentIndex ) ] = argumentToReplace
360+ . replace ( replaceRegexValue , constantValue as string ) ;
361+ } else {
362+ argumentsList [ Number ( argumentIndex ) ] = constantValue ;
363+ }
364364
365- // Log the modified arguments after replacement
366- if ( verbose === 'true' ) {
367- const formattedMessage = createFormattedMessage ( argumentsList , 'modified' ) ;
368- logMessage ( source , formattedMessage ) ;
369- }
365+ // Log the modified arguments after replacement
366+ if ( verbose === 'true' ) {
367+ const formattedMessage = createFormattedMessage ( argumentsList , 'modified' ) ;
368+ logMessage ( source , formattedMessage ) ;
369+ }
370370
371- hit ( source ) ;
371+ hit ( source ) ;
372372
373- isMatchingSuspended = false ;
373+ isMatchingSuspended = false ;
374374
375- return Reflect . apply ( target , thisArg , argumentsList ) ;
375+ return Reflect . apply ( target , thisArg , argumentsList ) ;
376+ } catch ( error ) {
377+ isMatchingSuspended = false ;
378+ logMessage ( source , `Unexpected error during argument replacement: ${ ( error as Error ) . message } ` ) ;
379+ return Reflect . apply ( target , thisArg , argumentsList ) ;
380+ }
376381 } ;
377382
378383 const constructWrapper = ( target : Function , argumentsList : unknown [ ] , newTarget : any ) => {
379- if ( isMatchingSuspended ) {
380- isMatchingSuspended = false ;
381- return Reflect . construct ( target , argumentsList , newTarget ) ;
382- }
383- isMatchingSuspended = true ;
384+ try {
385+ if ( isMatchingSuspended ) {
386+ return Reflect . construct ( target , argumentsList , newTarget ) ;
387+ }
388+ isMatchingSuspended = true ;
384389
385- // Log the original arguments before modification
386- if ( verbose === 'true' ) {
387- const formattedMessage = createFormattedMessage ( argumentsList ) ;
388- logMessage ( source , formattedMessage ) ;
389- }
390+ // Log the original arguments before modification
391+ if ( verbose === 'true' ) {
392+ const formattedMessage = createFormattedMessage ( argumentsList ) ;
393+ logMessage ( source , formattedMessage ) ;
394+ }
390395
391- // If we only need to log the arguments, skip further processing
392- if ( SHOULD_LOG_ONLY ) {
393- isMatchingSuspended = false ;
394- return Reflect . construct ( target , argumentsList , newTarget ) ;
395- }
396+ // If we only need to log the arguments, skip further processing
397+ if ( SHOULD_LOG_ONLY ) {
398+ isMatchingSuspended = false ;
399+ return Reflect . construct ( target , argumentsList , newTarget ) ;
400+ }
396401
397- const argumentToReplace = argumentsList [ Number ( argumentIndex ) ] ;
402+ const argumentToReplace = argumentsList [ Number ( argumentIndex ) ] ;
398403
399- const shouldSetArgument = checkArgument ( argumentToReplace ) ;
404+ const shouldSetArgument = checkArgument ( argumentToReplace ) ;
400405
401- if ( ! shouldSetArgument ) {
402- isMatchingSuspended = false ;
403- return Reflect . construct ( target , argumentsList , newTarget ) ;
404- }
406+ if ( ! shouldSetArgument ) {
407+ isMatchingSuspended = false ;
408+ return Reflect . construct ( target , argumentsList , newTarget ) ;
409+ }
405410
406- if ( typeof argumentToReplace === 'string' && shouldReplaceArgument ) {
407- argumentsList [ Number ( argumentIndex ) ] = argumentToReplace
408- . replace ( replaceRegexValue , constantValue as string ) ;
409- } else {
410- argumentsList [ Number ( argumentIndex ) ] = constantValue ;
411- }
411+ if ( typeof argumentToReplace === 'string' && shouldReplaceArgument ) {
412+ argumentsList [ Number ( argumentIndex ) ] = argumentToReplace
413+ . replace ( replaceRegexValue , constantValue as string ) ;
414+ } else {
415+ argumentsList [ Number ( argumentIndex ) ] = constantValue ;
416+ }
412417
413- // Log the modified arguments after replacement
414- if ( verbose === 'true' ) {
415- const formattedMessage = createFormattedMessage ( argumentsList , 'modified' ) ;
416- logMessage ( source , formattedMessage ) ;
417- }
418+ // Log the modified arguments after replacement
419+ if ( verbose === 'true' ) {
420+ const formattedMessage = createFormattedMessage ( argumentsList , 'modified' ) ;
421+ logMessage ( source , formattedMessage ) ;
422+ }
418423
419- hit ( source ) ;
424+ hit ( source ) ;
420425
421- isMatchingSuspended = false ;
426+ isMatchingSuspended = false ;
422427
423- return Reflect . construct ( target , argumentsList , newTarget ) ;
428+ return Reflect . construct ( target , argumentsList , newTarget ) ;
429+ } catch ( error ) {
430+ isMatchingSuspended = false ;
431+ logMessage ( source , `Unexpected error during argument replacement: ${ ( error as Error ) . message } ` ) ;
432+ return Reflect . construct ( target , argumentsList , newTarget ) ;
433+ }
424434 } ;
425435
426436 const getWrapper = ( target : Function , propName : string , receiver : any ) => {
0 commit comments