From 178b5095f0e48600f89b5e2ea5c1a96efbc11740 Mon Sep 17 00:00:00 2001 From: turbocrime Date: Mon, 3 Mar 2025 21:03:19 -0800 Subject: [PATCH] simplify errors --- packages/transport-chrome/src/stream.ts | 38 +++++++------------------ 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/packages/transport-chrome/src/stream.ts b/packages/transport-chrome/src/stream.ts index 70147ad64..295b32811 100644 --- a/packages/transport-chrome/src/stream.ts +++ b/packages/transport-chrome/src/stream.ts @@ -13,7 +13,7 @@ export class PortStreamSource implements UnderlyingDefaultSource { constructor(incoming: chrome.runtime.Port) { this.disconnect = () => incoming.disconnect(); - this.ac.signal.addEventListener('abort', this.onAbortDisconnect); + this.ac.signal.addEventListener('abort', this.onSignalDisconnect); incoming.onDisconnect.addListener(() => this.ac.abort(ConnectError.from('Source disconnected', Code.Unavailable)), @@ -27,7 +27,7 @@ export class PortStreamSource implements UnderlyingDefaultSource { ? this.push(this.cont, item) : this.ac.abort( new ConnectError( - 'PortStreamSource message before start', + 'Stream item arrived before start', Code.Unavailable, undefined, undefined, @@ -35,7 +35,7 @@ export class PortStreamSource implements UnderlyingDefaultSource { ), ); - private onAbortDisconnect = () => { + private onSignalDisconnect = () => { if (globalThis.__DEV__) { console.debug('PortStreamSource signal', this.ac.signal.reason); } @@ -73,17 +73,11 @@ export class PortStreamSource implements UnderlyingDefaultSource { chunk.abort, undefined, // fallback error - ConnectError.from('PortStreamSource failed to deserialize abort reason', Code.Aborted), + ConnectError.from('Failed to deserialize abort reason'), ); } else { // should never happen - throw new ConnectError( - 'PortStreamSource unexpected item', - Code.Unimplemented, - undefined, - undefined, - chunk, - ); + throw new TypeError('Unexpected stream item', { cause: chunk }); } } catch (e) { // disconnect and fail with cause @@ -113,22 +107,12 @@ export class PortStreamSink implements UnderlyingSink { outgoing.onDisconnect.removeListener(this.onDisconnectCancel); // set up expectation that the counterpart will disconnect const termination = shouldDisconnect(outgoing); - try { - // post the final message - outgoing.postMessage(item); - await termination; - } catch (cause) { - // in the failure case, we can't expect the counterpart to disconnect. - outgoing.disconnect(); - - throw new ConnectError( - 'PortStreamSink termination failed', - Code.Canceled, - undefined, - undefined, - cause, - ); - } + // post the final message + outgoing.postMessage(item); + await termination.finally( + // ensure port disconnected + () => outgoing.disconnect(), + ); }; this.ac.signal.addEventListener('abort', () => {