Skip to content

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Mar 31, 2025

The JSON-RPC specification says that a JSON-RPC-compliant server must return a response with either a result or an error field, and if an error field is present, then it must be an object with the following fields: code, message, and data. The isJsonRpcError from @metamask/utils (via the JsonRpcError type) not only checks for the three aforementioned properties, but also allows an additional, optional stack property to be present. However, it does not allow any other properties beyond the four declared.

This is a problem because the new implementation of the fetch middleware makes use of this function to know how to handle errors, and as a result, non-standard error responses are being treated as successful responses (and the errors themselves are being discarded). This is particularly noticeable when using Ganache as a local RPC server, because it produces such non-standard error responses, such as when a transaction fails.

To correct this bug, this commit brings the error-handling code in the fetch middleware closer to the original implementation, and updates the tests to ensure that Ganache errors are treated correctly.

The [JSON-RPC specification](https://www.jsonrpc.org/specification) says
that a JSON-RPC-compliant server must return a response with either a
`result` or an `error` field, and if an `error` field is present, then
it must be an object with the following fields: `code`, `message`, and
`data`. The `isJsonRpcError` from `@metamask/utils`
(via the [`JsonRpcError`
type][2]) not only checks for the three aforementioned properties, but
also allows an additional, optional `stack` property to be present.
However, it does not allow any other properties beyond the four
declared.

This is a problem because the new implementation of the `fetch`
middleware makes use of this function to know how to handle errors, and
as a result, non-standard error responses are being treated as
successful responses (and the errors themselves are being discarded).
This is particularly noticeable when using Ganache as a local RPC
server, because it produces such non-standard error responses, such as
when a transaction fails.

To correct this bug, this commit brings the error-handling code in the
`fetch` middleware closer to the original implementation, and updates
the tests to ensure that Ganache errors are treated correctly.

[1]: https://www.jsonrpc.org/specification
[2]: https://github.com/MetaMask/utils/blob/55b22a77e75641c4c8b05eec73982084ac9e7332/src/json.ts#L301
@mcmire mcmire force-pushed the fix-error-handling branch from 24f87ca to 08807f8 Compare March 31, 2025 18:50
@mcmire mcmire marked this pull request as ready for review March 31, 2025 18:54
@mcmire mcmire requested a review from a team as a code owner March 31, 2025 18:54
Copy link
Contributor

@cryptodev-2s cryptodev-2s left a comment

Choose a reason for hiding this comment

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

LGTM!

@mcmire mcmire merged commit a84a057 into main Mar 31, 2025
20 checks passed
@mcmire mcmire deleted the fix-error-handling branch March 31, 2025 20:40
@mcmire
Copy link
Contributor Author

mcmire commented Mar 31, 2025

Tested on mobile as a part of getting MetaMask/metamask-mobile#14273 to pass CI and this seems to work.

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.

2 participants