@@ -87,9 +87,10 @@ export class Ignite extends Macroable {
8787 bootLogs : true ,
8888 shutdownLogs : true ,
8989 environments : [ ] ,
90+ exitOnError : true ,
9091 loadConfigSafe : true ,
9192 athennaRcPath : './.athennarc.json' ,
92- uncaughtExceptionHandler : this . handleError
93+ uncaughtExceptionHandler : this . handleUncaughtError
9394 } )
9495
9596 this . setUncaughtExceptionHandler ( )
@@ -528,25 +529,62 @@ export class Ignite extends Macroable {
528529 if ( process . versions . bun || ( ! Is . Error ( error ) && ! Is . Exception ( error ) ) ) {
529530 console . error ( error )
530531
531- /**
532- * Return is needed only for testing purposes.
533- */
534- return process . exit ( 1 )
532+ return this . safeExit ( 1 )
535533 }
536534
537535 if ( ! Is . Exception ( error ) ) {
538536 error = error . toAthennaException ( )
539537 }
540538
539+ error . details . push ( { isUncaughtError : false } )
540+
541541 if ( Config . is ( 'app.logger.prettifyException' , true ) ) {
542542 await Log . channelOrVanilla ( 'exception' ) . fatal ( await error . prettify ( ) )
543543
544- process . exit ( 1 )
544+ return this . safeExit ( 1 )
545545 }
546546
547547 await Log . channelOrVanilla ( 'exception' ) . fatal ( error )
548548
549- process . exit ( 1 )
549+ return this . safeExit ( 1 )
550+ }
551+
552+ /**
553+ * Handle an uncaught error turning it pretty and logging as fatal.
554+ */
555+ public async handleUncaughtError ( error : any ) {
556+ if ( process . versions . bun || ( ! Is . Error ( error ) && ! Is . Exception ( error ) ) ) {
557+ console . error ( error )
558+
559+ return this . safeExit ( 1 )
560+ }
561+
562+ if ( ! Is . Exception ( error ) ) {
563+ error = error . toAthennaException ( )
564+ }
565+
566+ error . details . push ( { isUncaughtError : true } )
567+
568+ if ( Config . is ( 'app.logger.prettifyException' , true ) ) {
569+ await Log . channelOrVanilla ( 'exception' ) . fatal ( await error . prettify ( ) )
570+
571+ return this . safeExit ( 1 )
572+ }
573+
574+ await Log . channelOrVanilla ( 'exception' ) . fatal ( error )
575+
576+ return this . safeExit ( 1 )
577+ }
578+
579+ /**
580+ * Exit the application only if the exitOnError option is true.
581+ */
582+ private safeExit ( code : number ) : void {
583+ if ( ! this . options . exitOnError ) {
584+ return
585+ }
586+
587+ process . exit ( code )
550588 }
551589
552590 /**
0 commit comments