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

Add cause field to ApolloError. #11902

Merged
merged 4 commits into from
Jul 8, 2024
Merged

Add cause field to ApolloError. #11902

merged 4 commits into from
Jul 8, 2024

Conversation

phryneas
Copy link
Member

There seems to be progress on unjs/h3#691, so I'm opening a PR on our side.

Generally, this is probably a good idea independently of what they do there.

Copy link

changeset-bot bot commented Jun 21, 2024

🦋 Changeset detected

Latest commit: 5e6a6b4

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

This PR includes changesets to release 1 package
Name Type
@apollo/client 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
Contributor

github-actions bot commented Jun 21, 2024

size-limit report 📦

Path Size
dist/apollo-client.min.cjs 38.97 KB (+0.09% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" 47.69 KB (+0.07% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" (production) 45.22 KB (+0.05% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" 34.27 KB (+0.1% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" (production) 32.13 KB (+0.1% 🔺)
import { ApolloProvider } from "dist/react/index.js" 1.26 KB (0%)
import { ApolloProvider } from "dist/react/index.js" (production) 1.24 KB (0%)
import { useQuery } from "dist/react/index.js" 5.23 KB (+0.59% 🔺)
import { useQuery } from "dist/react/index.js" (production) 4.31 KB (+0.8% 🔺)
import { useLazyQuery } from "dist/react/index.js" 5.71 KB (+0.57% 🔺)
import { useLazyQuery } from "dist/react/index.js" (production) 4.79 KB (+0.72% 🔺)
import { useMutation } from "dist/react/index.js" 3.62 KB (+0.88% 🔺)
import { useMutation } from "dist/react/index.js" (production) 2.84 KB (+1.22% 🔺)
import { useSubscription } from "dist/react/index.js" 3.63 KB (0%)
import { useSubscription } from "dist/react/index.js" (production) 2.78 KB (0%)
import { useSuspenseQuery } from "dist/react/index.js" 5.49 KB (+0.54% 🔺)
import { useSuspenseQuery } from "dist/react/index.js" (production) 4.15 KB (+0.81% 🔺)
import { useBackgroundQuery } from "dist/react/index.js" 4.99 KB (0%)
import { useBackgroundQuery } from "dist/react/index.js" (production) 3.64 KB (0%)
import { useLoadableQuery } from "dist/react/index.js" 5.07 KB (0%)
import { useLoadableQuery } from "dist/react/index.js" (production) 3.72 KB (0%)
import { useReadQuery } from "dist/react/index.js" 3.39 KB (+1.14% 🔺)
import { useReadQuery } from "dist/react/index.js" (production) 3.33 KB (+1.07% 🔺)
import { useFragment } from "dist/react/index.js" 2.32 KB (0%)
import { useFragment } from "dist/react/index.js" (production) 2.27 KB (0%)

Copy link

netlify bot commented Jun 21, 2024

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit 5e6a6b4
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/668ba1a656907400080b8616
😎 Deploy Preview https://deploy-preview-11902--apollo-client-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -106,6 +112,10 @@ export class ApolloError extends Error {
this.networkError = networkError || null;
this.message = errorMessage || generateErrorMessage(this);
this.extraInfo = extraInfo;
this.cause =
[networkError, ...(clientErrors || []), ...(graphQLErrors || [])].find(
Copy link
Member

Choose a reason for hiding this comment

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

Should protocolErrors be included in this list?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, they are not Error objects, so no consumer of cause could actually handle them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fun fact: nothing in our codebase actually populates clientErrors, so I was thinking about skipping that, too. But I found a bunch of userland code on GitHub that manually creates ApolloError instances with clientErrors. Not what we had in mind, but I didn't want to break them.

Copy link
Member

Choose a reason for hiding this comment

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

According to MDN, it looks like it doesn't have to be an Error instance in order to use it:

Error messages written for human consumption may be inappropriate for machine parsing — since they're subject to rewording or punctuation changes that may break any existing parsing written to consume them. So when throwing an error from a function, as an alternative to a human-readable error message, you can instead provide the cause as structured data, for machine parsing.

function makeRSA(p, q) {
  if (!Number.isInteger(p) || !Number.isInteger(q)) {
    throw new Error("RSA key generation requires integer inputs.", {
      cause: { code: "NonInteger", values: [p, q] },
    });
  }
  if (!areCoprime(p, q)) {
    throw new Error("RSA key generation requires two co-prime integers.", {
      cause: { code: "NonCoprime", values: [p, q] },
    });
  }
  // rsa algorithm…
}

Despite our types, I think that graphqlErrors can also just be an array of objects (i.e. the raw parsed json value from the GraphQL response), not just GraphQLError instances.

Should we be concerned about that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we be concerned about that?

Hmmm... I think it's more about an expected shape than anything.

From that perspective, protocolErrors could get interesting again - they don't have most of the fields of a normal Error, but they do have a message field that some logging solution could read.

If we add them back in, which priority would you prefer?

Copy link
Member

Choose a reason for hiding this comment

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

IMO the checked order should be networkError, graphQLErrors, protocolErrors, clientErrors and find the first instance of one. Thanks for humoring me :)

Copy link
Member Author

Choose a reason for hiding this comment

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

6e96e72 - does this work? :)

@phryneas phryneas requested a review from jerelmiller July 2, 2024 09:17
Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

Looks great! Let's get this one in 3.11

@jerelmiller jerelmiller changed the base branch from main to release-3.11 July 5, 2024 22:15
@jerelmiller jerelmiller added this to the 3.11.0 milestone Jul 5, 2024
@phryneas phryneas added the auto-cleanup 🤖 label Jul 8, 2024
@phryneas phryneas merged commit 96422ce into release-3.11 Jul 8, 2024
35 of 36 checks passed
@phryneas phryneas deleted the pr/error-cause branch July 8, 2024 08:28
@github-actions github-actions bot mentioned this pull request Jul 9, 2024
@github-actions github-actions bot mentioned this pull request Jul 22, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 8, 2024
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.

2 participants