Skip to content

Improve error messages when contract has missing/invalid type or missing name#255

Open
federicobond wants to merge 6 commits into
seddonym:mainfrom
federicobond:improve-errors
Open

Improve error messages when contract has missing/invalid type or missing name#255
federicobond wants to merge 6 commits into
seddonym:mainfrom
federicobond:improve-errors

Conversation

@federicobond

Copy link
Copy Markdown

Each change is implemented in a separate commit for easier review. Let me know what you think.

When the contract type is missing from options, the error message now becomes:

Contract "My Contract" is not configured correctly:
    type: Unable to find the 'type' key in the contract options.

When the contract type is not known or registered, the error message now becomes:

Contract "My Contract" is not configured correctly:
    type: Unknown contract type: my-contract-type

When the contract has no configured name, it will now use the contract id as a fallback:

Contract "2" is not configured correctly:
    type: Unknown contract type: my-contract-type

@seddonym
seddonym self-requested a review March 5, 2025 12:59

@seddonym seddonym left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for looking at this - the error reporting is confusing and this is much better!

Currently tests are failing though - and generally it would be good to have test coverage for the change.

We'll also need to figure out what to do about TOML contracts.

try:
return registry.get_contract_class(contract_options["type"])
except NoSuchContractType as e:
raise InvalidContractOptions({"type": f"Uknown contract type '{e}'."})

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Typo "Uknown"

try:
return registry.get_contract_class(contract_options["type"])
except NoSuchContractType as e:
raise InvalidContractOptions({"type": f"Uknown contract type '{e}'."})

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Probably better to use contract_options["type"] here rather than rely on the message in the exception?

)
for contract_options in contracts_options:
contract_class = registry.get_contract_class(contract_options["type"])
contract_name = contract_options.get("name", contract_options['id'])

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Unfortunately in TOML files we won't necessarily have an "id" set.

Maybe we could have a second level of fallback which just displays the index of the contract in contracts_options, e.g. "Contract 2 is not configured correctly"?

I'd be open to requiring IDs to be passed in TOML contracts, as it would be useful to be able to rely on an identifier - but that's a breaking change so let's keep it out of this PR!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

What makes it a bit harder is that we are not iterating over contracts_options but rather the filtered list, so the indices may not match what's in the file. Would it make sense to assign the index as id by default in case there is none when reading the options from the config?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

That's a good idea - yes we could assign it earlier on.

If we do this, it might be worth adding id as an optional parameter to the TOML format, and documenting it, what do you think?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not sure I follow, is id not supported now in the TOML format?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

In the TOML format, you can just add contracts like so (no id needed).

https://import-linter.readthedocs.io/en/stable/toml.html

@federicobond

Copy link
Copy Markdown
Author

Tests should be passing now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants