Skip to content

fix(rpc-service): improve error handling for HTTP status codes #5923

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

cryptodev-2s
Copy link
Contributor

@cryptodev-2s cryptodev-2s commented Jun 5, 2025

Explanation

Improves error handling in the RPC service by making it more specific and consistent. The changes include:

  • Clarifies error handling for different HTTP status codes:
    • 401: Unauthorized error
    • 402/404/5xx: Resource unavailable error
    • 429: Rate limiting error
    • Other 4xx responses now throw a generic HTTP client error
    • Invalid JSON: Parse error

Note: A previous attempt to merge this work has already been done in #5843 then reverted back in #5881

References

Fixes #5844

Changelog

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@cryptodev-2s cryptodev-2s requested review from a team as code owners June 5, 2025 00:37
@cryptodev-2s cryptodev-2s self-assigned this Jun 5, 2025
@cryptodev-2s cryptodev-2s requested a review from a team June 5, 2025 00:37
@cryptodev-2s cryptodev-2s force-pushed the fix/rpc-service-error-handling branch from 1d755cb to b6b1250 Compare June 5, 2025 01:02
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Apologies for not re-creating this. Just had a few comments.

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Actually, can you reach out to the Wallet API team and ask them to review this PR? I wanted to make sure that this won't cause any incompatibility issues with dapps before merging this. (I'm marking as "request changes" just as a shortcut)

@cryptodev-2s
Copy link
Contributor Author

Actually, can you reach out to the Wallet API team and ask them to review this PR? I wanted to make sure that this won't cause any incompatibility issues with dapps before merging this. (I'm marking as "request changes" just as a shortcut)

Yes I already shared the PR with the Wallet API team in slack.

@cryptodev-2s cryptodev-2s requested review from mcmire and a team June 5, 2025 14:53
* 405, 429, 503, or 504.
* @throws A 401 error if the response status is 401.
* @throws A "rate limiting" error if the response HTTP status is 429.
* @throws A "resource unavailable" error if the response status is 402, 404, or any 5xx.
Copy link
Contributor

Choose a reason for hiding this comment

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

wondering why 404 doesn't get a 32002 instead

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

oops i meant -32001

Copy link
Contributor

@mcmire mcmire Jun 5, 2025

Choose a reason for hiding this comment

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

My thought here was that according to the JSON-RPC spec -32001 is for when an RPC method is not found. In my mind this assumes that the endpoint is available and is ready to serve JSON-RPC, it's just that the server doesn't know what to do with the given JSON-RPC method. HTTP 404, on the other hand, means that the server doesn't know even what to do with the given path. So that feels more like "resource unavailable" than "RPC method not found".

@jiexi
Copy link
Contributor

jiexi commented Jun 5, 2025

wondering if it makes sense to the constants found here https://github.com/MetaMask/rpc-errors/blob/main/src/error-constants.ts#L1C14-L1C24

@mcmire
Copy link
Contributor

mcmire commented Jun 5, 2025

@jiexi

wondering if it makes sense to the constants found here

Which constants, and where?

@jiexi
Copy link
Contributor

jiexi commented Jun 5, 2025

the errorCodes.rpc ones in place of some of the inlined 3xxxx values

@cryptodev-2s cryptodev-2s requested a review from jiexi June 5, 2025 17:42
@cryptodev-2s cryptodev-2s enabled auto-merge (squash) June 5, 2025 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RPC Service: Improve error handling for HTTP status codes
3 participants