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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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