diff --git a/.api-reports/api-report-core.api.md b/.api-reports/api-report-core.api.md index de8b71f915..35789a5844 100644 --- a/.api-reports/api-report-core.api.md +++ b/.api-reports/api-report-core.api.md @@ -172,6 +172,10 @@ export interface ApolloClientOptions { 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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-errors.api.md b/.api-reports/api-report-errors.api.md index 205b170bf0..2b3d65a055 100644 --- a/.api-reports/api-report-errors.api.md +++ b/.api-reports/api-report-errors.api.md @@ -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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-react.api.md b/.api-reports/api-report-react.api.md index 368a7fdc53..c81d938a88 100644 --- a/.api-reports/api-report-react.api.md +++ b/.api-reports/api-report-react.api.md @@ -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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-react_components.api.md b/.api-reports/api-report-react_components.api.md index b9bd5eda67..efb4ddab23 100644 --- a/.api-reports/api-report-react_components.api.md +++ b/.api-reports/api-report-react_components.api.md @@ -201,6 +201,10 @@ interface ApolloClientOptions { 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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-react_context.api.md b/.api-reports/api-report-react_context.api.md index bc1878779a..41aed0f90c 100644 --- a/.api-reports/api-report-react_context.api.md +++ b/.api-reports/api-report-react_context.api.md @@ -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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-react_hoc.api.md b/.api-reports/api-report-react_hoc.api.md index a5cf57a3c8..18f094dde4 100644 --- a/.api-reports/api-report-react_hoc.api.md +++ b/.api-reports/api-report-react_hoc.api.md @@ -200,6 +200,10 @@ interface ApolloClientOptions { 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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-react_hooks.api.md b/.api-reports/api-report-react_hooks.api.md index a88a99c06c..b5ee3e1c05 100644 --- a/.api-reports/api-report-react_hooks.api.md +++ b/.api-reports/api-report-react_hooks.api.md @@ -199,6 +199,10 @@ interface ApolloClientOptions { 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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-react_internal.api.md b/.api-reports/api-report-react_internal.api.md index be65e4e3a3..95a7366593 100644 --- a/.api-reports/api-report-react_internal.api.md +++ b/.api-reports/api-report-react_internal.api.md @@ -199,6 +199,10 @@ interface ApolloClientOptions { 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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-react_ssr.api.md b/.api-reports/api-report-react_ssr.api.md index f4ca55a6da..ea788b305b 100644 --- a/.api-reports/api-report-react_ssr.api.md +++ b/.api-reports/api-report-react_ssr.api.md @@ -200,6 +200,10 @@ interface ApolloClientOptions { 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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-testing.api.md b/.api-reports/api-report-testing.api.md index c147f00f82..01ba05ec8b 100644 --- a/.api-reports/api-report-testing.api.md +++ b/.api-reports/api-report-testing.api.md @@ -200,6 +200,10 @@ interface ApolloClientOptions { 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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-testing_core.api.md b/.api-reports/api-report-testing_core.api.md index 1cbfc54605..545e30231c 100644 --- a/.api-reports/api-report-testing_core.api.md +++ b/.api-reports/api-report-testing_core.api.md @@ -199,6 +199,10 @@ interface ApolloClientOptions { 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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report-utilities.api.md b/.api-reports/api-report-utilities.api.md index c53a249f69..84e1668f11 100644 --- a/.api-reports/api-report-utilities.api.md +++ b/.api-reports/api-report-utilities.api.md @@ -212,6 +212,10 @@ interface ApolloClientOptions { 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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.api-reports/api-report.api.md b/.api-reports/api-report.api.md index 4acdff1d7d..fb2bec5894 100644 --- a/.api-reports/api-report.api.md +++ b/.api-reports/api-report.api.md @@ -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) | null; // (undocumented) clientErrors: ReadonlyArray; // (undocumented) diff --git a/.changeset/flat-onions-guess.md b/.changeset/flat-onions-guess.md new file mode 100644 index 0000000000..ce7dc67887 --- /dev/null +++ b/.changeset/flat-onions-guess.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Add `cause` field to `ApolloError`. diff --git a/.size-limits.json b/.size-limits.json index 81ed0bcf99..5cca69e725 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -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 } diff --git a/src/errors/index.ts b/src/errors/index.ts index 6927705550..3c07411161 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -81,6 +81,17 @@ export class ApolloError extends Error { }>; public clientErrors: ReadonlyArray; 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) + | 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 @@ -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).