Skip to content

Commit ab84859

Browse files
committed
add logging for unhandled errors
1 parent 95adb9d commit ab84859

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/server.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ import { logger } from './utils/logger';
77
import { initPassport } from './middleware/passport-auth';
88
import { dbManager } from './db/database-manager';
99

10+
// Without these, an unawaited promise rejection (e.g. an abandoned pg pool acquisition)
11+
// terminates the process under Node's default --unhandled-rejections=throw, with no log
12+
// line identifying the offending call site. Log and stay alive for rejections; log and
13+
// exit for uncaught exceptions since the process state after one is undefined.
14+
process.on('unhandledRejection', (reason: unknown, promise: Promise<unknown>) => {
15+
logger.error({ err: reason, promise }, 'Unhandled promise rejection');
16+
});
17+
18+
process.on('uncaughtException', (err: Error, origin: string) => {
19+
logger.fatal({ err, origin }, 'Uncaught exception, process will exit');
20+
setTimeout(() => process.exit(1), 100).unref();
21+
});
22+
1023
const PORT = config.backend.port;
1124

1225
Promise.resolve()

0 commit comments

Comments
 (0)