I want to catch a custom defined error in my Foundry tests. Before I was using require and the following code worked:
try pathRegistry.swap(WETH, LUSD, amountIn, type(uint256).max) {
assertTrue(false, "swap(..) should revert when amountOut < amountIn.");
} catch Error(string memory reason) {
assertEq(reason, "INSUFFICIENT_AMOUNT_OUT");
}
After replacing this require statement
require(amountOutMin <= amountOut, "INSUFFICIENT_AMOUNT_OUT");
with
if (amountOut < amountOutMin) revert InsufficientAmountOut();
the catch execution path no longer gets executed.
I managed to get the catch part executed using the following code:
try pathRegistry.swap(WETH, LUSD, amountIn, type(uint256).max) {
assertTrue(false, "swap(..) should revert when amountOut < amountIn.");
} catch (bytes memory reason) {
emit log_string(string(reason));
}
However an issue with this code is that string(reason) is equal to �)p�
and I would like to test that specifically the InsufficientAmountOut
error got thrown.
How would I solve this? Thank you
EDIT:
According to this post errors are ABI encode. For this reason I assume that I should catch the error with catch (bytes memory reason)
. The issue I am now facing is that I don't know how to verify that bytes memory reason
can actually be decoded as error InsufficientAmountOut()
. I found a blog post where the author explains how to decode custom errors in javascript. However I don't know how to do it in Solidity. Thanks
InsufficientAmountOut
?TypeError: Invalid catch clause name. Expected either catch (...), catch Error(...), or catch Panic(...).
.