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

Type-safe HardhatError #5283

Merged
merged 10 commits into from
May 29, 2024
Merged

Type-safe HardhatError #5283

merged 10 commits into from
May 29, 2024

Conversation

alcuadrado
Copy link
Member

This PR modifies HardhatError so that its constructor and its messageArguments property are type-safe.

This introduces two extra changes:

  1. The error message template system now uses {variable} instead of %variable%, as that dramatically simplified this PR.
  2. The constructor only takes two parameters now if the message template doesn't use any variable. Those are the descriptor and the optional cause.

Copy link

vercel bot commented May 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hardhat ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 29, 2024 8:06pm

Copy link

changeset-bot bot commented May 28, 2024

⚠️ No Changeset found

Latest commit: 409dc51

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added the status:ready This issue is ready to be worked on label May 28, 2024
@alcuadrado alcuadrado force-pushed the hardhat-error-types branch from a495ad9 to cc7b887 Compare May 28, 2024 19:13
@alcuadrado alcuadrado requested a review from schaable May 28, 2024 19:13

template = template.replaceAll(variableTag, value);
}
if (Array.isArray(rawValue)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@schaable when merging this conflict I realized that the hasOwnProperty method didn't allow for classes to define their toString, so I switched it to explicitly handling arrays.

@alcuadrado
Copy link
Member Author

It should be easiear to review this one commit at the time. The 4th and 6th commits are just automatic string replacements.

Copy link
Member

@kanej kanej left a comment

Choose a reason for hiding this comment

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

I left a few questions for my own understanding, but pre-approving.

string,
ErrorMessageTemplateValue
>;
export type MessagetTemplateArguments<MessageTemplateT extends string> =
Copy link
Member

Choose a reason for hiding this comment

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

really nice 💯

for (const variableName of Object.keys(values)) {
let value: string;

return template.replaceAll(/{(.*?)}/g, (_match, variableName) => {
Copy link
Member

Choose a reason for hiding this comment

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

should this match the empty group {}?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! But after giving it a few minutes, I think it's better if it matches the behavior of the types. To avoid matching the empty group, the types will be significantly more complex.

I'll add a test for this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the tests in 409dc51

HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR,
);
});
describe("MessagetTemplateArguments type", () => {
Copy link
Member

@schaable schaable May 29, 2024

Choose a reason for hiding this comment

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

this is a nice example of type testing

Copy link
Member

@schaable schaable left a comment

Choose a reason for hiding this comment

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

LGTM, just a question regarding the regex

@alcuadrado alcuadrado force-pushed the hardhat-error-types branch from b01ff52 to 409dc51 Compare May 29, 2024 20:05
@alcuadrado alcuadrado merged commit e801ad2 into v-next May 29, 2024
41 checks passed
@alcuadrado alcuadrado deleted the hardhat-error-types branch May 29, 2024 20:09
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status:ready This issue is ready to be worked on
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants