Skip to content

Commit

Permalink
change exception type;
Browse files Browse the repository at this point in the history
add javadoc comment;
improve test;
  • Loading branch information
esaulpaugh committed Feb 10, 2025
1 parent c212545 commit 8ce456d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/main/java/com/esaulpaugh/headlong/abi/ABIJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ public static <T extends ABIObject> List<T> parseElements(int flags, String arra
return parseArray(reader(arrayJson), types, flags);
}

/**
* Reads and parses the value of the key "abi" as a contract ABI json array.
*
* @param flags {@link ABIType#FLAGS_NONE} (recommended) or {@link ABIType#FLAG_LEGACY_DECODE}
* @param objectJson the json object containing the "abi" field
* @param types the types to allow in the returned {@link List}
* @return the list of ABI objects
* @param <T> the element type
*/
public static <T extends ABIObject> List<T> parseABIField(int flags, String objectJson, Set<TypeEnum> types) {
try (final JsonReader reader = reader(objectJson)) {
reader.beginObject();
Expand All @@ -111,7 +120,7 @@ public static <T extends ABIObject> List<T> parseABIField(int flags, String obje
}
reader.skipValue();
}
throw new IllegalStateException("abi key not found");
throw new IllegalArgumentException("abi key not found");
} catch (IOException io) {
throw new IllegalStateException(io);
}
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/com/esaulpaugh/headlong/abi/ABIJSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,10 @@ public void testUserDefinedValueTypes() {
}

@Test
public void testParseABIField() {
public void testParseABIField() throws Throwable {
assertThrown(IllegalArgumentException.class, "abi key not found", () -> ABIJSON.parseABIField(FLAGS_NONE, "{}", ABIJSON.ALL));
assertThrown(IllegalStateException.class, () -> ABIJSON.parseABIField(FLAGS_NONE, "{\"abi\":null}", ABIJSON.ALL));
assertEquals(0, ABIJSON.parseABIField(FLAGS_NONE, "{\"abi\":[]}", ABIJSON.ALL).size());
final String json = "{\n" +
" \"abi\": [\n" +
" {\n" +
Expand Down

0 comments on commit 8ce456d

Please sign in to comment.