ProcessErrors.ts
1 import { ServerLog } from '../logger/Logger' 2 import { memoryUsage } from 'node:process' 3 import { ConfigManager } from '../config/ConfigManager' 4 5 export const buildInformation = process.env.BUILD_INFO ?? 'Lineage2TS Custom Build' 6 export const buildHash = process.env.BUILD_HASH ?? undefined 7 8 function getUptime(): string { 9 const messageChunks: Array<string> = [] 10 let seconds = process.uptime() 11 12 if ( seconds > 3600 ) { 13 messageChunks.push( `${ Math.floor( seconds / 3600 ) } hours` ) 14 seconds = seconds % 3600 15 } 16 17 if ( seconds > 60 ) { 18 messageChunks.push( `${ Math.floor( seconds / 60 ) } minutes` ) 19 } 20 21 if ( messageChunks.length === 0 ) { 22 return `${ Math.floor( seconds ) } seconds` 23 } 24 25 return messageChunks.join( ', ' ) 26 } 27 28 function getMemoryUsage() : string { 29 return `${Math.floor( memoryUsage.rss() / 1048576 ).toString()} MiB` 30 } 31 32 export function processInfo() : string { 33 return `running for ${getUptime()}, using ${getMemoryUsage()}` 34 } 35 36 export function processRuntime() : string { 37 return `${process.version} - ${process.platform}` 38 } 39 40 let assetsInfo : string 41 42 export function generateErrorFields( database: string ) : void { 43 assetsInfo = `database: ${ConfigManager.database.getEngine()} v${database}` 44 } 45 46 function getDefaultErrorFields() : Record<string, string> { 47 return { 48 buildInformation, 49 buildHash, 50 process: processInfo(), 51 runtime: processRuntime(), 52 assets: assetsInfo 53 } 54 } 55 56 export function setupProcessErrorHandling() : void { 57 process.on( 'unhandledRejection', ( error: unknown ) : void => { 58 ServerLog.error( { 59 ...getDefaultErrorFields(), 60 error, 61 }, 'Unhandled Promise rejection' ) 62 } ) 63 64 process.on( 'uncaughtException', ( error: Error ) : void => { 65 ServerLog.fatal( { 66 ...getDefaultErrorFields(), 67 error, 68 }, 'Uncaught exception' ) 69 } ) 70 71 process.on( 'warning', ( error: Error ) : void => { 72 ServerLog.warn( { 73 ...getDefaultErrorFields(), 74 error, 75 }, 'Runtime warning' ) 76 } ) 77 }