Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Non-JSON Error Responses from Providers #3389

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/utils/rpc/webSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export async function getWebSocketRpcClient(
onClose()
}
function onMessage({ data }: MessageEvent) {
onResponse(JSON.parse(data))
try {
const _data = JSON.parse(data);
onResponse(_data);
} catch (error) {
onResponse(data);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably here should be onError(error)

Copy link
Author

@yogaajs yogaajs Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an error, if the provider returns a string instead of an object the message is still valid.

It's just that on the provider side it's "badly done" since it should return an object with the error, the code etc...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but according to onResponse type https://github.com/wevm/viem/blob/main/src/utils/rpc/socket.ts#L22 it only accept RpcResponse object and can't accept string as argument

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right on that point, I hadn't seen it that way, but a rate limit is sort of an error. In reality anything that is not an rpc response is an error.

}
}

// Setup event listeners for RPC & subscription responses.
Expand Down