-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expectRevert should be able to check for custom errors #180
Comments
Seems like #152 is fixing this? |
Thank you for raising this @Rav3nPL. Are you running against Ganache or Hardhat? |
Using Truffle, OpenZepplien test suite and Chai. |
I've tried using the code in #152, and I couldn't make it work When calling a function that throws a custom error, I'm getting
I personally believe we need a system that allows to check custom errors arguments just like what we for events, and we are very far from it :/ |
@Rav3nPL I had read your issue wrong. This is a limitation of Ganache. Ganache simply returns |
Not necessarily. it('see promise', async function () {
try {
await test.willThrowOnZero('0')
}
catch (e) {
console.log(e)
}
}) And you find out that:
So looks like hash of revert return code IS known. |
No I think that's the transaction hash. The custom error is encoded as a 4-byte selector. |
That's the
part of error message |
Oh I see... I wasn't able to reproduce it when I tried yesterday because I was using a non-view function that reverts. We may have the error selector but do we have the ABI so we can decode it? |
When supporting custom errors, it's also essential to deal with error messages when the error occurs and we're not expecting it, just like an unexpected revert. Today it prints this, while we could be seeing the actual error:
This is how I'm testing custom errors...
|
@rsodre I tried your code in agoraxyz/token-xyz-contracts. I have installed ganache 7.3.2 and truffle 5.5.20 globally. {
"data": {
"hash": null,
"programCounter": 568,
"result": "0x1bbcd9e50000000000000000000000000000000000000000000000000000000062ffbf4e0000000000000000000000000000000000000000000000000000000062ffbed6",
"reason": null,
"message": "revert"
},
"reason": "Custom error (could not decode)",
"hijackedStack": "Error: Returned error: VM Exception while processing transaction: revert -- Reason given: Custom error (could not decode).\n at Object.ErrorResponse (/home/tomi_ohl/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-helpers/lib/errors.js:28:1)\n at /home/tomi_ohl/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/web3-core-requestmanager/lib/index.js:300:1\n at /home/tomi_ohl/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/packages/provider/wrapper.js:119:1\n at XMLHttpRequest.request.onreadystatechange (/home/tomi_ohl/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/web3-providers-http/lib/index.js:98:1)\n at XMLHttpRequestEventTarget.dispatchEvent (/home/tomi_ohl/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)\n at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._setReadyState (/home/tomi_ohl/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:208:1)\n at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._onHttpResponseEnd (/home/tomi_ohl/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:318:1)\n at IncomingMessage.<anonymous> (/home/tomi_ohl/.nvm/versions/node/v16.15.1/lib/node_modules/truffle/build/webpack:/node_modules/xhr2-cookies/dist/xml-http-request.js:289:48)\n at IncomingMessage.emit (node:events:539:35)\n at endReadableNT (node:internal/streams/readable:1345:12)\n at processTicksAndRejections (node:internal/process/task_queues:83:21)"
} As you see it doesn't include the reasonId (0x9856227d in my case). |
my receipt is very different than yours, and I use ganache-ui, which is very outdated, from before custom errors were added to Solidity. |
Nope, but thanks, your question helped! error DistributionEnded(uint256 current, uint256 end); May I guess none of your custom errors have parameters? @frangio I tested the above with non-view functions. We have the selector and the params. The abi could be fetched from the contract artifact that truffle generates, couldn't it? Alternatively, the user could specify the parameter types in a way. I feel like testing for custom errors should be possible with truffle either way. |
Correct, my error had no arguments. |
This library is not who should be doing that. The error needs to be decoded at the level of web3.js or Truffle. |
Well, alright. Anyways, I created a separate library for this: https://www.npmjs.com/package/custom-error-test-helper |
@TomiOhl I am trying to use your library with hardhat like so: const {
balance,
ether,
expectEvent,
expectRevert,
BN,
} = require('@openzeppelin/test-helpers');
const { expectRevertCustomError } = require("custom-error-test-helper");
const Marketplace = artifacts.require("Marketplace");
describe("Marketplace contract", function () {
let marketplace;
beforeEach(async function () {
marketplace = await Marketplace.new();
});
it("Should validate listed 721 errors", async function () {
await expectRevertCustomError(Marketplace, marketplace.listNFT(marketplace.address, BigNumber.from("1"), BigNumber.from("1"), BigNumber.from(TOMORROW), BigNumber.from(IN_FIVE_DAYS),
{ from: TOKEN_OWNER, value: listingFee}), "UnsupportedNftInterface");
});
}); I get the following error. Do you know what is causing this and how to fix the error? 1) Marketplace contract
Validate Listings
Should validate listed 721 errors:
TypeError: Cannot read properties of undefined (reading 'result')
at expectRevertCustomError (node_modules/custom-error-test-helper/build/index.js:17:88)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at runNextTicks (node:internal/process/task_queues:64:3)
at listOnTimeout (node:internal/timers:533:9)
at processTimers (node:internal/timers:507:7)
at async Context.<anonymous> (test/Marketplace.js:374:9) |
Note to others: the above question was answered in TomiOhl/custom-error-test-helper#1 |
Since 0.8.4 we have use of new and cheap custom errors https://blog.soliditylang.org/2021/04/21/custom-errors/
Using them in contract code, but in test I need to use expectRevert.uspecified to handle them in any way.
Reproduce:
Test running throws:

The text was updated successfully, but these errors were encountered: