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

Improve Zod errors #1542

Merged
merged 20 commits into from
Feb 23, 2024
Merged

Improve Zod errors #1542

merged 20 commits into from
Feb 23, 2024

Conversation

delucis
Copy link
Member

@delucis delucis commented Feb 21, 2024

Description

  • Improves our Zod error map:

    • Better handling of unions.
      Previously they logged only “Did not match union” and no further information.
      An example of the new format:

      0: Did not match union.

      Expected type { href: string } | { entries: array }
      Received { "label": "Custom link 1", "href": 5 }

    • Better formatting of expected values.
      Stringified objects/arrays now have some whitespace around items, e.g. { "label": "Text" } instead of {"label":"Text"}. They are also now wrapped in code backticks so they render as code in Astro’s error screens.

    • Better handling of deeply nested issues. (Thanks @HiDeoo!)

      Union errors that were found deep in a schema previously only reported the top-level path, e.g. sidebar.0. We are now able to show the full path, e.g. sidebar.0.items.1.

  • Refactors all safeParse() calls on Zod schemas to use a new parseWithFriendlyErrors() utility which bundles up our logic for throwing an error with the error map.

  • When throwing errors, uses AstroError for nicer formatting:

    Example of Astro’s error screen displaying a nicely formatted error message

    Example of terminal displaying a nicely formatted error message

  • Testing error messages is improved with a Vitest custom snapshot serializer, which captures both the message and the hint fields on AstroError. I updated Vitest to 1.3 to be able to configure this for all tests in one place (this feature was added in feat(config): add snapshotSerializers option vitest-dev/vitest#5092).

Copy link

changeset-bot bot commented Feb 21, 2024

🦋 Changeset detected

Latest commit: fa1cd88

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

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

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

Copy link

vercel bot commented Feb 21, 2024

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

Name Status Preview Updated (UTC)
starlight ✅ Ready (Inspect) Visit Preview Feb 23, 2024 10:24pm
1 Ignored Deployment
Name Status Preview Updated (UTC)
starlight-i18n ⬜️ Ignored (Inspect) Feb 23, 2024 10:24pm

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Feb 21, 2024
@astrobot-houston
Copy link
Collaborator

astrobot-houston commented Feb 21, 2024

size-limit report 📦

Path Size
/index.html 5.23 KB (0%)
/_astro/*.js 21.54 KB (0%)
/_astro/*.css 13.45 KB (0%)

delucis and others added 2 commits February 23, 2024 16:41
Co-Authored-By: HiDeoo <494699+HiDeoo@users.noreply.github.com>
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

AstroError ✨ 🤩

packages/starlight/utils/error-map.ts Show resolved Hide resolved
@delucis delucis merged commit b3b7a60 into main Feb 23, 2024
9 checks passed
@delucis delucis deleted the chris/error-map-2 branch February 23, 2024 22:36
@astrobot-houston astrobot-houston mentioned this pull request Feb 23, 2024
HiDeoo added a commit to HiDeoo/starlight that referenced this pull request Feb 26, 2024
* main:
  [ci] format
  [ci] release (withastro#1555)
  Update lockfile & patch version of EC to latest (withastro#1553)
  [ci] format
  [ci] release (withastro#1546)
  Improve Zod errors (withastro#1542)
  Update `astro-expressive-code` dependency to `^0.33.2` (withastro#1541)
  i18n(ja): Update pages.mdx (withastro#1551)
  i18n(es): update `plugins` (withastro#1548)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 core Changes to Starlight’s main package 🌟 tailwind Changes to Starlight’s Tailwind package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants