Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #3069 - Failing to handle schedule event body params due to f… #5268

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/plugins/aws/package/compile/events/schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ class AwsCompileScheduledEvents {
}

if (Input && typeof Input === 'object') {
if (_.has(Input, 'body') && typeof Input.body === 'string') {
try {
Input.body = JSON.parse(Input.body);
} catch (error) {
const errorMessage = [
'The body of the schedule event associated with',
` ${functionName} was passed as a string`,
' but it failed to parse to a JSON object.',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}
}
Input = JSON.stringify(Input);
}
if (Input && typeof Input === 'string') {
Expand Down
40 changes: 40 additions & 0 deletions lib/plugins/aws/package/compile/events/schedule/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,46 @@ describe('AwsCompileScheduledEvents', () => {
expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});

it('should not throw an error when Input body is a valid JSON string', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: {
rate: 'rate(10 minutes)',
enabled: false,
input: {
body: '{ "functionId": "..." }',
},
},
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).not.to.throw(Error);
});

it('should throw an error when Input body is an invalid JSON string', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
events: [
{
schedule: {
rate: 'rate(10 minutes)',
enabled: false,
input: {
body: 'an invalid input body',
},
},
},
],
},
};

expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error);
});

it('should not create corresponding resources when scheduled events are not given', () => {
awsCompileScheduledEvents.serverless.service.functions = {
first: {
Expand Down