Skip to content

codegen: improve "could not find field" error message #233

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

Closed
wants to merge 2 commits into from

Conversation

fnune
Copy link

@fnune fnune commented Mar 31, 2019

#97

I've considered adding newlines in between but I don't know if there's any existing convention that would conflict with that.

@fnune
Copy link
Author

fnune commented Mar 31, 2019

query RepoView($owner: String!, $name: String!) {
  bananas
  repository(owner: $owner, name: $name) {

// Etc
error: proc-macro derive panicked
  --> src/main.rs:23:10
   |
23 | #[derive(GraphQLQuery)]
   |          ^^^^^^^^^^^^
   |
   = help: message: Code generation failed.
           Could not find field `bananas`. Available fields: `__typename, codeOfConduct, codesOfConduct, license, licenses, marketplaceCategories, marketplaceCategory, marketplaceListing, marketplaceListings, meta, node, nodes, organization, rateLimit, relay, repository, repositoryOwner, resource, search, topic, user, viewer`.    

.map(|ref field| &field.name)
.fold(String::new(), |mut acc, item| {
acc.push_str(item);
acc.push_str(", ");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think Itertools::format would just be better.

fields.iter().map(|ref field| &field.name).format("`, `")

This keeps the field names enclosed in "`", but leaves the commas without markup.

@fnune
Copy link
Author

fnune commented Apr 2, 2019

That does look better. Added as an additional commit so it can be reviewed separately.

format_err!(
"Could not find field `{}`. Available fields: `{}`.",
name,
format_available_fields(fields).as_str()
Copy link
Contributor

Choose a reason for hiding this comment

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

This as_str seems superfluous now (same below).

Copy link
Author

Choose a reason for hiding this comment

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

Right - thanks!

@fnune fnune force-pushed the improve-error-messages branch from e81b892 to 4523ede Compare April 4, 2019 19:34
@tomhoule
Copy link
Member

Looks good! We discussed the iter formatting issue on another channel - we should address that concern, rebase and then let's merge this.

@fnune
Copy link
Author

fnune commented Apr 20, 2019

So I should rebase and then do the thing without itertools?

@tomhoule
Copy link
Member

yep - for the itertools part, you can implement a Display helper, something like:

struct CommaSeparated<T>(T);

impl<A, B> std::fmt::Display for CommaSeparated<A>
where A: Iter<Item=B>,
  B: Display {
 ....
}

@tomhoule
Copy link
Member

If it's too hard we can think about keeping the itertools dependency.

@tomhoule
Copy link
Member

@fnune the codebase has changed quite a bit since this PR was opened :)

@tomhoule tomhoule closed this Jun 28, 2021
@mathstuf
Copy link
Contributor

mathstuf commented Feb 2, 2025

Is this something we should revisit? Improving error messages is almost always a good thing IMO.

@tomhoule
Copy link
Member

tomhoule commented Feb 3, 2025

Is this something we should revisit? Improving error messages is almost always a good thing IMO.

yeah definitely. We could have an issue so we don't forget

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.

3 participants