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

feat: include mutation error items in error message #148

Merged
merged 2 commits into from
Mar 13, 2023

Conversation

rexxars
Copy link
Member

@rexxars rexxars commented Mar 9, 2023

When you encounter mutation errors, the error message/staack from the client can be less than helpful:

ClientError: Mutation(s) failed with 1 error(s)
    at onResponse (/my/app/node_modules/@sanity/client/dist/index.cjs:78:13)
    at applyMiddleware (/my/app/node_modules/get-it/dist/index.cjs:60:15)
    ...

The response body does usually contain more information, but holds it in an items array. It is not obvious that this is available - you can technically get it at error.details, but it's typed as any and not documented. Ideally we'd introduce a new MutationError class with better typing, but that would be a breaking change (people may be relying on instanceof ClientError checks or similar), so will leave that for a later PR.

This PR provides a slight improvement on the situation, in a way that should be backwards compatible:

  • It extracts the description property of the first 5 error items and includes them in the error message
  • Includes the number of additional errors there were

So the resulting error might look like the following:

ClientError: Mutation(s) failed with 6 error(s):
- Malformed document ID: "#some_invalid-id!"
- Malformed document ID: "@ruby_bird@"
- Malformed document ID: "!cant_contain_that"
- Malformed document ID: "what$about!this?"
- Malformed document ID: "%so_many_percent%"
...and 1 more
    at onResponse (/my/app/node_modules/@sanity/client/dist/index.cjs:78:13)
    at applyMiddleware (/my/app/node_modules/get-it/dist/index.cjs:60:15)
    ...

Copy link
Member

@stipsan stipsan left a comment

Choose a reason for hiding this comment

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

Nice! 👍

Copy link
Member

@bjoerge bjoerge 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 great! 💯

Just a minor nit, otherwise LGTM

src/http/errors.ts Outdated Show resolved Hide resolved
@rexxars rexxars force-pushed the feat/improve-mutation-errors branch from 4b51279 to 5f20a14 Compare March 13, 2023 15:26
@rexxars rexxars merged commit 193f45e into main Mar 13, 2023
@rexxars rexxars deleted the feat/improve-mutation-errors branch March 13, 2023 16:35
@github-actions
Copy link
Contributor

🎉 This PR is included in version 5.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants