Skip to content

Conversation

@pangolin1
Copy link

@pangolin1 pangolin1 commented Aug 8, 2023

ethjs-query

Hi,

Every time metamask gets an RPC error, it provides this incorrect error message and a convoluted JSON.
Screenshot 2023-08-08 at 5 21 28 PM

The problem is not an error in formatting the RPC output (this [ethjs-query] while formatting outputs from RPC log was copied from the code above it which does handle formatting errors and I think was not changed by accident).

The actual problem is that the ethjs-rpc library throws an error when it receives an error response from the RPC. This Is a normal javascript Error object with the RPC error object attached as a value parameter (see: https://github.com/MetaMask/ethjs-rpc/blob/6dcd6d298f5041f14dc8fa90e2f63fc185f3e886/src/index.js#L52). The RPC error object is to spec (i.e. {code: <code>, message: <msg>}). This error is currently caught, but it isn't parsed correctly, leading to the strange message in metamask.

This PR adds code which parses the RPC error into a more friendly human-readable string, which can then be used by metamask to more gracefully display RPC errors. It should not break anything as it is just changing the Error string.

  • You have followed our contributing guidelines
  • Pull request has tests (we are going for 100% coverage!)
  • Code is well-commented, linted and follows project conventions
  • Documentation is updated (if necessary)
  • Internal code generators and templates are updated (if necessary)
  • Description explains the issue/use-case resolved and auto-closes related issues

Be kind to code reviewers, please try to keep pull requests as small and focused as possible :)

IMPORTANT: By submitting a patch, you agree to allow the project
owners to license your work under the terms of the MIT License.

@socket-security
Copy link

socket-security bot commented Aug 8, 2023

No dependency changes detected. Learn more about Socket for GitHub ↗︎

👍 No dependency changes detected in pull request

@brad-decker
Copy link

@pangolin1 I think we're going to want this work but I'm not sure if the changes to package-lock.json is necessary for your work? I had very similar changes locally that will solve a problem for the MetaMask team that results in a wide variety of RPC errors being bucketed into the same sentry report due to the errors being re-thrown at the code line you've changed in this PR.

We ideally will want to land #5 first and then put this into a new release soon.

src/index.js Outdated
errorMsg += error.value.code ? ` (code ${error.value.code})` : '';
errorMsg += error.value.message ? `: "${error.value.message}"` : '';
}
reject(new Error(errorMsg));

Choose a reason for hiding this comment

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

Do we need to modify the error? I don't think we really need to do that here. The code will be included in the error message for debugging purposes. If we create a new error it confuses our reporting so we should simply handle the error and reject it (remove lines 101-105, and simply reject(error) on line 106

Copy link
Author

Choose a reason for hiding this comment

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

I think we do, because this error is rendered verbatim by metamask. If you don't format it, it will render a string representation of a double nested JSON

Copy link
Author

Choose a reason for hiding this comment

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

It is also unclear currently that this is an RPC error, rather than an error caused by this library. I think it's better to be explicit "RPC error: (code 32000) already known" rather than "{value: { code: 32000, message: already known}}"

Choose a reason for hiding this comment

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

Either way we cannot throw a new error or we will override the stack trace leading to our error reporting tools bundling very much different errors in the same bucket because it looks like it came from the same place. I tested my branch locally by copying the dist file over to the metamask-extension's node_modules to verify that the error made sense from a browser perspective. however its possible that my solution would be worse for dapp developers.

@pangolin1
Copy link
Author

sorry I don't know why the package,json file changed - possibly when i ran tests? I can roll this back

errorMsg += error.value.message ? `: "${error.value.message}"` : '';
}
// eslint-disable-next-line no-param-reassign
error.message = errorMsg;

Choose a reason for hiding this comment

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

@pangolin1 @brad-decker I just merged in main and this is what I came up with after resolving on the changes in #16

Would actually modifying the error with a new message rather than constructing a new one be a workable compromise?

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.

3 participants