Skip to content

Commit

Permalink
docs: better subscription error handling [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Nov 8, 2021
1 parent 4f7785d commit 2160755
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,24 +369,20 @@ function fetchOrSubscribe(operation: RequestParameters, variables: Variables) {
{
...sink,
error: (err) => {
if (err instanceof Error) {
return sink.error(err);
}
if (Array.isArray(err))
// GraphQLError[]
return sink.error(
new Error(err.map(({ message }) => message).join(', ')),
);

if (err instanceof CloseEvent) {
if (err instanceof CloseEvent)
return sink.error(
// reason will be available on clean closes
new Error(
`Socket closed with event ${err.code} ${err.reason || ''}`,
`Socket closed with event ${err.code} ${err.reason || ''}`, // reason will be available on clean closes only
),
);
}

return sink.error(
new Error(
(err as GraphQLError[]).map(({ message }) => message).join(', '),
),
);
return sink.error(err);
},
},
);
Expand Down Expand Up @@ -460,26 +456,20 @@ class WebSocketLink extends ApolloLink {
next: sink.next.bind(sink),
complete: sink.complete.bind(sink),
error: (err) => {
if (err instanceof Error) {
return sink.error(err);
}
if (Array.isArray(err))
// GraphQLError[]
return sink.error(
new Error(err.map(({ message }) => message).join(', ')),
);

if (err instanceof CloseEvent) {
if (err instanceof CloseEvent)
return sink.error(
// reason will be available on clean closes
new Error(
`Socket closed with event ${err.code} ${err.reason || ''}`,
`Socket closed with event ${err.code} ${err.reason || ''}`, // reason will be available on clean closes only
),
);
}

return sink.error(
new Error(
(err as GraphQLError[])
.map(({ message }) => message)
.join(', '),
),
);
return sink.error(err);
},
},
);
Expand Down

0 comments on commit 2160755

Please sign in to comment.