backend-app-api: register global error handlers

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-03-20 10:42:28 +01:00
parent db45887ad0
commit 8cce2205a3
2 changed files with 24 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-app-api': patch
---
Register unhandled rejection and uncaught exception handlers to avoid backend crashes.
@@ -165,6 +165,25 @@ export class BackendInitializer {
);
await registerInit.init.func(deps);
}
// Once the backend is started, any uncaught errors or unhandled rejections are caught
// and logged, in order to avoid crashing the entire backend on local failures.
if (process.env.NODE_ENV !== 'test') {
const rootLogger = await this.#serviceHolder.get(
coreServices.rootLogger,
'root',
);
process.on('unhandledRejection', (reason: Error) => {
rootLogger
?.child({ type: 'unhandledRejection' })
?.error('Unhandled rejection', reason);
});
process.on('uncaughtException', error => {
rootLogger
?.child({ type: 'uncaughtException' })
?.error('Uncaught exception', error);
});
}
}
#resolveInitOrder(registerInits: Array<BackendRegisterInit>) {