|
| 1 | +import json |
| 2 | + |
1 | 3 | import httpx |
2 | 4 | import pytest |
3 | 5 |
|
4 | | -from render_sdk.client.errors import ClientError, ServerError, TimeoutError |
| 6 | +from render_sdk.client.errors import ClientError, RenderError, ServerError, TimeoutError |
5 | 7 | from render_sdk.client.util import ( |
| 8 | + _handle_wrapper_exception, |
6 | 9 | handle_api_error, |
7 | 10 | handle_http_error, |
8 | | - handle_http_errors, |
9 | 11 | handle_httpx_exception, |
10 | 12 | handle_storage_http_error, |
11 | 13 | retry_with_backoff, |
@@ -169,19 +171,39 @@ def test_handle_api_error_fallback_when_content_is_not_json(): |
169 | 171 | handle_api_error(response, "API request") |
170 | 172 |
|
171 | 173 |
|
172 | | -@pytest.mark.asyncio |
173 | | -async def test_decorator_handle_http_errors(): |
174 | | - @handle_http_errors("test operation") |
175 | | - async def test_operation(): |
176 | | - return Response( |
177 | | - status_code=400, |
178 | | - content=b"", |
179 | | - headers={}, |
180 | | - parsed=Error(message="Bad request"), |
181 | | - ) |
| 174 | +def test_handle_wrapper_exception_reraises_render_error(): |
| 175 | + with pytest.raises(RenderError, match="already a render error"): |
| 176 | + _handle_wrapper_exception(RenderError("already a render error"), "op") |
| 177 | + |
| 178 | + |
| 179 | +def test_handle_wrapper_exception_httpx_request_error(): |
| 180 | + exc = httpx.TimeoutException("timed out") |
| 181 | + with pytest.raises(TimeoutError, match="op timed out"): |
| 182 | + _handle_wrapper_exception(exc, "op") |
| 183 | + |
| 184 | + |
| 185 | +def test_handle_wrapper_exception_json_decode_error(): |
| 186 | + exc = json.JSONDecodeError("Expecting value", "Unauthorized\n", 0) |
| 187 | + with pytest.raises( |
| 188 | + RenderError, match="server returned a non-JSON response: Unauthorized" |
| 189 | + ): |
| 190 | + _handle_wrapper_exception(exc, "create task") |
| 191 | + |
| 192 | + |
| 193 | +def test_handle_wrapper_exception_json_decode_error_empty_body(): |
| 194 | + exc = json.JSONDecodeError("Expecting value", "", 0) |
| 195 | + with pytest.raises( |
| 196 | + RenderError, match="server returned a non-JSON response: empty response" |
| 197 | + ): |
| 198 | + _handle_wrapper_exception(exc, "create task") |
| 199 | + |
182 | 200 |
|
183 | | - with pytest.raises(ClientError, match="test operation failed: Bad request"): |
184 | | - await test_operation() |
| 201 | +def test_handle_wrapper_exception_unexpected_error(): |
| 202 | + exc = ValueError("something weird") |
| 203 | + with pytest.raises( |
| 204 | + RenderError, match="failed with unexpected error: something weird" |
| 205 | + ): |
| 206 | + _handle_wrapper_exception(exc, "op") |
185 | 207 |
|
186 | 208 |
|
187 | 209 | class TestHandleStorageHttpError: |
|
0 commit comments