|
26 | 26 | TaskState, |
27 | 27 | ) |
28 | 28 | from a2a.utils.constants import TransportProtocol |
29 | | -from a2a.utils.errors import A2A_ERROR_MAPPING |
| 29 | +from a2a.utils.errors import A2A_ERROR_MAPPING, MethodNotFoundError |
30 | 30 | from google.protobuf import json_format |
31 | 31 | from google.protobuf.timestamp_pb2 import Timestamp |
32 | 32 |
|
@@ -152,6 +152,42 @@ async def test_rest_mapped_errors( |
152 | 152 | with pytest.raises(error_cls): |
153 | 153 | await client.send_message(request=params) |
154 | 154 |
|
| 155 | + @pytest.mark.parametrize( |
| 156 | + ('status_code', 'body', 'expected_error'), |
| 157 | + [ |
| 158 | + (401, {'error': 'invalid_token'}, A2AClientError), |
| 159 | + (404, {'error': 'Not Found'}, MethodNotFoundError), |
| 160 | + (502, ['bad gateway'], A2AClientError), |
| 161 | + (500, 'Internal Server Error', A2AClientError), |
| 162 | + (503, None, A2AClientError), |
| 163 | + ], |
| 164 | + ) |
| 165 | + def test_rest_non_status_error_bodies( |
| 166 | + self, |
| 167 | + mock_httpx_client: AsyncMock, |
| 168 | + mock_agent_card: MagicMock, |
| 169 | + status_code, |
| 170 | + body, |
| 171 | + expected_error, |
| 172 | + ): |
| 173 | + """JSON error bodies that are not a google.rpc.Status (e.g. from a proxy) fall back to the HTTP status.""" |
| 174 | + client = RestTransport( |
| 175 | + httpx_client=mock_httpx_client, |
| 176 | + agent_card=mock_agent_card, |
| 177 | + url='http://agent.example.com/api', |
| 178 | + ) |
| 179 | + mock_response = MagicMock(spec=httpx.Response) |
| 180 | + mock_response.status_code = status_code |
| 181 | + mock_response.json.return_value = body |
| 182 | + error = httpx.HTTPStatusError( |
| 183 | + 'Error', |
| 184 | + request=httpx.Request('GET', 'http://test.url'), |
| 185 | + response=mock_response, |
| 186 | + ) |
| 187 | + |
| 188 | + with pytest.raises(expected_error): |
| 189 | + client._handle_http_error(error) |
| 190 | + |
155 | 191 | @pytest.mark.asyncio |
156 | 192 | async def test_send_message_with_timeout_context( |
157 | 193 | self, mock_httpx_client: AsyncMock, mock_agent_card: MagicMock |
|
0 commit comments