Skip to content

Commit

Permalink
Throw abort errors from the channel in cases where the abort is clear
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed Oct 9, 2024
1 parent 6666c55 commit 6beb407
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ describe('createWebSocketChannel', () => {
signal: abortController.signal,
url: 'ws://fake', // Wrong URL!
});
await expect(channelPromise).rejects.toThrow(
new SolanaError(SOLANA_ERROR__RPC_SUBSCRIPTIONS__CHANNEL_CONNECTION_CLOSED, {
cause: abortController.signal.reason,
}),
);
await expect(channelPromise).rejects.toThrow(/operation was aborted/);
});
it('throws when the channel is aborted before a connection is established', async () => {
expect.assertions(2);
Expand All @@ -77,11 +73,7 @@ describe('createWebSocketChannel', () => {
const client = getLatestClient();
expect(client).toHaveProperty('readyState', WebSocket.CONNECTING);
abortController.abort();
await expect(channelPromise).rejects.toThrow(
new SolanaError(SOLANA_ERROR__RPC_SUBSCRIPTIONS__CHANNEL_FAILED_TO_CONNECT, {
errorEvent: {} as Event,
}),
);
await expect(channelPromise).rejects.toThrow(/operation was aborted/);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ export function createWebSocketChannel({
url,
}: Config): Promise<RpcSubscriptionsChannel<WebSocketMessage, string>> {
if (signal.aborted) {
return Promise.reject(
new SolanaError(SOLANA_ERROR__RPC_SUBSCRIPTIONS__CHANNEL_CONNECTION_CLOSED, {
cause: signal.reason,
}),
);
return Promise.reject(signal.reason);
}
let bufferDrainWatcher: Readonly<{ onCancel(): void; promise: Promise<void> }> | undefined;
let hasConnected = false;
Expand All @@ -37,14 +33,10 @@ export function createWebSocketChannel({
});
listenerRemovers.clear();
}
function handleAbort(ev: Event) {
function handleAbort() {
cleanupListeners();
if (!hasConnected) {
rejectOpen(
new SolanaError(SOLANA_ERROR__RPC_SUBSCRIPTIONS__CHANNEL_FAILED_TO_CONNECT, {
errorEvent: ev,
}),
);
rejectOpen(signal.reason);
}
if (webSocket.readyState !== WebSocket.CLOSED && webSocket.readyState !== WebSocket.CLOSING) {
webSocket.close(1000);
Expand Down

0 comments on commit 6beb407

Please sign in to comment.