Log Unhandled rejection stack (#113614)

* Refactor state types

* Log the stack trace of unhandled promise rejections

* Revert "Refactor state types"

This reverts commit 6a5123a1b2.

* Fix types

* code review feedback
This commit is contained in:
Rudolf Meijering 2021-10-11 23:50:35 +02:00 committed by GitHub
parent a03aa7b7fa
commit f88901c6ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View file

@ -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()', () => {

View file

@ -54,9 +54,10 @@ export class EnvironmentService {
this.configService.atPath<PidConfigType>(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) => {