From 9ccb46bc80024cb2de823702d2bd308052c6c516 Mon Sep 17 00:00:00 2001 From: Denis Badurina Date: Wed, 26 Aug 2020 18:16:22 +0200 Subject: [PATCH] fix(client): cant read the `CloseEvent.reason` after bundling so just pass the whole event to the sink error and let the user handle it --- src/client.ts | 15 +++++++-------- src/types.ts | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/client.ts b/src/client.ts index ba2dfad4..bdc1353e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -41,7 +41,7 @@ export function createClient(options: ClientOptions): Client { // to dispatch messages to the correct destination const subscribedSinks: Record = {}; - function errorAllSinks(err: Error) { + function errorAllSinks(err: Error | CloseEvent) { Object.entries(subscribedSinks).forEach(([, sink]) => sink.error(err)); } function completeAllSinks() { @@ -87,17 +87,16 @@ export function createClient(options: ClientOptions): Client { * > object (thereby invoking its onclose handler) to indicate the reason for the connection's closing. */ - socket.onclose = ({ code, reason }) => { - const err = new Error( - `Socket closed with event ${code}` + !reason ? '' : `: ${reason}`, - ); + socket.onclose = (closeEvent) => { + // NOTE: reading the `CloseEvent.reason` either trows or empties the whole error message + // (if trying to pass the reason in the `Error` message). let the user handle the close event - if (code === 1000 || code === 1001) { + if (closeEvent.code === 1000 || closeEvent.code === 1001) { // close event `1000: Normal Closure` is ok and so is `1001: Going Away` (maybe the server is restarting) completeAllSinks(); } else { // all other close events are considered erroneous - errorAllSinks(err); + errorAllSinks(closeEvent); } if (!done) { @@ -105,7 +104,7 @@ export function createClient(options: ClientOptions): Client { connecting = false; connected = false; // the connection is lost socket = null; - reject(err); // we reject here bacause the close is not supposed to be called during the connect phase + reject(closeEvent); // we reject here bacause the close is not supposed to be called during the connect phase } }; socket.onopen = () => { diff --git a/src/types.ts b/src/types.ts index 1231c718..a8c42e8c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -24,7 +24,7 @@ export interface Sink { /** Next value arriving. */ next(value: T): void; /** An error that has occured. Calling this function "closes" the sink. */ - error(error: Error | readonly GraphQLError[]): void; + error(error: Error | CloseEvent | readonly GraphQLError[]): void; /** The sink has completed. This function "closes" the sink. */ complete(): void; }