Skip to content

Commit e97df4c

Browse files
authored
optimize: Improved JSONRPCErrorDeserializer (#136)
This PR includes the following changes: 1. Improved the `JSONRPCErrorDeserializer` so that it can return the correct subclass based on the error code, instead of always returning `JSONRPCError`. 2. Added corresponding unit tests. Fixes #23 Signed-off-by: Sun Yuhan <[email protected]> Co-authored-by: Sun Yuhan <[email protected]>
1 parent b80a9d9 commit e97df4c

13 files changed

+124
-13
lines changed

core/src/main/java/io/a2a/spec/ContentTypeNotSupportedError.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1111
@JsonIgnoreProperties(ignoreUnknown = true)
1212
public class ContentTypeNotSupportedError extends JSONRPCError {
13+
14+
public final static Integer DEFAULT_CODE = -32005;
15+
1316
@JsonCreator
1417
public ContentTypeNotSupportedError(
1518
@JsonProperty("code") Integer code,
1619
@JsonProperty("message") String message,
1720
@JsonProperty("data") Object data) {
1821
super(
19-
defaultIfNull(code, -32005),
22+
defaultIfNull(code, DEFAULT_CODE),
2023
defaultIfNull(message, "Incompatible content types"),
2124
data);
2225
}

core/src/main/java/io/a2a/spec/InternalError.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1111
@JsonIgnoreProperties(ignoreUnknown = true)
1212
public class InternalError extends JSONRPCError {
13+
14+
public final static Integer DEFAULT_CODE = -32603;
15+
1316
@JsonCreator
1417
public InternalError(
1518
@JsonProperty("code") Integer code,
1619
@JsonProperty("message") String message,
1720
@JsonProperty("data") Object data) {
1821
super(
19-
defaultIfNull(code, -32603),
22+
defaultIfNull(code, DEFAULT_CODE),
2023
defaultIfNull(message, "Internal Error"),
2124
data);
2225
}

core/src/main/java/io/a2a/spec/InvalidAgentResponseError.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1414
@JsonIgnoreProperties(ignoreUnknown = true)
1515
public class InvalidAgentResponseError extends JSONRPCError {
16+
17+
public final static Integer DEFAULT_CODE = -32006;
18+
1619
@JsonCreator
1720
public InvalidAgentResponseError(
1821
@JsonProperty("code") Integer code,
1922
@JsonProperty("message") String message,
2023
@JsonProperty("data") Object data) {
2124
super(
22-
defaultIfNull(code, -32006),
25+
defaultIfNull(code, DEFAULT_CODE),
2326
defaultIfNull(message, "Invalid agent response"),
2427
data);
2528
}

core/src/main/java/io/a2a/spec/InvalidParamsError.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1111
@JsonIgnoreProperties(ignoreUnknown = true)
1212
public class InvalidParamsError extends JSONRPCError {
13+
14+
public final static Integer DEFAULT_CODE = -32602;
15+
1316
@JsonCreator
1417
public InvalidParamsError(
1518
@JsonProperty("code") Integer code,
1619
@JsonProperty("message") String message,
1720
@JsonProperty("data") Object data) {
1821
super(
19-
defaultIfNull(code, -32602),
22+
defaultIfNull(code, DEFAULT_CODE),
2023
defaultIfNull(message, "Invalid parameters"),
2124
data);
2225
}

core/src/main/java/io/a2a/spec/InvalidRequestError.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
@JsonIgnoreProperties(ignoreUnknown = true)
1212
public class InvalidRequestError extends JSONRPCError {
1313

14+
public final static Integer DEFAULT_CODE = -32600;
15+
1416
public InvalidRequestError() {
1517
this(null, null, null);
1618
}
@@ -21,7 +23,7 @@ public InvalidRequestError(
2123
@JsonProperty("message") String message,
2224
@JsonProperty("data") Object data) {
2325
super(
24-
defaultIfNull(code, -32600),
26+
defaultIfNull(code, DEFAULT_CODE),
2527
defaultIfNull(message, "Request payload validation error"),
2628
data);
2729
}

core/src/main/java/io/a2a/spec/JSONParseError.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
@JsonIgnoreProperties(ignoreUnknown = true)
1212
public class JSONParseError extends JSONRPCError implements A2AError {
1313

14+
public final static Integer DEFAULT_CODE = -32700;
15+
1416
public JSONParseError() {
1517
this(null, null, null);
1618
}
@@ -25,7 +27,7 @@ public JSONParseError(
2527
@JsonProperty("message") String message,
2628
@JsonProperty("data") Object data) {
2729
super(
28-
defaultIfNull(code, -32700),
30+
defaultIfNull(code, DEFAULT_CODE),
2931
defaultIfNull(message, "Invalid JSON payload"),
3032
data);
3133
}

core/src/main/java/io/a2a/spec/JSONRPCErrorDeserializer.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.a2a.spec;
22

33
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Map;
46

57
import com.fasterxml.jackson.core.JsonParser;
68
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -10,6 +12,22 @@
1012

1113
public class JSONRPCErrorDeserializer extends StdDeserializer<JSONRPCError> {
1214

15+
private static final Map<Integer, TriFunction<Integer, String, Object, JSONRPCError>> ERROR_MAP = new HashMap<>();
16+
17+
static {
18+
ERROR_MAP.put(JSONParseError.DEFAULT_CODE, JSONParseError::new);
19+
ERROR_MAP.put(InvalidRequestError.DEFAULT_CODE, InvalidRequestError::new);
20+
ERROR_MAP.put(MethodNotFoundError.DEFAULT_CODE, MethodNotFoundError::new);
21+
ERROR_MAP.put(InvalidParamsError.DEFAULT_CODE, InvalidParamsError::new);
22+
ERROR_MAP.put(InternalError.DEFAULT_CODE, InternalError::new);
23+
ERROR_MAP.put(PushNotificationNotSupportedError.DEFAULT_CODE, PushNotificationNotSupportedError::new);
24+
ERROR_MAP.put(UnsupportedOperationError.DEFAULT_CODE, UnsupportedOperationError::new);
25+
ERROR_MAP.put(ContentTypeNotSupportedError.DEFAULT_CODE, ContentTypeNotSupportedError::new);
26+
ERROR_MAP.put(InvalidAgentResponseError.DEFAULT_CODE, InvalidAgentResponseError::new);
27+
ERROR_MAP.put(TaskNotCancelableError.DEFAULT_CODE, TaskNotCancelableError::new);
28+
ERROR_MAP.put(TaskNotFoundError.DEFAULT_CODE, TaskNotFoundError::new);
29+
}
30+
1331
public JSONRPCErrorDeserializer() {
1432
this(null);
1533
}
@@ -26,6 +44,16 @@ public JSONRPCError deserialize(JsonParser jsonParser, DeserializationContext co
2644
String message = node.get("message").asText();
2745
JsonNode dataNode = node.get("data");
2846
Object data = dataNode != null ? jsonParser.getCodec().treeToValue(dataNode, Object.class) : null;
29-
return new JSONRPCError(code, message, data);
47+
TriFunction<Integer, String, Object, JSONRPCError> constructor = ERROR_MAP.get(code);
48+
if (constructor != null) {
49+
return constructor.apply(code, message, data);
50+
} else {
51+
return new JSONRPCError(code, message, data);
52+
}
53+
}
54+
55+
@FunctionalInterface
56+
private interface TriFunction<A, B, C, R> {
57+
R apply(A a, B b, C c);
3058
}
3159
}

core/src/main/java/io/a2a/spec/MethodNotFoundError.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1111
@JsonIgnoreProperties(ignoreUnknown = true)
1212
public class MethodNotFoundError extends JSONRPCError {
13+
14+
public final static Integer DEFAULT_CODE = -32601;
15+
1316
@JsonCreator
1417
public MethodNotFoundError(
1518
@JsonProperty("code") Integer code,
1619
@JsonProperty("message") String message,
1720
@JsonProperty("data") Object data) {
1821
super(
19-
defaultIfNull(code, -32601),
22+
defaultIfNull(code, DEFAULT_CODE),
2023
defaultIfNull(message, "Method not found"),
2124
data);
2225
}
2326

2427
public MethodNotFoundError() {
25-
this(-32601, null, null);
28+
this(DEFAULT_CODE, null, null);
2629
}
2730
}

core/src/main/java/io/a2a/spec/PushNotificationNotSupportedError.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1111
@JsonIgnoreProperties(ignoreUnknown = true)
1212
public class PushNotificationNotSupportedError extends JSONRPCError {
13+
14+
public final static Integer DEFAULT_CODE = -32003;
15+
1316
@JsonCreator
1417
public PushNotificationNotSupportedError(
1518
@JsonProperty("code") Integer code,
1619
@JsonProperty("message") String message,
1720
@JsonProperty("data") Object data) {
1821
super(
19-
defaultIfNull(code, -32003),
22+
defaultIfNull(code, DEFAULT_CODE),
2023
defaultIfNull(message, "Push Notification is not supported"),
2124
data);
2225
}

core/src/main/java/io/a2a/spec/TaskNotCancelableError.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1111
@JsonIgnoreProperties(ignoreUnknown = true)
1212
public class TaskNotCancelableError extends JSONRPCError {
13+
14+
public final static Integer DEFAULT_CODE = -32002;
15+
1316
public TaskNotCancelableError() {
1417
this(null, null, null);
1518
}
@@ -20,7 +23,7 @@ public TaskNotCancelableError(
2023
@JsonProperty("message") String message,
2124
@JsonProperty("data") Object data) {
2225
super(
23-
defaultIfNull(code, -32002),
26+
defaultIfNull(code, DEFAULT_CODE),
2427
defaultIfNull(message, "Task cannot be canceled"),
2528
data);
2629
}

0 commit comments

Comments
 (0)