diff --git a/src/core/server/environment/environment_service.test.ts b/src/core/server/environment/environment_service.test.ts index 34647d090b995..4b074482248b4 100644 --- a/src/core/server/environment/environment_service.test.ts +++ b/src/core/server/environment/environment_service.test.ts @@ -135,6 +135,32 @@ describe('UuidService', () => { expect(logger.get('process').warn).not.toHaveBeenCalled(); }); }); + + describe('unhandledRejection warnings', () => { + it('logs warn for an unhandeld promise rejected with an Error', async () => { + await service.preboot(); + + const err = new Error('something went wrong'); + process.emit('unhandledRejection', err, new Promise((res, rej) => rej(err))); + + expect(logger.get('process').warn).toHaveBeenCalledTimes(1); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch( + /Detected an unhandled Promise rejection: Error: something went wrong\n.*at / + ); + }); + + it('logs warn for an unhandeld promise rejected with a string', async () => { + await service.preboot(); + + const err = 'something went wrong'; + process.emit('unhandledRejection', err, new Promise((res, rej) => rej(err))); + + expect(logger.get('process').warn).toHaveBeenCalledTimes(1); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch( + /Detected an unhandled Promise rejection: "something went wrong"/ + ); + }); + }); }); describe('#setup()', () => { diff --git a/src/core/server/environment/environment_service.ts b/src/core/server/environment/environment_service.ts index f96b616256577..472883c1f482c 100644 --- a/src/core/server/environment/environment_service.ts +++ b/src/core/server/environment/environment_service.ts @@ -54,9 +54,10 @@ export class EnvironmentService { this.configService.atPath(pidConfigDef.path).pipe(take(1)).toPromise(), ]); - // was present in the legacy `pid` file. + // Log unhandled rejections so that we can fix them in preparation for https://github.com/elastic/kibana/issues/77469 process.on('unhandledRejection', (reason) => { - this.log.warn(`Detected an unhandled Promise rejection.\n${reason}`); + const message = (reason as Error)?.stack ?? JSON.stringify(reason); + this.log.warn(`Detected an unhandled Promise rejection: ${message}`); }); process.on('warning', (warning) => {