-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Stefan Stefanov <[email protected]>
- Loading branch information
1 parent
b7f9d1e
commit b1db954
Showing
4 changed files
with
108 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,60 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.20; | ||
|
||
import { ErrorsExternal } from "./ErrorsExternal.sol"; | ||
|
||
contract Errors { | ||
constructor() { | ||
error InsufficientBalance(uint256 available, uint256 required); | ||
ErrorsExternal errorsExternal; | ||
event Result(uint code, string message); | ||
|
||
constructor(address errorsExternalAddr) { | ||
errorsExternal = ErrorsExternal(errorsExternalAddr); | ||
} | ||
|
||
function assertCheck(bool condition) public pure returns (bool) { | ||
function assertCheck(bool condition) external pure returns (bool) { | ||
assert(condition); | ||
return true; | ||
} | ||
|
||
function requireCheck(bool shouldRevert) public pure returns (bool) { | ||
function requireCheck(bool shouldRevert) external pure returns (bool) { | ||
require(shouldRevert); | ||
return true; | ||
} | ||
|
||
function revertCheck() public pure returns (bool) { | ||
function revertCheck() external pure returns (bool) { | ||
revert(); | ||
} | ||
|
||
function revertWithMessageCheck(string calldata message) public pure returns (bool) { | ||
function revertWithMessageCheck(string calldata message) external pure returns (bool) { | ||
revert(message); | ||
} | ||
|
||
function revertWithCustomError() external pure returns (bool) { | ||
revert InsufficientBalance(1, 100); | ||
} | ||
|
||
function tryCatchWithSimpleRevert() external returns (int value, bool success) { | ||
try errorsExternal.revertSimple() returns (bool v) { | ||
return (1, v); | ||
} catch (bytes memory) { | ||
emit Result(0, 'revertSimple'); | ||
} | ||
} | ||
|
||
function tryCatchWithErrorMessageRevert(string memory message) external returns (int value, bool success) { | ||
try errorsExternal.revertWithErrorMessage(message) returns (bool v) { | ||
return (1, v); | ||
} catch Error(string memory message) { | ||
emit Result(0, message); | ||
} | ||
} | ||
|
||
function tryCatchWithPanic() external returns (uint value, bool success) { | ||
try errorsExternal.panic() returns (uint v) { | ||
return (v, false); | ||
} catch Panic(uint errorCode) { | ||
emit Result(errorCode, 'panic'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.20; | ||
|
||
contract ErrorsExternal { | ||
error InsufficientBalance(uint256 available, uint256 required); | ||
|
||
function revertWithCustomError() external pure returns (bool) { | ||
revert InsufficientBalance(1, 100); | ||
} | ||
|
||
function revertSimple() external pure returns (bool) { | ||
revert(); | ||
} | ||
|
||
function revertWithErrorMessage(string memory message) external pure returns (bool) { | ||
revert(message); | ||
} | ||
|
||
function panic() external pure returns (uint) { | ||
return uint(4)/uint(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters