@@ -345,7 +345,7 @@ export class InstanceController {
345345 } ;
346346 } catch ( error ) {
347347 this . logger . error ( error ) ;
348- return { error : true , message : error . toString ( ) } ;
348+ return { error : true , message : error ?. message ?? String ( error ) } ;
349349 }
350350 }
351351
@@ -392,7 +392,7 @@ export class InstanceController {
392392 } ;
393393 } catch ( error ) {
394394 this . logger . error ( error ) ;
395- return { error : true , message : error . toString ( ) } ;
395+ return { error : true , message : error ?. message ?? String ( error ) } ;
396396 }
397397 }
398398
@@ -453,47 +453,41 @@ export class InstanceController {
453453
454454 return { status : 'SUCCESS' , error : false , response : { message : 'Instance logged out' } } ;
455455 } catch ( error ) {
456- throw new InternalServerErrorException ( error . toString ( ) ) ;
456+ throw new InternalServerErrorException ( error ?. message ?? String ( error ) ) ;
457457 }
458458 }
459459
460460 public async deleteInstance ( { instanceName } : InstanceDto ) {
461461 const { instance } = await this . connectionState ( { instanceName } ) ;
462+
463+ const waInstances = this . waMonitor . waInstances [ instanceName ] ;
464+
462465 try {
463- const waInstances = this . waMonitor . waInstances [ instanceName ] ;
464466 if ( this . configService . get < Chatwoot > ( 'CHATWOOT' ) . ENABLED ) waInstances ?. clearCacheChatwoot ( ) ;
467+ } catch ( error ) {
468+ this . logger . warn ( `clearCacheChatwoot failed for "${ instanceName } ": ${ error ?. message ?? String ( error ) } ` ) ;
469+ }
465470
466- if ( instance . state === 'connecting' || instance . state === 'open' ) {
467- try {
468- await this . logout ( { instanceName } ) ;
469- } catch ( error ) {
470- // logout can throw "Connection Closed" when the underlying Baileys
471- // socket is already dead but waInstances[name] still exists. We
472- // must continue to the remove.instance emit below — that is the
473- // only path that purges the in-memory entry and runs cleaningUp().
474- // Without this catch, the stale entry persists until the entire
475- // process restarts.
476- this . logger . warn ( {
477- message : 'logout failed during deleteInstance — proceeding with cleanup' ,
478- instanceName,
479- error,
480- } ) ;
481- }
482- }
483-
471+ if ( instance . state === 'connecting' || instance . state === 'open' ) {
484472 try {
485- waInstances ?. sendDataWebhook ( Events . INSTANCE_DELETE , {
486- instanceName,
487- instanceId : waInstances . instanceId ,
488- } ) ;
473+ await this . logout ( { instanceName } ) ;
489474 } catch ( error ) {
490- this . logger . error ( error ) ;
475+ this . logger . warn (
476+ `Logout failed for "${ instanceName } ": ${ error ?. message ?? String ( error ) } . Continuing cleanup.` ,
477+ ) ;
491478 }
479+ }
492480
493- this . eventEmitter . emit ( 'remove.instance' , instanceName , 'inner' ) ;
494- return { status : 'SUCCESS' , error : false , response : { message : 'Instance deleted' } } ;
481+ try {
482+ waInstances ?. sendDataWebhook ( Events . INSTANCE_DELETE , {
483+ instanceName,
484+ instanceId : waInstances ?. instanceId ,
485+ } ) ;
495486 } catch ( error ) {
496- throw new BadRequestException ( error . toString ( ) ) ;
487+ this . logger . error ( error ) ;
497488 }
489+
490+ this . eventEmitter . emit ( 'remove.instance' , instanceName , 'inner' ) ;
491+ return { status : 'SUCCESS' , error : false , response : { message : 'Instance deleted' } } ;
498492 }
499493}
0 commit comments