Skip to content

Commit cf862e5

Browse files
authored
fix error msg when params not match (#296)
* fix error msg when params not match * add UT * rename ut name * add ctor encode test * fix typo
1 parent c80c791 commit cf862e5

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

sdk-abi/src/main/java/org/fisco/bcos/sdk/abi/ABICodec.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ public String encodeMethodFromString(String ABI, String methodName, List<String>
198198
+ " , supported functions are: "
199199
+ contractABIDefinition.getFunctions().keySet());
200200
}
201-
Throwable cause = null;
202201
for (ABIDefinition abiDefinition : methods) {
203202
if (abiDefinition.getInputs().size() == params.size()) {
204203
ABIObject inputABIObject = abiObjectFactory.createInputObject(abiDefinition);
@@ -207,15 +206,13 @@ public String encodeMethodFromString(String ABI, String methodName, List<String>
207206
String methodId = abiDefinition.getMethodId(cryptoSuite);
208207
return methodId + abiCodecJsonWrapper.encode(inputABIObject, params).encode();
209208
} catch (Exception e) {
210-
cause = e;
211209
logger.error(" exception in encodeMethodFromString : {}", e.getMessage());
212210
}
213211
}
214212
}
215213

216214
String errorMsg =
217-
" cannot encode in encodeMethodFromString with appropriate interface ABI, cause:"
218-
+ cause.getMessage();
215+
" cannot encode in encodeMethodFromString with appropriate interface ABI, make sure params match";
219216
logger.error(errorMsg);
220217
throw new ABICodecException(errorMsg);
221218
}

sdk-abi/src/test/java/org/fisco/bcos/sdk/test/abi/ABICodecTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,42 @@ public void testEncodeFromString() {
346346
}
347347
}
348348

349+
@Test
350+
public void testEncodeFromStringWithInvalidParams() {
351+
List<String> args = new ArrayList<String>();
352+
ABICodec abiCodec = new ABICodec(Utils.getCryptoSuite());
353+
try {
354+
abiCodec.encodeMethodFromString(abiDesc, "test", args);
355+
Assert.fail();
356+
} catch (Exception e) {
357+
Assert.assertTrue(e instanceof ABICodecException);
358+
}
359+
}
360+
361+
@Test
362+
public void testEncodeConsctructor() {
363+
List<String> args = new ArrayList<String>();
364+
ABICodec abiCodec = new ABICodec(Utils.getCryptoSuite());
365+
try {
366+
abiCodec.encodeConstructorFromString(abiDesc, "BIN", args);
367+
} catch (Exception e) {
368+
Assert.fail(e.getMessage());
369+
}
370+
}
371+
@Test
372+
public void testEncodeConsctructorWithInvalidParams() {
373+
List<String> args = new ArrayList<String>();
374+
args.add("invalid");
375+
ABICodec abiCodec = new ABICodec(Utils.getCryptoSuite());
376+
try {
377+
abiCodec.encodeConstructorFromString(abiDesc, "BIN", args);
378+
Assert.fail();
379+
} catch (Exception e) {
380+
Assert.assertTrue(e instanceof ABICodecException);
381+
}
382+
}
383+
384+
349385
@Test
350386
public void testEncodeByInterface() {
351387
ABICodec abiCodec = new ABICodec(Utils.getCryptoSuite());

0 commit comments

Comments
 (0)