Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
fix(lambda.js): catch unhandled promise rejections (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnathaniel authored Jan 7, 2021
1 parent a9661c2 commit d06db15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/wrappers/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ function baseLambdaWrapper(
process._events.beforeExit = originalBeforeExit;
});
};
// catch an unhandled promise rejection within the user's code
process.once('unhandledRejection', (reason) => {
utils.debugLog('Unhandled Promise Rejection caught. Please handle the rejected promise');
const rejectedError = {
name: 'UnhandledPromiseRejectionError',
message: reason || '',
stack: '',
};
eventInterface.setException(runner, rejectedError, false);
});


const handleUserExecutionDone = (error, result, sendSync) => {
clearTimeout(timeoutHandler);
Expand Down
12 changes: 12 additions & 0 deletions test/unit_tests/wrappers/test_lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ describe('lambdaWrapper tests', () => {
done() {},
};
consts.COLD_START = true;
process.setMaxListeners(0);
});

afterEach(() => {
Expand Down Expand Up @@ -277,6 +278,17 @@ describe('lambdaWrapper tests', () => {
}, 1);
});

it('lambdaWrapper: catch unhandled rejected promise', (done) => {
this.stubFunction = sinon.spy(() => {
// eslint-disable-next-line prefer-promise-reject-errors,no-new
new Promise((_, reject) => reject('Unauthorized'));
return 'success';
});
this.wrappedStub = lambdaWrapper.lambdaWrapper(this.stubFunction);
expect(this.wrappedStub({}, null, this.callbackStub)).to.equal('success');
done();
});

it('lambdaWrapper: update COLD_START value', () => {
consts.COLD_START = true;
this.wrappedStub({}, this.context, this.callbackStub);
Expand Down

0 comments on commit d06db15

Please sign in to comment.