@@ -4,6 +4,13 @@ import * as os from 'os';
44
55export class CrashLogger {
66 private static logFilePath = path . join ( process . cwd ( ) , 'crash.log' ) ;
7+ private static globalHandlersInstalled = false ;
8+
9+ static isIgnorableProcessWriteError ( error : unknown ) : boolean {
10+ const code = typeof error === 'object' && error ? String ( ( error as { code ?: unknown } ) . code || '' ) : '' ;
11+ const syscall = typeof error === 'object' && error ? String ( ( error as { syscall ?: unknown } ) . syscall || '' ) : '' ;
12+ return code === 'EPIPE' || code === 'ERR_STREAM_DESTROYED' || ( code === 'EOF' && syscall === 'write' ) ;
13+ }
714
815 static log ( error : any , context : string = 'General' ) {
916 const timestamp = new Date ( ) . toISOString ( ) ;
@@ -23,13 +30,24 @@ export class CrashLogger {
2330 }
2431
2532 static initGlobalHandlers ( ) {
33+ if ( this . globalHandlersInstalled ) {
34+ return ;
35+ }
36+ this . globalHandlersInstalled = true ;
37+
2638 process . on ( 'uncaughtException' , ( error ) => {
39+ if ( CrashLogger . isIgnorableProcessWriteError ( error ) ) {
40+ return ;
41+ }
2742 console . error ( 'Uncaught Exception:' , error ) ;
2843 CrashLogger . log ( error , 'UncaughtException' ) ;
2944 process . exit ( 1 ) ; // Exit is mandatory for uncaught exceptions to avoid undefined state
3045 } ) ;
3146
3247 process . on ( 'unhandledRejection' , ( reason , promise ) => {
48+ if ( CrashLogger . isIgnorableProcessWriteError ( reason ) ) {
49+ return ;
50+ }
3351 console . error ( 'Unhandled Rejection at:' , promise , 'reason:' , reason ) ;
3452 CrashLogger . log ( reason , 'UnhandledRejection' ) ;
3553 } ) ;
0 commit comments