Skip to content

Commit

Permalink
fix(amplify-nodejs-function-runtime-provider): fix lambda error format
Browse files Browse the repository at this point in the history
return proper error message and stack if a string error is provided return string error as is

fix aws-amplify#5553
  • Loading branch information
SwaySway committed Jan 9, 2021
1 parent 8486632 commit cb4d8ea
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,8 @@ exports[`execute callback handler should fail when returning an error 1`] = `[Er

exports[`execute callback handler should succeed with a returned value 1`] = `"foo"`;

exports[`execute failed execution should return the proper error 1`] = `[ReferenceError: undedvar is not defined]`;

exports[`execute failed execution should return the string error 1`] = `"string"`;

exports[`execute non-promise returned handler should resolve to null 1`] = `null`;
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,28 @@ describe('execute', () => {
).resolves.toMatchSnapshot();
});
});

describe('failed execution', () => {
it('should return the proper error', async () => {
await expect(
invokeFunction({
packageFolder: __dirname,
handler: 'handlers.undefinedVariableHandler',
event: '{}',
environment: {},
}),
).rejects.toMatchSnapshot();
});

it('should return the string error', async () => {
await expect(
invokeFunction({
packageFolder: __dirname,
handler: 'handlers.stringErrorHandler',
event: '{}',
environment: {},
}),
).rejects.toMatchSnapshot();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ module.exports.callbackHandler = async (event, context, callback) => {
module.exports.nonAsyncHandler = () => {
return 'foo';
};

module.exports.undefinedVariableHandler = (event, context, callback) => {
console.log(undedvar);
};

module.exports.stringErrorHandler = (event, context, callback) => {
throw 'string';
};
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,12 @@ process.on('message', async options => {
process.stdout.write('\n');
process.stdout.write(JSON.stringify({ result, error: null }));
} catch (error) {
let lambdaError = typeof error === 'string' ? { message: 'Unkown Error' } : { message: error.message, stack: error.stack };
process.stdout.write('\n');
process.stdout.write(
JSON.stringify({
result: null,
error: {
type: 'Lambda:Unhandled',
message: error.message || typeof error === 'string' ? error : 'Unknown error', // In case of callback('error'), it is only a string
},
error: { type: 'Lambda:Unhandled', ...lambdaError },
}),
);
}
Expand Down

0 comments on commit cb4d8ea

Please sign in to comment.