Skip to content

Commit 2111a8f

Browse files
committed
Fix error handling if lambda throws while launching
1 parent 957d2e6 commit 2111a8f

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

lib/lambda.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,13 @@ exports.create = function (base_options = {}) {
381381
);
382382
} catch (e) {
383383
log.error('Failed to launch Lambda', { name, base_dir }, e);
384-
// eslint-disable-next-line node/no-callback-literal
385-
callback(`{"message":"Failed to launch \\"${name}\\""}`);
386-
return;
384+
const message = `{"message":"Failed to launch \\"${name}\\""}`;
385+
if (callback) {
386+
callback(message);
387+
return;
388+
}
389+
// eslint-disable-next-line consistent-return
390+
return Promise.reject(message);
387391
}
388392
}
389393
if (!callback) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"environment": {
3+
"STUDIO_ENV_VAR": "Hello",
4+
"STUDIO_ENV_TPL": "${STUDIO_ENVIRONMENT_VARIABLE}"
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
exports.handle = function () {
4+
return new Promise((resolve, reject) => {
5+
setTimeout(() => {
6+
// eslint-disable-next-line prefer-promise-reject-errors
7+
reject(`${process.env.STUDIO_ENV_VAR} ${process.env.STUDIO_ENV_TPL}`);
8+
}, 1);
9+
});
10+
};

test/lambda-test.js

+17
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,23 @@ describe('lambda', () => {
355355
});
356356
});
357357

358+
it('fails to launch async lambda if environment variable is missing', async () => {
359+
lambda = Lambda.create({
360+
env: {
361+
AWS_PROFILE: 'local'
362+
}
363+
});
364+
365+
const promise = lambda.invoke('env-file-async');
366+
367+
await assert.rejects(
368+
promise,
369+
match.json({
370+
message: 'Failed to launch "env-file-async"'
371+
})
372+
);
373+
});
374+
358375
it('invokes lambda with DEBUG environment variables', (done) => {
359376
process.env.DEBUG = 'ON';
360377

0 commit comments

Comments
 (0)