Skip to content

Commit 79a6f45

Browse files
fix(core): restore structured __repr__ on A2A exceptions (a2aproject#1348)
# Description Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - Important Prefixes for [release-please](https://github.com/googleapis/release-please): - `fix:` which represents bug fixes, and correlates to a [SemVer](https://semver.org/) patch. - `feat:` represents a new feature, and correlates to a SemVer minor. - `feat!:`, or `fix!:`, `refactor!:`, etc., which represent a breaking change (indicated by the `!`) and will result in a SemVer major. - [x] Ensure the tests and linter pass (Run `bash scripts/format.sh` from the repository root to format) - [x] Appropriate docs were updated (if necessary) Fixes a2aproject#1347 🦕 ## Problem a2aproject#683 added `__repr__` methods to the client exception classes (a2aproject#679). The client error refactor in a2aproject#761 replaced those classes with the `A2AError`-based hierarchy, and the `__repr__` methods were not carried over. Both domain and client errors now fall back to the default `Exception.__repr__`, which only wraps `str()` output and loses structured attributes: ```python >>> repr(AgentCardResolutionError('Failed to fetch agent card', status_code=404)) "AgentCardResolutionError('Failed to fetch agent card')" # Loses status_code >>> repr(InvalidParamsError('Invalid field', data={'field': 'id'})) "InvalidParamsError('Invalid field')" # Loses data ``` ## Fix Added `__repr__` back, following the same pattern as a2aproject#683: `A2AError` (`src/a2a/utils/errors.py`): Exposes `message` and `data` as keyword arguments. All domain errors (`TaskNotFoundError`, `InvalidParamsError`, ...) and client errors (`A2AClientError`, `A2AClientTimeoutError`) inherit it. ```python >>> repr(InvalidParamsError('Invalid field', data={'field': 'id'})) "InvalidParamsError(message='Invalid field', data={'field': 'id'})" ``` `AgentCardResolutionError` (`src/a2a/client/errors.py`): Exposes `status_code`, as `A2AClientHTTPError` did in a2aproject#683. ```python >>> repr(AgentCardResolutionError('Failed to fetch agent card', status_code=404)) "AgentCardResolutionError(status_code=404, message='Failed to fetch agent card')" ``` This change does not alter traceback formatting, `__str__` output, exception hierarchy, catching behavior, or any existing error handling logic. It is purely additive and intended to improve debuggability via `repr()` in REPLs, logs, and structured logging contexts. ## Test Tests were added to the existing suite at `tests/client/test_errors.py`, and to a new `tests/utils/test_a2a_errors.py` for `A2AError` and domain errors (`test_errors.py` is already taken by the client suite), keeping the same structure and conventions already in place. No existing test was modified or removed, all additions are purely additive. Happy to adapt the test approach based on maintainer feedback. Co-authored-by: mykytanetipa <mykytanetipa@google.com>
1 parent 91929cd commit 79a6f45

4 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎src/a2a/client/errors.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ def __init__(self, message: str, status_code: int | None = None) -> None:
1414
super().__init__(message)
1515
self.status_code = status_code
1616

17+
def __repr__(self) -> str:
18+
"""Return a developer-friendly representation exposing the status code."""
19+
return (
20+
f'{self.__class__.__name__}('
21+
f'status_code={self.status_code!r}, '
22+
f'message={self.message!r})'
23+
)
24+
1725

1826
class A2AClientTimeoutError(A2AClientError):
1927
"""Exception for timeout errors during a request."""

‎src/a2a/utils/errors.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ def __init__(self, message: str | None = None, data: dict | None = None):
2727
self.data = data
2828
super().__init__(self.message)
2929

30+
def __repr__(self) -> str:
31+
"""Return a developer-friendly representation exposing message and data."""
32+
return (
33+
f'{self.__class__.__name__}('
34+
f'message={self.message!r}, '
35+
f'data={self.data!r})'
36+
)
37+
3038

3139
class TaskNotFoundError(A2AError):
3240
"""Exception raised when a task is not found."""

‎tests/client/test_errors.py‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import pytest
22

3-
from a2a.client import A2AClientError
3+
from a2a.client import (
4+
A2AClientError,
5+
A2AClientTimeoutError,
6+
AgentCardResolutionError,
7+
)
48

59

610
class TestA2AClientError:
@@ -23,3 +27,34 @@ def test_raising_base_error(self) -> None:
2327
raise A2AClientError('Generic client error')
2428

2529
assert str(excinfo.value) == 'Generic client error'
30+
31+
32+
class TestClientErrorRepr:
33+
"""Test cases for the __repr__ of client exception classes."""
34+
35+
@pytest.mark.parametrize(
36+
('error', 'expected'),
37+
[
38+
(
39+
A2AClientError('Network communication error'),
40+
"A2AClientError(message='Network communication error', data=None)",
41+
),
42+
(
43+
A2AClientTimeoutError('Client Request timed out'),
44+
"A2AClientTimeoutError(message='Client Request timed out', data=None)",
45+
),
46+
(
47+
AgentCardResolutionError('Failed to fetch agent card', 404),
48+
'AgentCardResolutionError(status_code=404, '
49+
"message='Failed to fetch agent card')",
50+
),
51+
(
52+
AgentCardResolutionError('Invalid agent card JSON'),
53+
'AgentCardResolutionError(status_code=None, '
54+
"message='Invalid agent card JSON')",
55+
),
56+
],
57+
)
58+
def test_repr(self, error: A2AClientError, expected: str) -> None:
59+
"""Test that repr exposes structured attributes as keyword arguments."""
60+
assert repr(error) == expected

‎tests/utils/test_a2a_errors.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
3+
from a2a.utils.errors import (
4+
A2AError,
5+
InvalidParamsError,
6+
TaskNotFoundError,
7+
VersionNotSupportedError,
8+
)
9+
10+
11+
class TestA2AErrorRepr:
12+
"""Test cases for the __repr__ of A2AError and its subclasses."""
13+
14+
@pytest.mark.parametrize(
15+
('error', 'expected'),
16+
[
17+
(A2AError(), "A2AError(message='A2A Error', data=None)"),
18+
(
19+
TaskNotFoundError(),
20+
"TaskNotFoundError(message='Task not found', data=None)",
21+
),
22+
(
23+
TaskNotFoundError('Task abc not found'),
24+
"TaskNotFoundError(message='Task abc not found', data=None)",
25+
),
26+
(
27+
InvalidParamsError('Invalid field', data={'field': 'id'}),
28+
"InvalidParamsError(message='Invalid field', data={'field': 'id'})",
29+
),
30+
(
31+
VersionNotSupportedError(data={'supported': ['1.0']}),
32+
"VersionNotSupportedError(message='Version not supported', "
33+
"data={'supported': ['1.0']})",
34+
),
35+
],
36+
)
37+
def test_repr(self, error: A2AError, expected: str) -> None:
38+
"""Test that repr exposes message and data as keyword arguments."""
39+
assert repr(error) == expected
40+
41+
def test_str_unchanged(self) -> None:
42+
"""Test that __str__ still returns only the message."""
43+
error = InvalidParamsError('Invalid field', data={'field': 'id'})
44+
assert str(error) == 'Invalid field'

0 commit comments

Comments
 (0)