Skip to content

Commit 2160755

Browse files
committed
docs: better subscription error handling [skip ci]
1 parent 4f7785d commit 2160755

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

README.md

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,20 @@ function fetchOrSubscribe(operation: RequestParameters, variables: Variables) {
369369
{
370370
...sink,
371371
error: (err) => {
372-
if (err instanceof Error) {
373-
return sink.error(err);
374-
}
372+
if (Array.isArray(err))
373+
// GraphQLError[]
374+
return sink.error(
375+
new Error(err.map(({ message }) => message).join(', ')),
376+
);
375377

376-
if (err instanceof CloseEvent) {
378+
if (err instanceof CloseEvent)
377379
return sink.error(
378-
// reason will be available on clean closes
379380
new Error(
380-
`Socket closed with event ${err.code} ${err.reason || ''}`,
381+
`Socket closed with event ${err.code} ${err.reason || ''}`, // reason will be available on clean closes only
381382
),
382383
);
383-
}
384384

385-
return sink.error(
386-
new Error(
387-
(err as GraphQLError[]).map(({ message }) => message).join(', '),
388-
),
389-
);
385+
return sink.error(err);
390386
},
391387
},
392388
);
@@ -460,26 +456,20 @@ class WebSocketLink extends ApolloLink {
460456
next: sink.next.bind(sink),
461457
complete: sink.complete.bind(sink),
462458
error: (err) => {
463-
if (err instanceof Error) {
464-
return sink.error(err);
465-
}
459+
if (Array.isArray(err))
460+
// GraphQLError[]
461+
return sink.error(
462+
new Error(err.map(({ message }) => message).join(', ')),
463+
);
466464

467-
if (err instanceof CloseEvent) {
465+
if (err instanceof CloseEvent)
468466
return sink.error(
469-
// reason will be available on clean closes
470467
new Error(
471-
`Socket closed with event ${err.code} ${err.reason || ''}`,
468+
`Socket closed with event ${err.code} ${err.reason || ''}`, // reason will be available on clean closes only
472469
),
473470
);
474-
}
475471

476-
return sink.error(
477-
new Error(
478-
(err as GraphQLError[])
479-
.map(({ message }) => message)
480-
.join(', '),
481-
),
482-
);
472+
return sink.error(err);
483473
},
484474
},
485475
);

0 commit comments

Comments
 (0)