Skip to content

Commit

Permalink
Handle closed connections when sending to server
Browse files Browse the repository at this point in the history
  Fixes #346.

  This is tricky, because it is codec exception in the node, which is
  obviously a bug so we don't necessarily want to accomodate with
  special handling logic for that on the server. In particular, it's
  hard to tell whether the server needs to send a response or not at
  this level. So it's better handled client-side.

  We need to notify the team about this.
  • Loading branch information
KtorZ committed Nov 10, 2023
1 parent c5bbfda commit 29094db
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions clients/TypeScript/packages/client/src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,20 @@ export const send = async <T>(
): Promise<T> => {
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))
})
}

Expand Down

0 comments on commit 29094db

Please sign in to comment.