Skip to content

Commit 65419a0

Browse files
author
Mike-CZ
committed
Add custom errors for common and ownership packages
1 parent 7bbeaa5 commit 65419a0

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

contracts/common/Initializable.sol

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ contract Initializable {
2424
*/
2525
bool private initializing;
2626

27+
/**
28+
* @dev The contract instance has already been initialized.
29+
*/
30+
error ContractInitialized();
31+
2732
/**
2833
* @dev Modifier to use in the initializer function of a contract.
2934
*/
3035
modifier initializer() {
31-
require(initializing || !initialized, "Contract instance has already been initialized");
36+
if (!initializing && initialized) {
37+
revert ContractInitialized();
38+
}
3239

3340
bool isTopLevelCall = !initializing;
3441
if (isTopLevelCall) {

contracts/common/ReentrancyGuard.sol

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ contract ReentrancyGuard is Initializable {
1919
// counter to allow mutex lock with only one SSTORE operation
2020
uint256 private _guardCounter;
2121

22+
/**
23+
* @dev Reentrant call.
24+
*/
25+
error ReentrantCall();
26+
2227
function initialize() internal initializer {
2328
// The counter starts at one to prevent changing it from zero to a non-zero
2429
// value, which is a more expensive operation.
@@ -36,7 +41,9 @@ contract ReentrancyGuard is Initializable {
3641
_guardCounter += 1;
3742
uint256 localCounter = _guardCounter;
3843
_;
39-
require(localCounter == _guardCounter, "ReentrancyGuard: reentrant call");
44+
if (localCounter != _guardCounter) {
45+
revert ReentrantCall();
46+
}
4047
}
4148

4249
uint256[50] private ______gap;

contracts/ownership/Ownable.sol

+16-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ import "../common/Initializable.sol";
1515
contract Ownable is Initializable {
1616
address private _owner;
1717

18+
/**
19+
* @dev The caller is not the owner.
20+
*/
21+
error NotOwner();
22+
23+
/**
24+
* @dev Given zero address.
25+
*/
26+
error ZeroAddress();
27+
1828
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
1929

2030
/**
@@ -36,7 +46,9 @@ contract Ownable is Initializable {
3646
* @dev Throws if called by any account other than the owner.
3747
*/
3848
modifier onlyOwner() {
39-
require(isOwner(), "Ownable: caller is not the owner");
49+
if (!isOwner()) {
50+
revert NotOwner();
51+
}
4052
_;
4153
}
4254

@@ -71,7 +83,9 @@ contract Ownable is Initializable {
7183
* @dev Transfers ownership of the contract to a new account (`newOwner`).
7284
*/
7385
function _transferOwnership(address newOwner) internal {
74-
require(newOwner != address(0), "Ownable: new owner is the zero address");
86+
if (newOwner == address(0)) {
87+
revert ZeroAddress();
88+
}
7589
emit OwnershipTransferred(_owner, newOwner);
7690
_owner = newOwner;
7791
}

test/NodeDriver.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ describe('NodeDriver', () => {
3838

3939
it('Should revert when not owner', async function () {
4040
const account = ethers.Wallet.createRandom();
41-
await expect(this.nodeDriverAuth.connect(this.nonOwner).migrateTo(account)).to.be.revertedWith(
42-
'Ownable: caller is not the owner',
41+
await expect(this.nodeDriverAuth.connect(this.nonOwner).migrateTo(account)).to.be.revertedWithCustomError(
42+
this.nodeDriverAuth,
43+
'NotOwner',
4344
);
4445
});
4546
});
@@ -52,9 +53,9 @@ describe('NodeDriver', () => {
5253

5354
it('Should revert when not owner', async function () {
5455
const address = ethers.Wallet.createRandom();
55-
await expect(this.nodeDriverAuth.connect(this.nonOwner).copyCode(this.sfc, address)).to.be.revertedWith(
56-
'Ownable: caller is not the owner',
57-
);
56+
await expect(
57+
this.nodeDriverAuth.connect(this.nonOwner).copyCode(this.sfc, address),
58+
).to.be.revertedWithCustomError(this.nodeDriverAuth, 'NotOwner');
5859
});
5960
});
6061

@@ -66,8 +67,9 @@ describe('NodeDriver', () => {
6667
});
6768

6869
it('Should revert when not owner', async function () {
69-
await expect(this.nodeDriverAuth.connect(this.nonOwner).updateNetworkVersion(1)).to.be.revertedWith(
70-
'Ownable: caller is not the owner',
70+
await expect(this.nodeDriverAuth.connect(this.nonOwner).updateNetworkVersion(1)).to.be.revertedWithCustomError(
71+
this.nodeDriverAuth,
72+
'NotOwner',
7173
);
7274
});
7375
});
@@ -78,8 +80,9 @@ describe('NodeDriver', () => {
7880
});
7981

8082
it('Should revert when not owner', async function () {
81-
await expect(this.nodeDriverAuth.connect(this.nonOwner).advanceEpochs(10)).to.be.revertedWith(
82-
'Ownable: caller is not the owner',
83+
await expect(this.nodeDriverAuth.connect(this.nonOwner).advanceEpochs(10)).to.be.revertedWithCustomError(
84+
this.nodeDriverAuth,
85+
'NotOwner',
8386
);
8487
});
8588
});

test/SFC.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,16 @@ describe('SFC', () => {
290290
});
291291

292292
it('Should revert when transferring ownership if not owner', async function () {
293-
await expect(this.sfc.connect(this.user).transferOwnership(ethers.ZeroAddress)).to.be.revertedWith(
294-
'Ownable: caller is not the owner',
293+
await expect(this.sfc.connect(this.user).transferOwnership(ethers.ZeroAddress)).to.be.revertedWithCustomError(
294+
this.nodeDriverAuth,
295+
'NotOwner',
295296
);
296297
});
297298

298299
it('Should revert when transferring ownership to zero address', async function () {
299-
await expect(this.sfc.transferOwnership(ethers.ZeroAddress)).to.be.revertedWith(
300-
'Ownable: new owner is the zero address',
300+
await expect(this.sfc.transferOwnership(ethers.ZeroAddress)).to.be.revertedWithCustomError(
301+
this.nodeDriverAuth,
302+
'ZeroAddress',
301303
);
302304
});
303305
});

0 commit comments

Comments
 (0)