Skip to content

Commit 3e639b0

Browse files
committed
replace the Exception with TronError
1 parent ac6bc5f commit 3e639b0

File tree

2 files changed

+32
-63
lines changed

2 files changed

+32
-63
lines changed

chainbase/src/main/java/org/tron/common/utils/LocalWitnesses.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.tron.common.crypto.SignInterface;
2626
import org.tron.common.crypto.SignUtils;
2727
import org.tron.core.config.Parameter.ChainConstant;
28+
import org.tron.core.exception.TronError;
2829

2930
@Slf4j(topic = "app")
3031
public class LocalWitnesses {
@@ -78,13 +79,14 @@ private void validate(String privateKey) {
7879

7980
if (StringUtils.isBlank(privateKey)
8081
|| privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH) {
81-
throw new IllegalArgumentException(
82-
String.format("private key must be %d hex string, actual: %d",
83-
ChainConstant.PRIVATE_KEY_LENGTH,
84-
StringUtils.isBlank(privateKey) ? 0 : privateKey.length()));
82+
throw new TronError(String.format("private key must be %d hex string, actual: %d",
83+
ChainConstant.PRIVATE_KEY_LENGTH,
84+
StringUtils.isBlank(privateKey) ? 0 : privateKey.length()),
85+
TronError.ErrCode.WITNESS_INIT);
8586
}
8687
if (!StringUtil.isHexadecimal(privateKey)) {
87-
throw new IllegalArgumentException("private key must be hex string");
88+
throw new TronError("private key must be hex string",
89+
TronError.ErrCode.WITNESS_INIT);
8890
}
8991
}
9092

framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
package org.tron.core.config.args;
1717

18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertThrows;
20+
import static org.junit.Assert.assertTrue;
1821
import static org.junit.Assert.fail;
1922

2023
import com.google.common.collect.Lists;
@@ -28,6 +31,8 @@
2831
import org.tron.common.utils.PublicMethod;
2932
import org.tron.common.utils.StringUtil;
3033
import org.tron.core.Constant;
34+
import org.tron.core.exception.TronError;
35+
import org.tron.core.exception.TronError.ErrCode;
3136

3237
public class LocalWitnessTest {
3338

@@ -42,7 +47,7 @@ public void setLocalWitness() {
4247
localWitness
4348
.setPrivateKeys(
4449
Lists.newArrayList(
45-
PRIVATE_KEY));
50+
PRIVATE_KEY));
4651
}
4752

4853
@Test
@@ -52,13 +57,13 @@ public void whenSetNullPrivateKey() {
5257
Assert.assertNotNull(localWitness.getPublicKey());
5358
}
5459

55-
@Test(expected = IllegalArgumentException.class)
60+
@Test(expected = TronError.class)
5661
public void whenSetEmptyPrivateKey() {
5762
localWitness.setPrivateKeys(Lists.newArrayList(""));
5863
fail("private key must be 64-bits hex string");
5964
}
6065

61-
@Test(expected = IllegalArgumentException.class)
66+
@Test(expected = TronError.class)
6267
public void whenSetBadFormatPrivateKey() {
6368
localWitness.setPrivateKeys(Lists.newArrayList("a111"));
6469
fail("private key must be 64-bits hex string");
@@ -104,62 +109,24 @@ public void testValidPrivateKeyWithPrefix() {
104109
@Test
105110
public void testInvalidPrivateKey() {
106111
LocalWitnesses localWitnesses = new LocalWitnesses();
112+
String expectedMessage = "private key must be 64 hex string";
113+
assertTronError(localWitnesses, null, expectedMessage);
114+
assertTronError(localWitnesses, "", expectedMessage);
115+
assertTronError(localWitnesses, " ", expectedMessage);
116+
assertTronError(localWitnesses, "11111", expectedMessage);
117+
String expectedMessage2 = "private key must be hex string";
118+
final String privateKey = "11111111111111111111111111111111111111111111111111111111111111 ";
119+
assertTronError(localWitnesses, privateKey, expectedMessage2);
120+
final String privateKey2 = "xy11111111111111111111111111111111111111111111111111111111111111";
121+
assertTronError(localWitnesses, privateKey2, expectedMessage2);
122+
}
107123

108-
try {
109-
localWitnesses.addPrivateKeys(null);
110-
fail("should throw IllegalArgumentException");
111-
} catch (IllegalArgumentException e) {
112-
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
113-
} catch (Exception e) {
114-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
115-
}
116-
117-
try {
118-
localWitnesses.addPrivateKeys("");
119-
fail("should throw IllegalArgumentException");
120-
} catch (IllegalArgumentException e) {
121-
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
122-
} catch (Exception e) {
123-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
124-
}
125-
126-
try {
127-
localWitnesses.addPrivateKeys(" ");
128-
fail("should throw IllegalArgumentException");
129-
} catch (IllegalArgumentException e) {
130-
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
131-
} catch (Exception e) {
132-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
133-
}
134-
135-
try {
136-
localWitnesses.addPrivateKeys("11111");
137-
fail("should throw IllegalArgumentException");
138-
} catch (IllegalArgumentException e) {
139-
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
140-
} catch (Exception e) {
141-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
142-
}
143-
144-
try {
145-
String privateKey = "11111111111111111111111111111111111111111111111111111111111111 ";
146-
localWitnesses.addPrivateKeys(privateKey);
147-
fail("should throw IllegalArgumentException");
148-
} catch (IllegalArgumentException e) {
149-
Assert.assertTrue(e.getMessage().contains("private key must be hex string"));
150-
} catch (Exception e) {
151-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
152-
}
153-
154-
try {
155-
String privateKey = "xy11111111111111111111111111111111111111111111111111111111111111";
156-
localWitnesses.addPrivateKeys(privateKey);
157-
fail("should throw IllegalArgumentException");
158-
} catch (IllegalArgumentException e) {
159-
Assert.assertTrue(e.getMessage().contains("private key must be hex string"));
160-
} catch (Exception e) {
161-
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
162-
}
124+
private void assertTronError(LocalWitnesses localWitnesses, String privateKey,
125+
String expectedMessage) {
126+
TronError thrown = assertThrows(TronError.class,
127+
() -> localWitnesses.addPrivateKeys(privateKey));
128+
assertEquals(ErrCode.WITNESS_INIT, thrown.getErrCode());
129+
assertTrue(thrown.getMessage().contains(expectedMessage));
163130
}
164131

165132
@Test

0 commit comments

Comments
 (0)