Skip to content

Commit

Permalink
Add cause field to ApolloError. (#11902)
Browse files Browse the repository at this point in the history
* Add `cause` field to `ApolloError`.

* review feedback: include `protocolErrors`

* Clean up Prettier, Size-limit, and Api-Extractor

---------

Co-authored-by: phryneas <phryneas@users.noreply.github.com>
  • Loading branch information
phryneas and phryneas committed Jul 8, 2024
1 parent 5ac68eb commit 96422ce
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .api-reports/api-report-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ export interface ApolloClientOptions<TCacheShape> {
export class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-errors.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import type { GraphQLErrorExtensions } from 'graphql';
// @public (undocumented)
export class ApolloError extends Error {
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ export interface ApolloContextValue {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react_components.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ interface ApolloClientOptions<TCacheShape> {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react_context.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ export interface ApolloContextValue {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react_hoc.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ interface ApolloClientOptions<TCacheShape> {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react_hooks.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ interface ApolloClientOptions<TCacheShape> {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react_internal.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ interface ApolloClientOptions<TCacheShape> {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react_ssr.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ interface ApolloClientOptions<TCacheShape> {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-testing.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ interface ApolloClientOptions<TCacheShape> {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-testing_core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ interface ApolloClientOptions<TCacheShape> {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-utilities.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ interface ApolloClientOptions<TCacheShape> {
class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ export interface ApolloContextValue {
export class ApolloError extends Error {
// Warning: (ae-forgotten-export) The symbol "ApolloErrorOptions" needs to be exported by the entry point index.d.ts
constructor({ graphQLErrors, protocolErrors, clientErrors, networkError, errorMessage, extraInfo, }: ApolloErrorOptions);
cause: ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>) | null;
// (undocumented)
clientErrors: ReadonlyArray<Error>;
// (undocumented)
Expand Down
5 changes: 5 additions & 0 deletions .changeset/flat-onions-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Add `cause` field to `ApolloError`.
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 39873,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32865
"dist/apollo-client.min.cjs": 39906,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32896
}
18 changes: 18 additions & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ export class ApolloError extends Error {
}>;
public clientErrors: ReadonlyArray<Error>;
public networkError: Error | ServerParseError | ServerError | null;
/**
* Indicates the specific original cause of the error.
*
* This field contains the first available `networkError`, `graphQLError`, `protocolError`, `clientError`, or `null` if none are available.
*/
public cause:
| ({
message: string;
extensions?: GraphQLErrorExtensions[];
} & Partial<Error>)
| null;

// An object that can be used to provide some additional information
// about an error, e.g. specifying the type of error this is. Used
Expand All @@ -106,6 +117,13 @@ export class ApolloError extends Error {
this.networkError = networkError || null;
this.message = errorMessage || generateErrorMessage(this);
this.extraInfo = extraInfo;
this.cause =
[
networkError,
...(graphQLErrors || []),
...(protocolErrors || []),
...(clientErrors || []),
].find((e) => !!e) || null;

// We're not using `Object.setPrototypeOf` here as it isn't fully
// supported on Android (see issue #3236).
Expand Down

0 comments on commit 96422ce

Please sign in to comment.