88from google .protobuf .json_format import MessageToDict
99from jsonrpc .jsonrpc2 import JSONRPC20Response
1010
11+ from a2a .server .apps .jsonrpc .errors import (
12+ InternalError as JSONRPCInternalError ,
13+ )
14+ from a2a .server .apps .jsonrpc .errors import (
15+ JSONRPCError ,
16+ )
1117from a2a .server .context import ServerCallContext
1218from a2a .server .request_handlers .request_handler import RequestHandler
1319from a2a .types .a2a_pb2 import (
2733)
2834from a2a .utils import proto_utils
2935from a2a .utils .errors import (
36+ A2AException ,
3037 AuthenticatedExtendedCardNotConfiguredError ,
38+ ContentTypeNotSupportedError ,
3139 InternalError ,
40+ InvalidAgentResponseError ,
41+ InvalidParamsError ,
42+ InvalidRequestError ,
43+ MethodNotFoundError ,
44+ PushNotificationNotSupportedError ,
3245 ServerError ,
46+ TaskNotCancelableError ,
3347 TaskNotFoundError ,
48+ UnsupportedOperationError ,
3449)
3550from a2a .utils .helpers import validate
3651from a2a .utils .telemetry import SpanKind , trace_class
3954logger = logging .getLogger (__name__ )
4055
4156
57+ EXCEPTION_MAP : dict [type [A2AException ], type [JSONRPCError ]] = {
58+ TaskNotFoundError : JSONRPCError ,
59+ TaskNotCancelableError : JSONRPCError ,
60+ PushNotificationNotSupportedError : JSONRPCError ,
61+ UnsupportedOperationError : JSONRPCError ,
62+ ContentTypeNotSupportedError : JSONRPCError ,
63+ InvalidAgentResponseError : JSONRPCError ,
64+ AuthenticatedExtendedCardNotConfiguredError : JSONRPCError ,
65+ InternalError : JSONRPCInternalError ,
66+ InvalidParamsError : JSONRPCError ,
67+ InvalidRequestError : JSONRPCError ,
68+ MethodNotFoundError : JSONRPCError ,
69+ }
70+
71+ ERROR_CODE_MAP : dict [type [A2AException ], int ] = {
72+ TaskNotFoundError : - 32001 ,
73+ TaskNotCancelableError : - 32002 ,
74+ PushNotificationNotSupportedError : - 32003 ,
75+ UnsupportedOperationError : - 32004 ,
76+ ContentTypeNotSupportedError : - 32005 ,
77+ InvalidAgentResponseError : - 32006 ,
78+ AuthenticatedExtendedCardNotConfiguredError : - 32007 ,
79+ InvalidParamsError : - 32602 ,
80+ InvalidRequestError : - 32600 ,
81+ MethodNotFoundError : - 32601 ,
82+ }
83+
84+
4285def _build_success_response (
4386 request_id : str | int | None , result : Any
4487) -> dict [str , Any ]:
@@ -47,10 +90,22 @@ def _build_success_response(
4790
4891
4992def _build_error_response (
50- request_id : str | int | None , error : Any
93+ request_id : str | int | None , error : Exception
5194) -> dict [str , Any ]:
5295 """Build a JSON-RPC error response dict."""
53- error_dict = error .model_dump (exclude_none = True )
96+ jsonrpc_error : JSONRPCError
97+ if isinstance (error , A2AException ):
98+ error_type = type (error )
99+ model_class = EXCEPTION_MAP .get (error_type , JSONRPCInternalError )
100+ code = ERROR_CODE_MAP .get (error_type , - 32603 )
101+ jsonrpc_error = model_class (
102+ code = code ,
103+ message = str (error ),
104+ )
105+ else :
106+ jsonrpc_error = JSONRPCInternalError (message = str (error ))
107+
108+ error_dict = jsonrpc_error .model_dump (exclude_none = True )
54109 return JSONRPC20Response (error = error_dict , _id = request_id ).data
55110
56111
0 commit comments