4

when user's transaction fails before submitting to the blockchain, I want to know the failure reason, so I can give a better user experience.

for example if user rejects a transaction in Metamask, or if the transaction fails due to insufficient funds for gas. how can I detect that?

my current implementation is not great, and will break easily. is there more elegant way to do it?

return sendTransaction(txData).catch(err => {
    if (err.message.includes('User denied transaction') ||
        err.message.includes('Request has been rejected.') ||
        err.message.includes('transaction has been discarded') ||
        err.message.includes('Transaction not confirmed'))
        throw new Error('web3UserTxRejected');

    if (err.message.includes('nonce too low'))
        throw new Error('web3NonceTooLow');

    if (err.message.includes('nonce may not be larger than'))
        throw new Error('web3NonceTooHigh');

    if (err.message.includes('insufficient funds for gas'))
        throw new Error('web3InsufficientFundsForGas');

    if (err.message.includes('intrinsic gas too low'))
        throw new Error('web3GasTooLow');

    throw new Error(err);
});

1 Answer 1

4

Currently there is no standard API return values for errors for web3.js implementations (somebody please correct me if I am wrong).

  • MetaMask is free to change the error message strings as they wish

  • Error messages can be localized

  • Other wallets like Parity, Status.im, Trust give different error message

I feel the JavaScript design patterns are partly to blame here; JavaScript design patterns do not encourage good user facing error handling.

I suggest this could be addressed by opening the discussion on web3.js Github issue tracked and propose how the error handling should be done.

2
  • Not only the error handling is a issue. It's also not easy yet to implement the same app with different launchers. Exploring Metamask Mascara I currently favor the Idea of some kind of Dapp launcher unify developer- and user experience. Apr 22, 2018 at 16:25
  • Is it all still currently true? Are there any attempts to standardize connection errors, user denied account access ones, etc.? Oct 26, 2021 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.