Ethereum: With ethers.js v5, how to get the reverted message string or reverted reason?

Here is an article on how to returnee the message or cause from the “Ethers.js” library:

Returned a returnee or reason to get a reason for eters.js v5

There are statements in the thoroughness, such as “return (” some reason “)ordemanding (a ethers.utils.revert () function.

Ethers.utils.revert () Function returns a promise that solves the error object containing information about the returned event. We may then use an error message or cause by calling the “error.message” or error.reason '.

Here's an example:

Javascript

Const Ethers = Requires (‘eetters.js’);

// Create a new account

Const Account = Eether.utils Account.create ();

// Restore for custom reasons

Ethers.revert ({

Sender: Account.address,

to: ‘0x …’,

Gas: 100000,

NONCE: 1,

Value: 10n, // Wei

Reason: ‘Something went wrong! Try again later. ‘

});

// Get a returned error message or reason

Const Error = Ether.utils.revert (). Then ((err) => err);

Console.log (error.message); // “Something went wrong! Please try again later.”

`

In this example, we will create a new account and then use the Ethers.revert () 'to return the event to a tailor -made reason. The returned promise will determine the error object containing information about the returned event.

By using the error message or cause usingerror.messageor error.reason ‘, we can get valuable views on what went wrong during the return process.

Best Practices

Ethereum: With ethers.js v5, how to get the reverted message string or reverted reason?

  • Always handle errors and show them to the user.

  • Follow error messages and later analysis.

  • Use Ethers.utils.revert () `when you have to restore the event for custom reason or message.

  • Note that Ethers.JS V5 may have changed its application subscription, so check the documents for any changes.

I hope this will help! Tell me if you have any questions or you need additional help.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *