Skip to content

Commit

Permalink
fix: resolves #2563
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Jul 30, 2024
1 parent 410ff2b commit 9684e35
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/actions/public/watchContractEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1567,4 +1567,34 @@ describe('subscribe', () => {
unwatch()
})
})

test(
'https://github.com/wevm/viem/issues/2563',
async () => {
let error: Error | undefined
const unwatch = watchContractEvent(webSocketClient, {
...usdcContractConfig,
onError: (error_) => {
error = error_
},
onLogs: () => {},
})

await wait(100)
const { socket } = await webSocketClient.transport.getRpcClient()
socket.close()
await wait(100)

expect(error).toMatchInlineSnapshot(`
[SocketClosedError: The socket has been closed.
URL: http://localhost
Version: [email protected]]
`)

unwatch()
},
{ timeout: 10_000 },
)
})
33 changes: 33 additions & 0 deletions src/clients/transports/webSocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ test('subscribe', async () => {
expect(blocks.length).toBe(1)
})

test('throws on socket closure', async () => {
const transport = webSocket(anvilMainnet.rpcUrl.ws, {
key: 'jsonRpc',
name: 'JSON RPC',
})({})
if (!transport.value) return

let error: Error | undefined
const { subscriptionId } = await transport.value.subscribe({
params: ['newHeads'],
onData: () => {},
onError: (error_) => {
error = error_
},
})

// Make sure we are subscribed.
expect(subscriptionId).toBeDefined()

await wait(100)
const { socket } = await transport.value.getRpcClient()
socket.close()
await wait(100)

expect(error).toMatchInlineSnapshot(`
[SocketClosedError: The socket has been closed.
URL: http://localhost
Version: [email protected]]
`)
})

test('throws on bogus subscription', async () => {
const transport = webSocket(anvilMainnet.rpcUrl.ws, {
key: 'jsonRpc',
Expand Down
5 changes: 5 additions & 0 deletions src/clients/transports/webSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export function webSocket(
method: 'eth_subscribe',
params,
},
onError(error) {
reject(error)
onError?.(error)
return
},
onResponse(response) {
if (response.error) {
reject(response.error)
Expand Down

0 comments on commit 9684e35

Please sign in to comment.