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.
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.
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.
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.
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
1 if (CombinedProtocolErrors.is(error)) { 2 // TypeScript now knows `error` is a CombinedProtocolErrors object 3 console.log(error.errors); 4 }
Signature
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
1formatMessage( 2 errors: ReadonlyArray<GraphQLFormattedError>, 3 options: MessageFormatterOptions 4): string
Parameters
(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.
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.