forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_error_handlers.py
More file actions
92 lines (66 loc) · 2.59 KB
/
test_error_handlers.py
File metadata and controls
92 lines (66 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Tests for a2a.utils.error_handlers module."""
from unittest.mock import patch
import pytest
from a2a.types import (
InternalError,
InvalidRequestError,
MethodNotFoundError,
TaskNotFoundError,
)
from a2a.utils.error_handlers import (
A2AErrorToHttpStatus,
rest_error_handler,
rest_stream_error_handler,
)
from a2a.utils.errors import ServerError
class MockJSONResponse:
def __init__(self, content, status_code):
self.content = content
self.status_code = status_code
@pytest.mark.asyncio
async def test_rest_error_handler_server_error():
"""Test rest_error_handler with ServerError."""
error = InvalidRequestError(message='Bad request')
@rest_error_handler
async def failing_func():
raise ServerError(error=error)
with patch('a2a.utils.error_handlers.JSONResponse', MockJSONResponse):
result = await failing_func()
assert isinstance(result, MockJSONResponse)
assert result.status_code == 400
assert result.content == {'message': 'Bad request'}
@pytest.mark.asyncio
async def test_rest_error_handler_unknown_exception():
"""Test rest_error_handler with unknown exception."""
@rest_error_handler
async def failing_func():
raise ValueError('Unexpected error')
with patch('a2a.utils.error_handlers.JSONResponse', MockJSONResponse):
result = await failing_func()
assert isinstance(result, MockJSONResponse)
assert result.status_code == 500
assert result.content == {'message': 'unknown exception'}
@pytest.mark.asyncio
async def test_rest_stream_error_handler_server_error():
"""Test rest_stream_error_handler with ServerError."""
error = InternalError(message='Internal server error')
@rest_stream_error_handler
async def failing_stream():
raise ServerError(error=error)
with pytest.raises(ServerError) as exc_info:
await failing_stream()
assert exc_info.value.error == error
@pytest.mark.asyncio
async def test_rest_stream_error_handler_reraises_exception():
"""Test rest_stream_error_handler reraises other exceptions."""
@rest_stream_error_handler
async def failing_stream():
raise RuntimeError('Stream failed')
with pytest.raises(RuntimeError, match='Stream failed'):
await failing_stream()
def test_a2a_error_to_http_status_mapping():
"""Test A2AErrorToHttpStatus mapping."""
assert A2AErrorToHttpStatus[InvalidRequestError] == 400
assert A2AErrorToHttpStatus[MethodNotFoundError] == 404
assert A2AErrorToHttpStatus[TaskNotFoundError] == 404
assert A2AErrorToHttpStatus[InternalError] == 500