Skip to content

Commit 325554a

Browse files
authored
Merge pull request #4 from axieinfinity/feature/lib-error-handler
feat: add `LibErrorHandler`
2 parents 29686ed + abd4932 commit 325554a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/LibErrorHandler.sol

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
3+
4+
library LibErrorHandler {
5+
/// @dev Reserves error definition to upload to signature database.
6+
error ExternalCallFailed(bytes4 msgSig, bytes4 callSig);
7+
8+
/// @notice handle low level call revert if call failed,
9+
/// If extcall return empty bytes, reverts with custom error.
10+
/// @param status Status of external call
11+
/// @param callSig function signature of the calldata
12+
/// @param returnOrRevertData bytes result from external call
13+
function handleRevert(bool status, bytes4 callSig, bytes memory returnOrRevertData) internal pure {
14+
// Get the function signature of current context
15+
bytes4 msgSig = msg.sig;
16+
assembly ("memory-safe") {
17+
if iszero(status) {
18+
// Load the length of bytes array
19+
let revertLength := mload(returnOrRevertData)
20+
// Check if length != 0 => revert following reason from external call
21+
if iszero(iszero(revertLength)) {
22+
// Start of revert data bytes. The 0x20 offset is always the same.
23+
revert(add(returnOrRevertData, 0x20), revertLength)
24+
}
25+
26+
// Load free memory pointer
27+
let ptr := mload(0x40)
28+
// Store 4 bytes the function selector of ExternalCallFailed(msg.sig, callSig)
29+
// Equivalent to revert ExternalCallFailed(bytes4,bytes4)
30+
mstore(ptr, 0x49bf4104)
31+
// Store 4 bytes of msgSig parameter in the next slot
32+
mstore(add(ptr, 0x20), msgSig)
33+
// Store 4 bytes of callSig parameter in the next slot
34+
mstore(add(ptr, 0x40), callSig)
35+
// Revert 68 bytes of error starting from 0x1c
36+
revert(add(ptr, 0x1c), 0x44)
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)