Repository navigation
Add error classes - #22
Conversation
| @@ -0,0 +1,4 @@ | |||
| package io.a2a.spec; | |||
|
|
|||
| public class A2AClientError extends Exception { | |||
There was a problem hiding this comment.
Not 100% sure here, the Python implementation is:
class A2AClientError(Exception):
pass
There was a problem hiding this comment.
Maybe we could add a constructor here that just takes a string message?
There was a problem hiding this comment.
pass means "no implementation" in python. It's like an interface in Java, just for typing
There was a problem hiding this comment.
well bad explanation...since it extends Exception class, in python it inherits also its constructor. IOW pass just pass anything to the super class. So this code should work
def a_method():
raise A2AClientError("Connection failed")
try:
amehod()
except A2AClientError as e:
print(f"Exception message: {e}")
and it should print something like: Exception message: Connection failed
| @@ -0,0 +1,4 @@ | |||
| package io.a2a.spec; | |||
|
|
|||
| public class A2AClientJSONError extends A2AClientError { | |||
There was a problem hiding this comment.
Not totally sure here either, here is the Python
class A2AClientJSONError(A2AClientError):
def __init__(self, message: str):
self.message = message
super().__init__(f'JSON Error: {message}')
There was a problem hiding this comment.
Looks like they're just prefixing the message string with "JSON Error". We can always do that later if we think it's useful. I see now that A2AClientJSONError isn't in a2a.json so what we do here is mostly up to us.
| @@ -0,0 +1,5 @@ | |||
| package io.a2a.spec; | |||
|
|
|||
| public class MissingAPIKeyError extends Exception { | |||
There was a problem hiding this comment.
Same doubt as for the A2AClientError
| * Represents a JSONRPC error. | ||
| */ | ||
| public record JSONRPCError(int code, String message, Object data) { | ||
| public class JSONRPCError { |
There was a problem hiding this comment.
Just to check, any reason to convert this to a class? This is immutable and we're using Jackson to convert to/from JSON. It looks like the MCP Java SDK used records for errors like this.
There was a problem hiding this comment.
Ah, I see now, we need to use a class because the other errors need to extend this one, right?
There was a problem hiding this comment.
@fjuma Yeah, I wasn't able to inherit from Records. It says they are final
|
|
||
| public class UnsupportedOperationError extends JSONRPCError { | ||
|
|
||
| public UnsupportedOperationError(int code, String message, Object data) { |
There was a problem hiding this comment.
For classes like this that extend JSONRPCError, I believe we'll need the @JsonCreator annotation on the constructor and @JsonProperty("blah") annotations on each parameter to be able to successfully create the object from JSON. Let me know if I'm missing something though.
There was a problem hiding this comment.
I need to read up a bit on this :-)
| public class UnsupportedOperationError extends JSONRPCError { | ||
|
|
||
| public UnsupportedOperationError(int code, String message, Object data) { | ||
| super(code, message, data); |
There was a problem hiding this comment.
Looks like the spec specifies default values for code and message:
https://github.com/google/A2A/blob/main/specification/json/a2a.json#L1450
https://github.com/google/A2A/blob/main/samples/python/common/types.py#L303-L304
Looks like the other error types also have defaults that we should use as well.
| Assert.checkNotNullParam("message", message); | ||
| this.code = code; | ||
| this.message = message; | ||
| this.data = data; |
There was a problem hiding this comment.
Is data needed here? I don't see it in the python implementation for this one.
|
Hi @fjuma PR updated. For the root JSONRPCError class I wasn't sure if this should have the annotations, or if it is always one of the subclasses. I originally made it abstract, but looking at spec json, a lot of the Response classes simply have the error as JSONRPCError. So, I think the errors must be getting deserialized on the client, and thus ended up putting the annotations on this class anyway. I see in the test it is simply deserialized as JSONRPCError. So I'm now wondering a bit if the sub-classes actually need the annotations, but don't know yet whether they are needed or not. Perhaps they are used for the serialization. |
| @JsonProperty("message") String message, | ||
| @JsonProperty("data") Object data) { | ||
| super( | ||
| defaultIfNull(code, -32702), |
| @JsonProperty("message") String message, | ||
| @JsonProperty("data") Object data) { | ||
| super( | ||
| defaultIfNull(code, -32704), |
|
Looks good, just a couple small comments on the error codes.
For https://github.com/fjuma/a2a-java-sdk/blob/main/src/main/java/io/a2a/spec/Part.java#L12-L21 For |
Fixes #1