1

I am trying to catch custom errors in my frontend project, but am not able to do that.

My contract:

pragma solidity 0.8.16;

contract Test {
    error CustomError(uint256 value);

    uint public a;

    function throwError() public {
        a = 6;
        revert CustomError(5);
    }
}

I am trying to use the ethers' interface method to get the error. React code:

  const handleError = async (e: any) => {
    e.preventDefault();

    try {
      let result: any = await dispatch(callContractSendMethod("throwError", [], userAddress));
      result = JSON.parse(result.toString().split(".")[1]);

      const interfaces: any = new Interface([
        "function CustomError(uint256 value)",
      ]);

      const decoded = interfaces.decodeFunctionData(
        interfaces.functions["CustomError(uint256)"],
        result.data
      );

      console.log("decoded", decoded);
    } catch (error: any) {
      console.log("error", error);
    }
  };

But the issue I am facing is that the code interfaces.functions["CustomError(uint256)"] is returning undefined. Because of this, the decoding process is not working correctly. Any solution or way to do it?

1
  • Is it Working ?? are we getting msg string on frontend Commented Nov 4 at 15:09

1 Answer 1

2

Well, I found an answer. I needed to use a different method. Corrected solution:

 const decoded = interfaces.decodeFunctionData(
    interfaces.getFunction("CustomError(uint256)"),
    result.data
  );

Your Answer

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

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