Skip to content

Commit

Permalink
Log Unhandled rejection stack (#113614)
Browse files Browse the repository at this point in the history
* Refactor state types

* Log the stack trace of unhandled promise rejections

* Revert "Refactor state types"

This reverts commit 6a5123a.

* Fix types

* code review feedback
  • Loading branch information
rudolf authored Oct 11, 2021
1 parent a03aa7b commit f88901c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/core/server/environment/environment_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/core/server/environment/environment_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit f88901c

Please sign in to comment.