Decode Error(string) in try-catch

I’m trying to decode error data from try-catch in Solidity.

try callee.foo(_val) returns (uint256 v) {
    return v;
} catch (bytes memory data) {
    errData = data;
    return 0;
}

I get errorData like:

0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d4c657373207468616e2031303000000000000000000000000000000000000000

This is from revert("Less than 100"); Solidity docs says 0x08c379a0 is selector of Error(string) and the next is offset, length and revert string.

I tried to decode this hexadecimal data using abi.decode like:

(type) =  abi.decode(errData, (type, type...)); 

But it does not work fine. What’s the best way to decode error data from revert(string)?

1 Like

Hi @swkim109,

You may want to look at using functionCall from Address utilities.

You can see how the revert reason is bubbled up:

1 Like

So, should I decode data by using assembly?

1 Like

Hi @swkim109,

I suggest you look at using functionCall rather than creating your own functionality.

If that doesn’t meet your needs, then you could use the same approach to bubble up the revert message.

1 Like

I found some trick:

function f(bytes calldata _errData) external pure returns (string memory) {
    return abi.decode(_errData[4:], (string));
}
1 Like