Hello mates. I would like to know if there are some caveats against using
file level constants to declare error messages on require statements. I think that
is not the case because according to solidity documentation, constants are inline
and don't occupy storage, but maybe there is something that I am not seeing.
The idea is something like this
/* snip */
string constant MsgFoo = "some message" ;
contract ExampleContract {
function exampleFunction() external {
require(msg.sender != address(0) ,MsgFoo) ;
/* code */
}
}
/* snip */
What I am looking for is for test purposes. If using this approach then
when testing (at least using foundry) I would import constants declaration
and write something like this.
/* snip */
import "path/to/ExampleContract.sol" ; // MsgFoo variable is imported here
contract TestExampleContract is Test {
ExampleContract testContract ;
functino setUp() {
testContract = ExampleContract() ;
}
function test_exampleFunction() public {
vm.expectRevert(MsgFoo) ; // this declare that call to `exampleFunction` must revert with message `MsgFoo`
vm.prank(address(0)) // this set msg.sender = address(0) in the next function call
testContract.exampleFunction() ;
}
/* snip */
Of course, you can simply copy the message string of the require
statement and assign it to a constant
in your test or simply copy and paste directly the string in the expect revert statement like
vm.expectRevert(YourConstant)
or vm.expectRevert("message here")
although I personally
prefer to avoid this because errors may happen...(but is what I ended doing until now)
Thanks in advance.