Skip to content

fix(core): Duration.toString() throws an error#18243

Merged
mergify[bot] merged 5 commits intomasterfrom
huijbers/duration-tostring
Jan 3, 2022
Merged

fix(core): Duration.toString() throws an error#18243
mergify[bot] merged 5 commits intomasterfrom
huijbers/duration-tostring

Conversation

@rix0rrr
Copy link
Copy Markdown
Contributor

@rix0rrr rix0rrr commented Jan 3, 2022

Duration.toString() was intended to produce a value that would throw an exception when resolved,
but unintentionally was written to always throw immediately (the reason it was throwing is that Token.asString()
doesn't accept functions, it only accepts data values--Lazy.string() should have been used).

Instead, we remove the validation completely. toString() now produces a meaningless string, and users
should avoid using the Duration object in a context where it will be implicitly converted to a string.

Fixes #18176.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

`Duration.toString()` shouldn't throw an exception directly--instead, it
should produce an encoded token that throws when resolved.

The reason it was throwing is that `Token.asString()` doesn't accept
functions, it only accepts data values. Use `Lazy.string()` instead.

Fixes #18176.
@rix0rrr rix0rrr requested a review from a team January 3, 2022 10:13
@rix0rrr rix0rrr self-assigned this Jan 3, 2022
@gitpod-io
Copy link
Copy Markdown

gitpod-io bot commented Jan 3, 2022

@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Jan 3, 2022
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jan 3, 2022
});

test('can stringify but not resolve', () => {
const stringified = `${Duration.hours(1)}`;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it makes more sense to simply return a string representation of the duration (eg "1 hour" or even just ״<duration>").

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are saying get rid of the misuse protection because it's not worth it, right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yap

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@rix0rrr rix0rrr requested a review from a team January 3, 2022 10:38
@rix0rrr rix0rrr requested a review from eladb January 3, 2022 12:34
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Jan 3, 2022

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 3220584
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit df03df8 into master Jan 3, 2022
@mergify mergify bot deleted the huijbers/duration-tostring branch January 3, 2022 15:12
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Jan 3, 2022

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
`Duration.toString()` was intended to produce a value that would throw an exception when resolved,
but unintentionally was written to always throw immediately (the reason it was throwing is that `Token.asString()`
doesn't accept functions, it only accepts data values--`Lazy.string()` should have been used).

Instead, we remove the validation completely. `toString()` now produces a meaningless string, and users
should avoid using the `Duration` object in a context where it will be implicitly converted to a string.

Fixes aws#18176.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

@aws-cdk/core Related to core CDK functionality contribution/core This is a PR that came from AWS.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(core): Duration.toString always throws

3 participants