Skip to content

Commit caab6a2

Browse files
fix(compat): map v0.3 gRPC errors from their ErrorInfo reason (#1466)
Fixes #1465 When the status message isn't in a2a-python's `"ClassName: message"` form, the v0.3 gRPC client now reads `ErrorInfo.reason`, the same way the v1 gRPC client does. The existing format is still checked first. Against a2a-go's v0.3 gRPC server, get_task and cancel on a missing task now raise `TaskNotFoundError`, and invalid params, unsupported operation and a send to a finished task raise their own types. Errors with no ErrorInfo still raise `A2AClientError`. The new ErrorInfo test in `test_grpc_transport.py` fails on main. tests/compat and tests/client pass. Co-authored-by: mykytanetipa <mykytanetipa@google.com>
1 parent 3932982 commit caab6a2

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

‎src/a2a/compat/v0_3/grpc_transport.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
from collections.abc import AsyncGenerator, Callable
44
from functools import wraps
5-
from typing import Any, NoReturn
5+
from typing import Any, NoReturn, cast
66

77
from a2a.client.errors import A2AClientError, A2AClientTimeoutError
8-
from a2a.utils.errors import JSON_RPC_ERROR_CODE_MAP
8+
from a2a.utils.errors import A2A_REASON_TO_ERROR, JSON_RPC_ERROR_CODE_MAP
99

1010

1111
try:
@@ -18,6 +18,10 @@
1818
) from e
1919

2020

21+
from google.rpc import ( # type: ignore[reportMissingModuleSource]
22+
error_details_pb2,
23+
)
24+
2125
from a2a.client.client import ClientCallContext, ClientConfig
2226
from a2a.client.optionals import Channel
2327
from a2a.client.transports.base import ClientTransport
@@ -33,6 +37,7 @@
3337
from a2a.compat.v0_3.extension_headers import add_legacy_extension_header
3438
from a2a.types import a2a_pb2
3539
from a2a.utils.constants import PROTOCOL_VERSION_0_3, VERSION_HEADER
40+
from a2a.utils.grpc_status import status_from_call
3641
from a2a.utils.telemetry import SpanKind, trace_class
3742

3843

@@ -53,6 +58,18 @@ def _map_grpc_error(e: grpc.aio.AioRpcError) -> NoReturn:
5358
exception_cls = _A2A_ERROR_NAME_TO_CLS.get(error_type_name)
5459
if exception_cls:
5560
raise exception_cls(error_message) from e
61+
62+
# Other v0.3 servers (a2a-go) name the error in ErrorInfo.reason instead.
63+
status = status_from_call(cast('grpc.Call', e))
64+
if status is not None:
65+
for detail in status.details:
66+
# `.DESCRIPTOR` is typed as a py/C union; `.Is` wants a Descriptor.
67+
if detail.Is(error_details_pb2.ErrorInfo.DESCRIPTOR): # ty: ignore[invalid-argument-type]
68+
error_info = error_details_pb2.ErrorInfo()
69+
detail.Unpack(error_info)
70+
exception_cls = A2A_REASON_TO_ERROR.get(error_info.reason)
71+
if error_info.domain == 'a2a-protocol.org' and exception_cls:
72+
raise exception_cls(status.message) from e
5673
raise A2AClientError(f'gRPC Error {e.code().name}: {e.details()}') from e
5774

5875

‎tests/compat/v0_3/test_grpc_transport.py‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,101 @@
11
from unittest.mock import AsyncMock, MagicMock
22

3+
import grpc
34
import pytest
45

56
from a2a.client.client import ClientCallContext
7+
from a2a.client.errors import A2AClientError
68
from a2a.client.optionals import Channel
79
from a2a.compat.v0_3 import a2a_v0_3_pb2
810
from a2a.compat.v0_3.grpc_transport import CompatGrpcTransport
911
from a2a.types.a2a_pb2 import (
12+
GetTaskRequest,
1013
Message,
1114
Role,
1215
SendMessageRequest,
1316
SendMessageResponse,
1417
)
18+
from a2a.utils.errors import TaskNotFoundError, UnsupportedOperationError
19+
from google.protobuf import any_pb2
20+
from google.rpc import error_details_pb2, status_pb2
21+
22+
23+
def _rpc_error(
24+
code: grpc.StatusCode, message: str, reason: str | None = None
25+
) -> grpc.aio.AioRpcError:
26+
trailing = grpc.aio.Metadata()
27+
if reason:
28+
status = status_pb2.Status(code=code.value[0], message=message)
29+
detail = any_pb2.Any()
30+
detail.Pack(
31+
error_details_pb2.ErrorInfo(
32+
reason=reason, domain='a2a-protocol.org'
33+
)
34+
)
35+
status.details.append(detail)
36+
trailing = grpc.aio.Metadata(
37+
('grpc-status-details-bin', status.SerializeToString()),
38+
)
39+
return grpc.aio.AioRpcError(
40+
code=code,
41+
initial_metadata=grpc.aio.Metadata(),
42+
trailing_metadata=trailing,
43+
details=message,
44+
)
45+
46+
47+
def _transport_raising(error: grpc.aio.AioRpcError) -> CompatGrpcTransport:
48+
transport = CompatGrpcTransport(
49+
channel=AsyncMock(spec=Channel), agent_card=None
50+
)
51+
transport.stub = MagicMock()
52+
transport.stub.GetTask = AsyncMock(side_effect=error)
53+
return transport
54+
55+
56+
@pytest.mark.asyncio
57+
async def test_compat_grpc_error_mapped_from_error_info_reason():
58+
"""A v0.3 server that is not a2a-python (a2a-go) names the error in
59+
ErrorInfo.reason, not in a "ClassName: message" status message."""
60+
transport = _transport_raising(
61+
_rpc_error(
62+
grpc.StatusCode.NOT_FOUND,
63+
'failed to get task: task not found',
64+
reason='TASK_NOT_FOUND',
65+
)
66+
)
67+
68+
with pytest.raises(TaskNotFoundError) as excinfo:
69+
await transport.get_task(GetTaskRequest(id='missing'))
70+
71+
assert str(excinfo.value) == 'failed to get task: task not found'
72+
73+
74+
@pytest.mark.asyncio
75+
async def test_compat_grpc_error_mapped_from_class_name_message():
76+
transport = _transport_raising(
77+
_rpc_error(
78+
grpc.StatusCode.FAILED_PRECONDITION,
79+
'UnsupportedOperationError: not supported',
80+
)
81+
)
82+
83+
with pytest.raises(UnsupportedOperationError) as excinfo:
84+
await transport.get_task(GetTaskRequest(id='t1'))
85+
86+
assert str(excinfo.value) == 'not supported'
87+
88+
89+
@pytest.mark.asyncio
90+
async def test_compat_grpc_error_unmapped_stays_client_error():
91+
transport = _transport_raising(
92+
_rpc_error(grpc.StatusCode.UNAVAILABLE, 'connection refused')
93+
)
94+
95+
with pytest.raises(A2AClientError) as excinfo:
96+
await transport.get_task(GetTaskRequest(id='t1'))
97+
98+
assert type(excinfo.value) is A2AClientError
1599

16100

17101
@pytest.mark.asyncio

0 commit comments

Comments
 (0)