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

[Content collections] Better error formatting #6508

Merged
merged 13 commits into from
Mar 13, 2023
Merged

Conversation

bholmesdev
Copy link
Contributor

Changes

This fixes a few problems in today's error formatter:

  • The collection -> entry path is bolded for visibility
  • Frontmatter keys are now always placed at the start of an error message. This fixes inconsistencies where the key name could be at the start of the end of a message.
  • Union errors now log "Did not match union" instead of "Invalid input." Will also log "type" and "invalid literal" errors for keys shared by all members of a union.
  • Unhandled errors now correctly fall through to Zod's error message. This would log "Invalid Input" for all unhandled messages before.

Example: a union between { key: z.literal('tutorial') } and { key: z.literal('blog') } will raise a single error when key does not match:

Did not match union.
> key: Expected `'tutorial' | 'blog'`, received 'foo'

Before / after

Here is a before and after of the error experience when authoring in docs.astro.build. These errors are based on the union schema configured here.

Example invalid frontmatter:

---
type: guide
i18nReady: maybe?
---

Before

image

After

image

Testing

  • Unit test error map

Docs

  • Small change to errors data: formatting and slight message change

@bholmesdev bholmesdev requested a review from a team as a code owner March 10, 2023 21:03
@changeset-bot
Copy link

changeset-bot bot commented Mar 10, 2023

🦋 Changeset detected

Latest commit: 13887e0

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Mar 10, 2023
@jasikpark
Copy link
Contributor

Curious if there's anything to upstream / open source as a general solution once this merges 😁 looking amazing

@@ -771,7 +771,7 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati
code: 9001,
message: (collection: string, entryId: string, error: ZodError) => {
return [
`${String(collection)} → ${String(entryId)} frontmatter does not match collection schema.`,
`**${String(collection)} → ${String(entryId)}** frontmatter is invalid.`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Flagging for docs review @sarah11918 👀

Copy link
Member

Choose a reason for hiding this comment

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

@bholmesdev I love the change overall! And, I know you wanted something shorter here, but I don't love "invalid" vs "does not match what you said you wanted here." (which is why it's invalid)

If you feel the need to shorten, something like ...

"unexpected frontmatter" feels a little more user friendly?

Your call! Non-blocking!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood! Didn't feel too strongly about this change. I think I'll revert to the old wording, with the bolding added 👍

@bholmesdev
Copy link
Contributor Author

@jasikpark Already want to bring this to our RSS config handler! Also throws a big ole Invalid input for some of the types.

Copy link
Contributor

@jasikpark jasikpark left a comment

Choose a reason for hiding this comment

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

this is CLEAN 👏 quick glance over looks great to me, though I haven't tested or thought through the logic

@jasikpark
Copy link
Contributor

reads similar to https://github.com/causaly/zod-validation-error - maybe there's interesting prior art there for how to generalize this, tho ig it probably looks exactly the same lol

@bholmesdev
Copy link
Contributor Author

@jasikpark I gave that library a try! Didn't like that keys were listed at the end of an error instead of the beginning (i.e. Required at "key" instead of "key": Required). Also doesn't have the union error handling and bolding that we have here. Thanks for mentioning that though, not sure how we could align this with their approach without an explosion of config options 😅

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Left a comment! I might try to go for more surprise than judgement, but non-blocking!

@jasikpark
Copy link
Contributor

@bholmesdev yep didn't think it'd be a drop-in at all, just wanted to make you aware / show that there's existing interest in the concept of "give me pretty error messages from a zod validation error"

Copy link
Member

@yanthomasdev yanthomasdev left a comment

Choose a reason for hiding this comment

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

That's a great improvement, nice work @bholmesdev! Docs LGTM 🙌

@bholmesdev bholmesdev merged commit c638740 into main Mar 13, 2023
@bholmesdev bholmesdev deleted the better-zod-error-map branch March 13, 2023 18:16
@astrobot-houston astrobot-houston mentioned this pull request Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants