@@ -54,16 +54,43 @@ def _build_error_payload(
5454 return {'error' : payload }
5555
5656
57- def _create_error_response (error : Exception ) -> Response :
58- """Helper function to create a JSONResponse for an error."""
57+ def build_rest_error_payload (error : Exception ) -> dict [str , Any ]:
58+ """Build a REST error payload dict from an exception.
59+
60+ Returns:
61+ A dict with the error payload in the standard REST error format.
62+ """
5963 if isinstance (error , A2AError ):
6064 mapping = A2A_REST_ERROR_MAPPING .get (
6165 type (error ), RestErrorMap (500 , 'INTERNAL' , 'INTERNAL_ERROR' )
6266 )
63- http_code = mapping .http_code
64- grpc_status = mapping .grpc_status
65- reason = mapping .reason
67+ # SECURITY WARNING: Data attached to A2AError.data is serialized unaltered and exposed publicly to the client in the REST API response.
68+ metadata = getattr (error , 'data' , None ) or {}
69+ return _build_error_payload (
70+ code = mapping .http_code ,
71+ status = mapping .grpc_status ,
72+ message = getattr (error , 'message' , str (error )),
73+ reason = mapping .reason ,
74+ metadata = metadata ,
75+ )
76+ if isinstance (error , ParseError ):
77+ return _build_error_payload (
78+ code = 400 ,
79+ status = 'INVALID_ARGUMENT' ,
80+ message = str (error ),
81+ reason = 'INVALID_REQUEST' ,
82+ metadata = {},
83+ )
84+ return _build_error_payload (
85+ code = 500 ,
86+ status = 'INTERNAL' ,
87+ message = 'unknown exception' ,
88+ )
6689
90+
91+ def _create_error_response (error : Exception ) -> Response :
92+ """Helper function to create a JSONResponse for an error."""
93+ if isinstance (error , A2AError ):
6794 log_level = (
6895 logging .ERROR
6996 if isinstance (error , InternalError )
@@ -76,42 +103,17 @@ def _create_error_response(error: Exception) -> Response:
76103 getattr (error , 'message' , str (error )),
77104 f', Data={ error .data } ' if error .data else '' ,
78105 )
79-
80- # SECURITY WARNING: Data attached to A2AError.data is serialized unaltered and exposed publicly to the client in the REST API response.
81- metadata = getattr (error , 'data' , None ) or {}
82-
83- return JSONResponse (
84- content = _build_error_payload (
85- code = http_code ,
86- status = grpc_status ,
87- message = getattr (error , 'message' , str (error )),
88- reason = reason ,
89- metadata = metadata ,
90- ),
91- status_code = http_code ,
92- media_type = 'application/json' ,
93- )
94- if isinstance (error , ParseError ):
106+ elif isinstance (error , ParseError ):
95107 logger .warning ('Parse error: %s' , str (error ))
96- return JSONResponse (
97- content = _build_error_payload (
98- code = 400 ,
99- status = 'INVALID_ARGUMENT' ,
100- message = str (error ),
101- reason = 'INVALID_REQUEST' ,
102- metadata = {},
103- ),
104- status_code = 400 ,
105- media_type = 'application/json' ,
106- )
107- logger .exception ('Unknown error occurred' )
108+ else :
109+ logger .exception ('Unknown error occurred' )
110+
111+ payload = build_rest_error_payload (error )
112+ # Extract HTTP status code from the payload
113+ http_code = payload .get ('error' , {}).get ('code' , 500 )
108114 return JSONResponse (
109- content = _build_error_payload (
110- code = 500 ,
111- status = 'INTERNAL' ,
112- message = 'unknown exception' ,
113- ),
114- status_code = 500 ,
115+ content = payload ,
116+ status_code = http_code ,
115117 media_type = 'application/json' ,
116118 )
117119
0 commit comments