diff --git a/clients/TypeScript/packages/client/src/Connection.ts b/clients/TypeScript/packages/client/src/Connection.ts index fbdf78bcf7..62720c0058 100644 --- a/clients/TypeScript/packages/client/src/Connection.ts +++ b/clients/TypeScript/packages/client/src/Connection.ts @@ -168,9 +168,20 @@ export const send = async ( ): Promise => { const { socket } = context return new Promise((resolve, reject) => { + function onUnexpectedClose (code: CloseEvent['code'], reason: CloseEvent['reason']) { + reject(new JSONRPCError( + -32000, + 'Connection closed', + { code, reason } + )) + } + + socket.once('close', onUnexpectedClose) + send(socket) .then(resolve) .catch(error => reject(JSONRPCError.tryFrom(error) || error)) + .finally(() => socket.removeListener('close', onUnexpectedClose)) }) }