Skip to content

Commit 9d08774

Browse files
committed
fix: add validation
1 parent 0174c8b commit 9d08774

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

packages/aws-cdk-lib/aws-synthetics/lib/canary.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,11 @@ export class Canary extends cdk.Resource implements ec2.IConnectable {
571571

572572
let timeoutInSeconds: number | undefined;
573573
if (!cdk.Token.isUnresolved(props.timeout) && props.timeout !== undefined) {
574+
const timeoutInMillis = props.timeout?.toMilliseconds();
575+
if (timeoutInMillis % 1000 !== 0) {
576+
throw new Error(`\`timeout\` must be set as an integer representing seconds, got ${timeoutInMillis} milliseconds.`);
577+
}
578+
574579
timeoutInSeconds = props.timeout.toSeconds();
575580
if (timeoutInSeconds < 3 || timeoutInSeconds > 840) {
576581
throw new Error(`\`timeout\` must be between 3 seconds and 840 seconds, got ${timeoutInSeconds} seconds.`);

packages/aws-cdk-lib/aws-synthetics/test/canary.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,22 @@ test('timeout can be set', () => {
396396
});
397397
});
398398

399+
test.each([100, 3100])('throws when timeout is not set as an integer representing seconds , %d milliseconds', (milliseconds: number) => {
400+
// GIVEN
401+
const stack = new Stack();
402+
403+
// WHEN
404+
expect(() => new synthetics.Canary(stack, 'Canary', {
405+
runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_7_0,
406+
test: synthetics.Test.custom({
407+
handler: 'index.handler',
408+
code: synthetics.Code.fromInline('/* Synthetics handler code */'),
409+
}),
410+
timeout: Duration.millis(milliseconds),
411+
}))
412+
.toThrow(`\`timeout\` must be set as an integer representing seconds, got ${milliseconds} milliseconds.`);
413+
});
414+
399415
test.each([2, 900])('throws when timeout is out of range, %d seconds', (seconds: number) => {
400416
// GIVEN
401417
const stack = new Stack();

0 commit comments

Comments
 (0)