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 {
...
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 ...
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
...
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, ...
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.
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)"));
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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)"),
...
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.... != ...
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/...
1
vote
How to test custom error from inherited contract?
Just "import contract1" and use "contract1.Error1" will be able to solve this issue.
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" ...
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 ...
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-...
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 ...
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, ...
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 ...
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 ...
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);
...
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);
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) {
// ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
custom-errors × 35solidity × 22
hardhat × 7
error × 5
ethers.js × 5
require × 4
contract-development × 3
testing × 3
revert × 3
remix × 2
dapps × 2
javascript × 2
revert-opcode × 2
solidity-0.8.x × 2
chai × 2
forge × 2
foundry × 2
web3js × 1
truffle × 1
transactions × 1
contract-design × 1
ether × 1
contract-invocation × 1
events × 1
evm × 1