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

Conversation

yogaajs
Copy link

@yogaajs yogaajs commented Feb 27, 2025

Some providers, return error responses as strings instead of JSON objects. For example, getBlock.io returns "rate limit reached" as a plain string, causing a SyntaxError when attempting to parse it as JSON.

Handle Non-JSON Error Responses from Providers

Some providers, return error responses as strings instead of JSON objects. For example, getBlock.io returns "rate limit reached" as a plain string, causing a SyntaxError when attempting to parse it as JSON:
Copy link

changeset-bot bot commented Feb 27, 2025

⚠️ No Changeset found

Latest commit: c8352cd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Feb 27, 2025

@yogaajs is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants