Skip to main content
22 votes

Testing for custom error reverts in hardhat

Update to selected answer Custom error hardhat will check for the name of the error, not the contents of the message MyContract.sol error SomeCustomError(string message); contract MyContract { ...
8koi's user avatar
  • 331
16 votes
Accepted

How to bubble up a custom error when using DELEGATECALL?

Check out this function I modified from the openzepplin's Address.sol. function _delegatecall(address target, bytes memory data) internal returns (bytes memory) { (bool success, bytes memory ...
diccccn's user avatar
  • 376
13 votes
Accepted

Testing for custom error reverts in hardhat

Try moving the await before the expect. e.g. await expect(nftMarketplace.listItem(basicNft.address, TOKEN_ID, PRICE)).to.be.revertedWith(`AlreadyListed`) That's what my tests use for custom errors ...
helpful hint's user avatar
7 votes
Accepted

Catching custom error

SOLUTION 1: Best solution is using expectRevert cheat code from forge-std: vm.expectRevert(ExampleBridgeContract.InvalidCaller.selector); exampleBridge.convert(empty, empty, empty, empty, 0, 0, 0, ...
Jan Beneš's user avatar
6 votes
Accepted

When should I use `require` vs custom `revert` errors?

Custom errors are more gas efficient than using require with a string explanation. So ideally you'd always use this over require.
Patrick Collins's user avatar
5 votes
Accepted

Foundry expectRevert on Custom Error with Multiple Errors

I had to remove the space between uint256 and uint256 on this line: bytes4 selector = bytes4(keccak256("MaximumContributionExceeded(uint256,uint256)"));
currenthandle's user avatar
4 votes
Accepted

Test fails with custom error despite having "MyCustomError"

as far as I know to.be.revertedWith is used for catching string errors. To expect a Custom Error you need to use to.be.revertedWithCustomError(contractInstance,NameOfTheCustomError). Also if the ...
Yamen Merhi's user avatar
3 votes
Accepted

What type is "MyInterface.MyCustomError" in Solidity?

I eventually figured out half of the answer on my own by trying to force cast the type to bytes4. It seems like MyInterface.MyCustomError is of type function() pure: Explicit type conversion not ...
Paul Razvan Berg's user avatar
3 votes
Accepted

what is the correct way to define a custom error

If it's defined outside you can share it among your contracts and maintain consistency in your code, other than that I don't think there are any other relevant differences in deployment or execution ...
ceseshi's user avatar
  • 765
3 votes

When should I use `require` vs custom `revert` errors?

When reviewing the Impostors NFT contract (which exclusively uses custom errors with revert), they noted this in regards to using custom errors: It saves bytecode to revert on custom errors instead ...
luxo's user avatar
  • 228
2 votes

How to handle Solidity Custom Errors in ethers.js

You can handle custom errors from your Solidity smart contract in your application using ethers-decode-error quite easily. This utility also allows you to extract and use the parameters from custom ...
toybox's user avatar
  • 81
2 votes

unexpected revert behaviour using custom errors in pure function

That is due to how Ethers dynamically handles transactions. If you need to call a method that does not change the state of a smart contract (i.e. test1 and test3), Ethers uses a call. Calls are used ...
Giuseppe Bertone's user avatar
2 votes

How to write test cases for custom errors?

What error is it? I tried it by myself and see the error Invalid Chai property: revertedWithCustomError. Did you try to change revertedWithCustomError => revertedWith like below? it("Should ...
CT95's user avatar
  • 543
2 votes
Accepted

Is there a way to handle `abi.decode` errors?

While writing the question, I was able to find a solution. ATTOW this is already a feature request. See issue-10381 and issue-10933 on github. Thankfully one of the core members mentioned in this ...
BonisTech's user avatar
  • 672
2 votes

How to display solidity's custom errors on frontend?

Well, I found an answer. I needed to use a different method. Corrected solution: const decoded = interfaces.decodeFunctionData( interfaces.getFunction("CustomError(uint256)"), ...
Shubham Sharma's user avatar
1 vote

Debugging when catching a custom error

Since custom errors are encoded the same way functions are, so one way is to manually decode the bytes hex-data that you're getting in: [FAIL. Reason: Error != expected error: 0x57f3b2d7.... != ...
SYED ASAD KAZMI's user avatar
1 vote
Accepted

Hardhat doesn't recognize custom errors from inherited Contracts

The message is thrown at chai-matchers validation and it means, that the expected error is not found in the contracts interface: https://github.com/NomicFoundation/hardhat/blob/...
tenbits's user avatar
  • 2,508
1 vote

How to test custom error from inherited contract?

Just "import contract1" and use "contract1.Error1" will be able to solve this issue.
HouseHusband's user avatar
1 vote

I am getting Struct containing a (nested) mapping cannot be constructed error

I hope this will answer your question and help you with your issue. First of all, structs with nested mappings can only be accessed through storage, so you can not create the "Transaction" ...
ckryptoon's user avatar
1 vote

Foundry expectRevert on Custom Error with Multiple Errors

If your custom errors are declared inside the contract, and they do not have params, you can simply do: vm.expectRevert(MyContract.MaximumContributionExceeded.selector); If they do have params, you ...
Danziger's user avatar
  • 151
1 vote

How do does the trasaction error catching work?

You can take a look at how does custom-errors are being handled when Ethereum transaction fails with this example: https://medium.com/coinmonks/solidity-revert-with-custom-error-explained-with-example-...
Yongjian P.'s user avatar
  • 4,308
1 vote

Can revert context be provided to frontend application for solidity's reverts?

You can use the parseError method from your contract's interface. Read the docs for better understanding about Interface parsing Using your contract's interface, you can use the parseError method and ...
BonisTech's user avatar
  • 672
1 vote

Can revert context be provided to frontend application for solidity's reverts?

Require and revert are both fine, but what you can do is a callStatic (previously 'staticCall') to try the transaction and then handle the error however you'd like. contract.transfer(someAddress, ...
Wmm's user avatar
  • 41
1 vote

When should I use `require` vs custom `revert` errors?

Custom errors reduce the cost to deploy and call a function on a contract. Errors also provide parameterization which gives the error much more context of the reasons why the error occurred. Compare ...
John's user avatar
  • 81
1 vote

How to bubble up a custom error when using DELEGATECALL?

Normally a call or a delegatecall doesn't revert while calling a function that reverts. (bool success, bytes memory result) = to.call/delegatecall{value,gas,etc}(data). The success variable will ...
Yamen Merhi's user avatar
1 vote
Accepted

Are there well-solved patterns and practices for handling errors in Solidity?

Solidity v0.8.4 to the rescue! // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.4; error Unauthorized(); contract VendingMachine { address payable owner = payable(msg.sender); ...
Paul Razvan Berg's user avatar
1 vote
Accepted

How to handle Solidity Custom Errors in ethers.js

According to the document, you can use revertedWithCustomError await expect( contract.compare(actual) ).to.be.revertedWithCustomError(contract, 'ComparisionFailed').withArgs(123, 1);
emn178's user avatar
  • 126
1 vote

How to handle Solidity Custom Errors in ethers.js

I wrote a function to check for custom errors: export function customError(errorName: string, ...args: any[]) { let argumentString = ''; if (Array.isArray(args) && args.length) { // ...
M. Heuer's user avatar
  • 198

Only top scored, non community-wiki answers of a minimum length are eligible