CombinedProtocolErrors

API reference


Fatal transport-level errors returned when executing a subscription using the multipart HTTP subscription protocol. See the documentation on the multipart HTTP protocol for GraphQL Subscriptions for more information on these errors.

These errors indicate issues with the subscription transport itself, rather than GraphQL-level errors. They typically occur when there are problems communicating with subgraphs from the Apollo Router.

TypeScript
1 import { CombinedProtocolErrors } from "@apollo/client/errors"; 2 3 // Check if an error is a CombinedProtocolErrors instance 4 if (CombinedProtocolErrors.is(error)) { 5 // Access individual protocol errors 6 error.errors.forEach((protocolError) => { 7 console.log(protocolError.message); 8 console.log(protocolError.extensions); 9 }); 10 }

Providing a custom message formatter

By default, CombinedProtocolErrors formats the message property by joining each error's message field with a newline. To customize the format of the message, such as changing the delimiter or adding a message prefix, override the static formatMessage method.

The following example demonstrates how to format the error message by joining each error with a comma.

TypeScript
1import { CombinedProtocolErrors } from "@apollo/client/errors"; 2 3CombinedProtocolErrors.formatMessage = (errors) => { 4 return errors.map((error) => error.message).join(", "); 5};

See the formatMessage section for details about the parameters provided to the formatMessage function.

note
The message formatter needs to be configured before any operation is executed by Apollo Client, otherwise the default message formatter is used. We recommend configuring the message formatter before initializing your ApolloClient instance.

Using the default message formatter

To format part of the message using the default message formatter, call the defaultFormatMessage function provided to the options argument of your message formatter.

The following example prepends a string to the message and uses the default message formatter to format the error messages.

TypeScript
1CombinedProtocolErrors.formatMessage = (errors, { defaultFormatMessage }) => { 2 return `[Protocol errors]: ${defaultFormatMessage(errors)}`; 3};

Static methods

A method that determines whether an error is a CombinedProtocolErrors object. This method enables TypeScript to narrow the error type.

Example

TypeScript
1 if (CombinedProtocolErrors.is(error)) { 2 // TypeScript now knows `error` is a CombinedProtocolErrors object 3 console.log(error.errors); 4 }

Signature

TypeScript
1is( 2 error: unknown 3): error is CombinedProtocolErrors

See the instance properties for more details about the available properties provided by the CombinedProtocolErrors object.

A function that formats the error message used for the error's message property. Override this method to provide your own formatting.

The formatMessage function is called by the CombinedProtocolErrors constructor to provide a formatted message as the message property of the CombinedProtocolErrors object. Follow the "Providing a custom message formatter" guide to learn how to modify the message format.

Signature

TypeScript
1formatMessage( 2 errors: ReadonlyArray<GraphQLFormattedError>, 3 options: MessageFormatterOptions 4): string

Parameters

Name / Type
Description
errors
ReadonlyArray<GraphQLFormattedError>

The array of GraphQL errors returned from the server in the errors field of the response.

options
MessageFormatterOptions

Additional context that could be useful when formatting the message.

Properties
Name / Type
Description
(errors: ReadonlyArray<GraphQLFormattedError>) => string

The default message formatter. Call this to get a string with the default formatted message.

Read more...

To format part of the message using the default message formatter, call the defaultFormatMessage function provided to the options argument of your message formatter.

The following example prepends a string to the message and uses the default message formatter to format the error messages.

TypeScript
1CombinedProtocolErrors.formatMessage = (errors, { defaultFormatMessage }) => { 2 return `[Protocol errors]: ${defaultFormatMessage(errors)}`; 3};

Instance properties

These properties are specific to the CombinedProtocolErrors object. Standard error instance properties are also available.

ReadonlyArray<GraphQLFormattedError>

The raw list of errors returned by the top-level errors field in the multipart HTTP subscription response.