ethers.js icon indicating copy to clipboard operation
ethers.js copied to clipboard

Exceptions & Error-Messages are hard to parse. Is there anything to improve this?

Open itinance opened this issue 3 years ago β€’ 2 comments

Describe the bug

When during execution of a smart contract function a revert is happening, it is hard to parse the proper error message. The error-string contains normal text together with some JSONified strings in the middle.

Like this:

Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"code":-32603,"message":"execution reverted: Code has already claimed","data":{"originalError":{"code":3,"data":"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000018436f64652068617320616c726561647920636c61696d65640000000000000000","message":"execution reverted: Code has already claimed"}}}, method="estimateGas", transaction={"from":"0xC16f5C62b29704F7aBECb27A3cb7E12a91383261","to":"0xb21FFFd62BD2f4aBd2a1dC34A2302Fda364977a0","data":"0xd2c34d3f0000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000063132333435360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004153801a64156372ec7cd1d91868dd35ed68972dfa8b347c59db14bc49b753ed576fbe8a2bc00b6d0ba5dc8429c01748ae55e87dffa9547aafeb844aa40bb6c3e31b00000000000000000000000000000000000000000000000000000000000000","accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.5.1)

The actual error is somewhere in the middle and states:

"execution reverted: Code has already claimed"

Has some already found out a way to parse the actual error message correctly?

Reproduction steps

Call any method that reverts for some reason.

Environment:

all versions.

Search Terms metamask, web3, errors

itinance avatar Dec 31 '21 11:12 itinance

@itinance You can try console.log(error.error.message) when you are catching the error.

rodsotdia avatar Jan 11 '22 04:01 rodsotdia

Hey ! πŸ‘‹πŸ»

I landed on this issue a few months ago while Google searching for a solution to this issue. Since then, I have been working on a solution that allows me to provide details about failing transactions to my users.

You can find my work in progress under a package at https://github.com/enzoferey/ethers-error-parser πŸ‘πŸ»

EDIT 19/11/2022: the package is still in active development and we are rounding corners release after release. Check it out 😁

enzoferey avatar Jul 23 '22 23:07 enzoferey

Wrap code with a try catch, when you get the error parse it and use the .code and .reason to get the proper error message

Typescript/Angular example try{ var tx = await smartContract.mint(); } catch (ex) { var error = ex as Error as any; var msg = error.code + " " + error.reason; }

Taz-Bizeloper avatar Nov 19 '22 21:11 Taz-Bizeloper

In my experience this behaviour is the single biggest source of confusion to developers from ethers.js.

In the past I've told colleagues many times "when you see one of these errors which looks like a gas issue, it's almost always NOT a gas issue, so look more carefully to see if the real error is buried inside the huge output" - and even then, sometimes they have forget and waste time trying to debug a non-existent gas issue!

IMHO it really needs be fixed within ethers, if at all possible. Surely there's some way to distinguish between a genuine gas error and a simulated transaction failure?

aspiers avatar Feb 13 '23 20:02 aspiers

@aspiers It has been addressed in v6. It isn’t really something that can be addressed in v5, due to the way that the API was designed, not without breaking apps that depend on the current operation. In v6, estimateGas can throw call exceptions and the reason for a call exception is available in the action property.

ricmoo avatar Feb 13 '23 20:02 ricmoo