How to get custom error data from revert #628
-
contract function: function setImplementation(address _addr) external onlyOwner() {
if (_addr == address(0)) revert AddressIsZero();
implementation = _addr;
} viem code: const walletClient = createWalletClient({
account,
chain: polygonMumbai,
transport: http()
});
const main = async () => {
try {
const [owner] = await walletClient.getAddresses();
const txhash = await walletClient.writeContract({
address: contract_address,
abi: contractAbi,
functionName: "setImplementation",
args: [
ethers.constants.AddressZero as `0x${string}`
]
})
} catch(err: any) {
// const revertError = err.walk((err: any) => err instanceof ContractFunctionRevertedError)
// if (revertError) {
// revertError.data.errorName
// }
console.log(err.cause.data.errorName)
}
} result: owner@justweis-MackBook-Pro test-contract % npx hardhat run scripts/error.ts
/Users/owner/Desktop/dev/test-contract/scripts/error.ts:45
console.log(err.cause.data.errorName)
^
TypeError: Cannot read properties of undefined (reading 'errorName')
at main (/Users/owner/Desktop/dev/test-contract/scripts/error.ts:45:36)
at processTicksAndRejections (node:internal/process/task_queues:95:5) |
Beta Was this translation helpful? Give feedback.
Answered by
xxxijustwei
Jun 2, 2023
Replies: 1 comment
-
use |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
xxxijustwei
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use
publicClient.simulateContract
catch error.