Here is an article on how to recover the chain or reason for the income message from the Library Ethers.js
Solidity:
Turn the message chain or reason with ethers.js v5
In Solidity, there are declarations like revert (" A reason ")
oror
Demand (a ethers.utils.revert ().
The function `ethers.utils.revert ()
returns a promise that resolves with an error objectining information on the transaction transferred. We can then access the error message or the reason by calling error.message or 'error.reason'.
HERE IS AN EXAMPLE:
Javascript
Const ethers = Require ('ethers.js');
// Create a New Account
Constant = ethers.utils account.create ();
// come back with a personalized reason
Ethers.revert ({
DE: Account.address,
to: '0x ...',
Gas: 100,000,
Nonce: 1,
Value: 10n, // in Wei
Reason: "Something Went Wrong! Please try again again. ',
});
// Get the Error Message or the Reason Chosen
Const error = ethers.utils.revert (). then ((Err) => Err);
console.log (error.sessage); // "Something Went Wrong! Please try again again."
'
In this example, we create a new account, then use "ethers.revert ()" to reintegrate a transaction with a personalized reason. The returned promise is resolved with an error object which contains information on the transferred.
By accessing the error message or the reason using "Error.message" or "Error.reason", we can get precious information on what did not work during the reversion process.
Best Practices
- Always Manage the Errors correctly and display them to the user.
- Keep a trace of error messages and reasons for subquent analysis.
- Useethers.utils.revert ()` when you have to return a transaction with a personalized reason or message.
- Know that ethers.js v5 may have changed its API, so be sure to check the documentation for any change.
I hope it helps! Let me know if you have any questions or if you need additional help.