I'm toying with the recently introduced custom errors. Here's a mini replica of my set up:
interface MyInterface {
error MyCustomError();
}
I'm writing my tests in Solidity and I'd like to use the expectRevert cheatcode (part of HEVM) to test that my implementation contract reverts with MyCustomError
. Here's what I came up with so far:
vm.expectRevert(abi.encodeWithSignature("MyCustomError()"));
This works since the expectRevert
cheatcode is implemented in three different ways:
expectRevert()
expectRevert(bytes calldata)
expectRevert(bytes4)
But using abi.encodeWithSignature
feels like going in a roundabout way. I tried to reference the custom error directly:
vm.expectRevert(MyInterface.MyCustomError);
But this doesn't compile, so it follows that MyCustomError
is neither bytes
nor bytes4
when referenced like this. That's fine. But then, what type is it, and could I cast it to either bytes
or bytes4
?
MyInterface.MyCustomError.selector
? it should return a bytes4 to identify your error in a relatively readable way..selector
member. I posted an answer below.