## Summary Routes HTTP adapter exceptions to the right error class instead of shoe-horning everything into `UpstreamError`. Addresses Eric's earlier feedback that several exceptions this PR was wrapping as `UpstreamError` didn't satisfy the "something happened with the upstream" claim (local pool exhaustion, client-side request construction, local TLS failures). ### Scope - `UpstreamError` (unchanged) — upstream responded with an HTTP status code. - **`NetworkTransportError`** (new sibling in `arcade-core`) — no complete response was received. `status_code=None`. Three kinds: `NETWORK_TRANSPORT_RUNTIME_TIMEOUT`, `_UNREACHABLE`, `_UNMAPPED`. - **`FatalToolError`** (existing) — client construction bugs (`InvalidURL`, `UnsupportedProtocol`, `MissingSchema`, `InvalidHeader`, `LocalProtocolError`, …) and local TLS/cert config failures. Never retried. --- ## Before / After (per Eric's request) Shows the error payload a tool produces for each exception, before this PR vs. after. "Before" = current `main` (exceptions without real HTTP responses fall through to the generic `@tool` `FatalToolError` catch-all with `message=str(exc)`). ### No-response transport failures | Exception | Before — class / message / kind | After — class / message / kind | |---|---|---| | `httpx.PoolTimeout` | `FatalToolError` — `str(exc)` leaks raw detail — `TOOL_RUNTIME_FATAL`, not retryable | `NetworkTransportError` — `"HTTP request timed out before a complete response was received."` — `NETWORK_TRANSPORT_RUNTIME_TIMEOUT`, **retryable** | | `httpx.ConnectTimeout` | same as above | same as PoolTimeout — `TIMEOUT`, retryable | | `httpx.ConnectError` (refused / DNS) | `FatalToolError` — `str(exc)` | `NetworkTransportError` — `"HTTP request failed before reaching the upstream service."` — `UNREACHABLE`, retryable | | `httpx.RemoteProtocolError` (upstream sent bad HTTP) | `FatalToolError` — `str(exc)` | `NetworkTransportError` — same message as ConnectError — `UNREACHABLE`, retryable | | `httpx.DecodingError` | `FatalToolError` — `str(exc)` | `NetworkTransportError` — `"HTTP response from upstream could not be decoded."` — `UNMAPPED`, retryable | | `httpx.TooManyRedirects` | `FatalToolError` — `str(exc)` | `NetworkTransportError` — `"HTTP redirect limit exceeded before a final response was received."` — `UNMAPPED`, **not** retryable | ### Client construction / local env bugs | Exception | Before | After | |---|---|---| | `httpx.UnsupportedProtocol`, `httpx.InvalidURL`, `httpx.LocalProtocolError` | `FatalToolError` with `message=str(exc)` (may leak scheme / URL content) | `FatalToolError` — `"Tool constructed an invalid HTTP request — likely a tool-authoring bug."` — `TOOL_RUNTIME_FATAL`, not retryable | | `requests.MissingSchema`, `InvalidURL`, `InvalidHeader`, `InvalidSchema`, `InvalidProxyURL`, `URLRequired` | same as above | same as above | | `requests.SSLError` | `FatalToolError` — `str(exc)` often contains raw cert chain detail | `FatalToolError` — `"TLS handshake failed — likely a local certificate or trust configuration issue."` — `TOOL_RUNTIME_FATAL`, not retryable | ### Real HTTP response errors (UNCHANGED — same behavior) | Exception | Class | Message | Kind | Retryable | |---|---|---|---|---| | `httpx.HTTPStatusError` 404 | `UpstreamError` | `"Upstream HTTP request failed (Not Found, client error)."` | `UPSTREAM_RUNTIME_NOT_FOUND` | No | | `httpx.HTTPStatusError` 429 (w/ Retry-After: 60) | `UpstreamRateLimitError` | `"Upstream HTTP request failed (Too Many Requests, client error). Retry after 60 second(s)."` | `UPSTREAM_RUNTIME_RATE_LIMIT` | Yes | | `httpx.HTTPStatusError` 500 | `UpstreamError` | `"Upstream HTTP request failed (Internal Server Error, server error)."` | `UPSTREAM_RUNTIME_SERVER_ERROR` | Yes | ### What's no longer in the message - Raw exception `str(exc)` output (which frequently includes the full URL with query-string tokens, connection pool details, or cert chains) is **no longer the agent-facing `message`**. It's preserved in `developer_message` for server-side diagnostics. - The misleading "Upstream HTTP…" prefix is gone from network-transport and construction-bug messages. Those messages now honestly describe what happened on the tool side. - For 429s without a `Retry-After` header, we still show "Retry after N seconds." (pre-existing behavior; see follow-up notes). --- ## Companion PRs - [ArcadeAI/arcade-mcp#823](https://github.com/ArcadeAI/arcade-mcp/pull/823) — introduces `NetworkTransportError` in `arcade-core` - [ArcadeAI/monorepo#911](https://github.com/ArcadeAI/monorepo/pull/911) — adds the 3 `ErrorKind` constants to the Go engine and Datadog dashboards - [ArcadeAI/docs#920](https://github.com/ArcadeAI/docs/pull/920) — documents the new hierarchy and adapter routing ## Follow-ups (out of scope for this PR) A short investigation surfaced several pre-existing issues that are worth fixing separately. A full list is in `NETWORK_TRANSPORT_ERROR_FOLLOWUPS.md` (shared offline). Summary: 1. `requests.HTTPError` with `response is None` returns `None` from the adapter; should fall through to the `NetworkTransportError(UNMAPPED)` fallback instead of becoming a generic `FatalToolError`. 2. `developer_message` can leak URL query strings (and therefore tokens) since it stores raw `str(exc)`. 3. `_sanitize_uri` does not strip userinfo (credentials in URL path). 4. `_parse_retry_ms` misinterprets epoch-style `x-ratelimit-reset` headers. 5. 429 responses without `Retry-After` synthesize a fabricated "Retry after 1 second(s)." suffix. 6. `UPSTREAM_RUNTIME_VALIDATION_ERROR` is defined but never emitted. 7. `UpstreamError` silently accepts out-of-range status codes. 8. `requests.HTTPError` branch re-extracts `request_url` / `request_method` inconsistently (dead work). ## Test plan - [x] Existing `libs/tests/sdk/test_httpx_adapter.py` + `test_graphql_adapter.py` updated; every no-response / construction-bug test asserts the new class + kind + `can_retry`. - [x] Full test suite passes locally. - [x] mypy clean on `arcade-core`, `arcade-tdk`, `arcade-mcp-server`. - [x] Smoke-tested 21 exception routing cases end-to-end against real httpx / requests exceptions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Changes core error classification and retryability for `httpx`/`requests`/GraphQL transport failures, which can affect tool retry behavior and telemetry. Risk is mitigated by extensive new/updated tests covering the new mappings and privacy expectations. > > **Overview** > **Improves error adapter behavior to be more semantically correct and privacy-safe.** The HTTP adapter now distinguishes real HTTP responses (`UpstreamError`/`UpstreamRateLimitError`) from no-response failures (`NetworkTransportError` with `ErrorKind` + retryability) and from client construction/local TLS issues (`FatalToolError`). > > **Reduces sensitive data exposure in agent-facing messages.** Status-based errors now emit standardized messages derived from status phrase/class, while preserving raw exception detail in `developer_message`; Google/Microsoft/Slack fallback paths similarly switch to `unhandled <ExceptionType>` messages and move `str(exc)` into `developer_message`. GraphQL transport connection/protocol errors are reclassified from `UpstreamError` (502) to `NetworkTransportError`, and transport/server messages are standardized. > > Bumps `arcade-tdk` version to `3.8.0` and expands/updates the SDK test suite to assert new classes, `kind`, `can_retry`, request metadata extraction, and privacy behavior. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 1041cb1bec4fa3b0bae3e7c6b860b84cf376cf9a. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
511 lines
21 KiB
Python
511 lines
21 KiB
Python
from datetime import datetime, timezone
|
|
from unittest.mock import Mock, patch
|
|
|
|
from arcade_core.errors import UpstreamError, UpstreamRateLimitError
|
|
from arcade_tdk.providers.google.error_adapter import GoogleErrorAdapter
|
|
|
|
|
|
class TestGoogleErrorAdapter:
|
|
"""Test the Google error adapter functionality."""
|
|
|
|
def setup_method(self):
|
|
self.adapter = GoogleErrorAdapter()
|
|
|
|
def _create_mock_errors_module(self):
|
|
"""Create a mock errors module with all necessary error classes."""
|
|
|
|
class MockHttpError(Exception):
|
|
pass
|
|
|
|
class MockBatchError(Exception):
|
|
pass
|
|
|
|
class MockInvalidJsonError(Exception):
|
|
pass
|
|
|
|
class MockUnknownApiNameOrVersion(Exception):
|
|
pass
|
|
|
|
class MockUnacceptableMimeTypeError(Exception):
|
|
pass
|
|
|
|
class MockMediaUploadSizeError(Exception):
|
|
pass
|
|
|
|
class MockInvalidChunkSizeError(Exception):
|
|
pass
|
|
|
|
class MockInvalidNotificationError(Exception):
|
|
pass
|
|
|
|
mock_errors = Mock()
|
|
mock_errors.HttpError = MockHttpError
|
|
mock_errors.BatchError = MockBatchError
|
|
mock_errors.InvalidJsonError = MockInvalidJsonError
|
|
mock_errors.UnknownApiNameOrVersion = MockUnknownApiNameOrVersion
|
|
mock_errors.UnacceptableMimeTypeError = MockUnacceptableMimeTypeError
|
|
mock_errors.MediaUploadSizeError = MockMediaUploadSizeError
|
|
mock_errors.InvalidChunkSizeError = MockInvalidChunkSizeError
|
|
mock_errors.InvalidNotificationError = MockInvalidNotificationError
|
|
|
|
return mock_errors
|
|
|
|
def test_adapter_slug(self):
|
|
"""Test that the adapter has the correct slug."""
|
|
assert GoogleErrorAdapter.slug == "_google_api_client"
|
|
|
|
def test_sanitize_uri_removes_query_params(self):
|
|
"""Test URI sanitization removes query parameters."""
|
|
uri = "https://www.googleapis.com/drive/v3/files/123?key=secret&fields=id,name"
|
|
result = self.adapter._sanitize_uri(uri)
|
|
assert result == "https://www.googleapis.com/drive/v3/files/123"
|
|
|
|
def test_sanitize_uri_removes_fragments(self):
|
|
"""Test URI sanitization removes fragments."""
|
|
uri = "https://www.googleapis.com/gmail/v1/users/me/messages#inbox"
|
|
result = self.adapter._sanitize_uri(uri)
|
|
assert result == "https://www.googleapis.com/gmail/v1/users/me/messages"
|
|
|
|
def test_sanitize_uri_handles_trailing_slashes(self):
|
|
"""Test URI sanitization handles trailing slashes."""
|
|
uri = "https://www.googleapis.com///sheets/v4/spreadsheets///"
|
|
result = self.adapter._sanitize_uri(uri)
|
|
assert result == "https://www.googleapis.com/sheets/v4/spreadsheets"
|
|
|
|
def test_parse_retry_after_with_seconds(self):
|
|
"""Test parsing retry-after header with seconds value."""
|
|
mock_error = Mock()
|
|
mock_error.resp = Mock()
|
|
mock_error.resp.headers = {"Retry-After": "120"}
|
|
|
|
result = self.adapter._parse_retry_after(mock_error)
|
|
assert result == 120_000
|
|
|
|
def test_parse_retry_after_with_lowercase_header(self):
|
|
"""Test parsing retry-after header with lowercase key."""
|
|
mock_error = Mock()
|
|
mock_error.resp = Mock()
|
|
mock_error.resp.headers = {"retry-after": "60"}
|
|
|
|
result = self.adapter._parse_retry_after(mock_error)
|
|
assert result == 60_000
|
|
|
|
def test_parse_retry_after_with_date_format(self):
|
|
"""Test parsing retry-after header with absolute date format."""
|
|
future_date = "Mon, 01 Jan 2029 12:00:00 GMT"
|
|
mock_error = Mock()
|
|
mock_error.resp = Mock()
|
|
mock_error.resp.headers = {"Retry-After": future_date}
|
|
|
|
with patch("arcade_tdk.providers.google.error_adapter.datetime") as mock_datetime:
|
|
parsed_date = datetime(2029, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
|
|
mock_datetime.strptime.return_value = parsed_date
|
|
|
|
# Mock datetime.now() to return a time before the parsed date
|
|
current_time = datetime(2029, 1, 1, 11, 58, 0, tzinfo=timezone.utc)
|
|
mock_datetime.now.return_value = current_time
|
|
mock_datetime.timezone = timezone
|
|
|
|
result = self.adapter._parse_retry_after(mock_error)
|
|
assert result == 120_000 # 2 minute diff
|
|
|
|
def test_parse_retry_after_no_headers(self):
|
|
"""Test parsing retry-after when no headers are present."""
|
|
mock_error = Mock()
|
|
mock_error.resp = Mock()
|
|
mock_error.resp.headers = {}
|
|
|
|
result = self.adapter._parse_retry_after(mock_error)
|
|
assert result == 1_000
|
|
|
|
def test_parse_retry_after_no_resp_attribute(self):
|
|
"""Test parsing retry-after when error has no resp attribute."""
|
|
mock_error = Mock()
|
|
del mock_error.resp
|
|
|
|
result = self.adapter._parse_retry_after(mock_error)
|
|
assert result == 1_000 # defaults to 1 second
|
|
|
|
def test_parse_retry_after_invalid_date(self):
|
|
"""Test parsing retry-after with invalid date format falls back to default."""
|
|
mock_error = Mock()
|
|
mock_error.resp = Mock()
|
|
mock_error.resp.headers = {"Retry-After": "invalid-date"}
|
|
|
|
result = self.adapter._parse_retry_after(mock_error)
|
|
assert result == 1_000
|
|
|
|
def test_map_http_error_basic(self):
|
|
"""Test mapping basic HTTP error."""
|
|
mock_error = Mock()
|
|
mock_error.status_code = 404
|
|
mock_error.reason = "Not Found"
|
|
mock_error.error_details = None
|
|
mock_error.uri = "https://www.googleapis.com/drive/v3/files/missing"
|
|
mock_error.method_ = "get"
|
|
|
|
result = self.adapter._map_http_error(mock_error)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert not isinstance(result, UpstreamRateLimitError)
|
|
assert result.status_code == 404
|
|
assert result.message == "Upstream Google API error: Not Found"
|
|
assert result.extra["service"] == "_google_api_client"
|
|
assert result.extra["endpoint"] == "https://www.googleapis.com/drive/v3/files/missing"
|
|
assert result.extra["http_method"] == "GET"
|
|
|
|
def test_map_http_error_with_string_details(self):
|
|
"""Test mapping HTTP error with string error details."""
|
|
mock_error = Mock()
|
|
mock_error.status_code = 400
|
|
mock_error.reason = "Bad Request"
|
|
mock_error.error_details = "Invalid field value"
|
|
mock_error.uri = "https://www.googleapis.com/sheets/v4/spreadsheets"
|
|
mock_error.method_ = "post"
|
|
|
|
result = self.adapter._map_http_error(mock_error)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 400
|
|
assert "Invalid field value" in result.message
|
|
assert result.extra["service"] == "_google_api_client"
|
|
assert result.extra["http_method"] == "POST"
|
|
|
|
def test_map_http_error_with_structured_details(self):
|
|
"""Test mapping HTTP error with structured error details."""
|
|
mock_error = Mock()
|
|
mock_error.status_code = 403
|
|
mock_error.reason = "Forbidden"
|
|
mock_error.error_details = {"error": {"code": 403, "message": "Insufficient permissions"}}
|
|
mock_error.uri = "https://www.googleapis.com/drive/v3/files"
|
|
mock_error.method_ = "delete"
|
|
|
|
result = self.adapter._map_http_error(mock_error)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 403
|
|
assert result.message == "Upstream Google API error: Forbidden"
|
|
assert "Upstream Google API error details" in result.developer_message
|
|
assert result.extra["http_method"] == "DELETE"
|
|
|
|
def test_map_http_error_rate_limit(self):
|
|
"""Test mapping 429 rate limit error."""
|
|
mock_error = Mock()
|
|
mock_error.status_code = 429
|
|
mock_error.reason = "Too Many Requests"
|
|
mock_error.error_details = None
|
|
mock_error.uri = "https://www.googleapis.com/gmail/v1/users/me/messages"
|
|
mock_error.method_ = "get"
|
|
mock_error.resp = Mock()
|
|
mock_error.resp.headers = {"Retry-After": "30"}
|
|
|
|
result = self.adapter._map_http_error(mock_error)
|
|
|
|
assert isinstance(result, UpstreamRateLimitError)
|
|
assert result.retry_after_ms == 30_000
|
|
assert result.message == "Upstream Google API error: Too Many Requests"
|
|
assert result.extra["service"] == "_google_api_client"
|
|
assert result.extra["endpoint"] == "https://www.googleapis.com/gmail/v1/users/me/messages"
|
|
assert result.extra["http_method"] == "GET"
|
|
|
|
def test_map_http_error_no_reason(self):
|
|
"""Test mapping HTTP error with no reason."""
|
|
mock_error = Mock()
|
|
mock_error.status_code = 500
|
|
mock_error.reason = None
|
|
mock_error.error_details = None
|
|
mock_error.uri = "https://www.googleapis.com/calendar/v3/calendars"
|
|
mock_error.method_ = "post"
|
|
|
|
result = self.adapter._map_http_error(mock_error)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 500
|
|
assert result.message == "Upstream Google API error: HTTP 500 error"
|
|
|
|
def test_map_http_error_missing_attributes(self):
|
|
"""Test mapping HTTP error without uri and method attributes."""
|
|
mock_error = Mock()
|
|
mock_error.status_code = 503
|
|
mock_error.reason = "Service Unavailable"
|
|
mock_error.error_details = None
|
|
|
|
# Remove uri and method_ attributes
|
|
if hasattr(mock_error, "uri"):
|
|
del mock_error.uri
|
|
if hasattr(mock_error, "method_"):
|
|
del mock_error.method_
|
|
|
|
result = self.adapter._map_http_error(mock_error)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 503
|
|
assert result.extra["service"] == "_google_api_client"
|
|
assert "endpoint" not in result.extra
|
|
assert "http_method" not in result.extra
|
|
|
|
def test_handle_http_errors_with_http_error(self):
|
|
"""Test handling HttpError exceptions."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
|
|
# Create mock error instance
|
|
mock_error = mock_errors.HttpError()
|
|
mock_error.status_code = 401
|
|
mock_error.reason = "Unauthorized"
|
|
mock_error.error_details = None
|
|
mock_error.uri = "https://www.googleapis.com/drive/v3/files"
|
|
mock_error.method_ = "get"
|
|
|
|
result = self.adapter._handle_http_errors(mock_error, mock_errors)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 401
|
|
assert result.message == "Upstream Google API error: Unauthorized"
|
|
|
|
def test_handle_http_errors_with_batch_error_with_status(self):
|
|
"""Test handling BatchError with response status."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
|
|
# Create mock error instance
|
|
mock_error = mock_errors.BatchError()
|
|
mock_error.reason = "Batch operation failed"
|
|
mock_error.error_details = None
|
|
mock_error.resp = Mock()
|
|
mock_error.resp.status = 400
|
|
|
|
result = self.adapter._handle_http_errors(mock_error, mock_errors)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 400
|
|
assert result.message == "Upstream Google API error: Batch operation failed"
|
|
|
|
def test_handle_http_errors_with_batch_error_no_status(self):
|
|
"""Test handling BatchError without response status."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
|
|
# Create mock error instance
|
|
mock_error = mock_errors.BatchError()
|
|
mock_error.reason = "Batch operation failed"
|
|
|
|
result = self.adapter._handle_http_errors(mock_error, mock_errors)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 500
|
|
assert (
|
|
result.message == "Upstream Google API batch operation failed: Batch operation failed"
|
|
)
|
|
assert result.extra["service"] == "google_api"
|
|
assert result.extra["error_type"] == "BatchError"
|
|
|
|
def test_handle_http_errors_unhandled_exception(self):
|
|
"""Test handling non-HTTP exceptions returns None."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
|
|
# Create a non-HTTP exception
|
|
mock_error = ValueError("Not an HTTP error")
|
|
|
|
result = self.adapter._handle_http_errors(mock_error, mock_errors)
|
|
assert result is None
|
|
|
|
def test_handle_other_errors_invalid_json_error(self):
|
|
"""Test handling InvalidJsonError."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
mock_error = mock_errors.InvalidJsonError("Invalid JSON response")
|
|
|
|
result = self.adapter._handle_other_errors(mock_error, mock_errors)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 502
|
|
assert result.message == "Upstream Google API returned invalid JSON response"
|
|
assert result.developer_message == "Invalid JSON response"
|
|
assert result.extra["service"] == "_google_api_client"
|
|
assert result.extra["error_type"] == "InvalidJsonError"
|
|
|
|
def test_handle_other_errors_unknown_api_name_or_version(self):
|
|
"""Test handling UnknownApiNameOrVersion."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
mock_error = mock_errors.UnknownApiNameOrVersion("Unknown API: nonexistent/v1")
|
|
|
|
result = self.adapter._handle_other_errors(mock_error, mock_errors)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 404
|
|
assert result.message == "Upstream Google API error: Unknown API name or version"
|
|
assert result.developer_message == "Unknown API: nonexistent/v1"
|
|
assert result.extra["error_type"] == "UnknownApiNameOrVersion"
|
|
|
|
def test_handle_other_errors_unacceptable_mime_type_error(self):
|
|
"""Test handling UnacceptableMimeTypeError."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
mock_error = mock_errors.UnacceptableMimeTypeError("MIME type not supported")
|
|
|
|
result = self.adapter._handle_other_errors(mock_error, mock_errors)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 400
|
|
assert (
|
|
result.message == "Upstream Google API error: Unacceptable MIME type for this operation"
|
|
)
|
|
assert result.developer_message == "MIME type not supported"
|
|
assert result.extra["error_type"] == "UnacceptableMimeTypeError"
|
|
|
|
def test_handle_other_errors_media_upload_size_error(self):
|
|
"""Test handling MediaUploadSizeError."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
mock_error = mock_errors.MediaUploadSizeError("File size exceeds 5GB limit")
|
|
|
|
result = self.adapter._handle_other_errors(mock_error, mock_errors)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 400
|
|
assert result.message == "Upstream Google API error: Media file size exceeds allowed limit"
|
|
assert result.developer_message == "File size exceeds 5GB limit"
|
|
assert result.extra["error_type"] == "MediaUploadSizeError"
|
|
|
|
def test_handle_other_errors_invalid_chunk_size_error(self):
|
|
"""Test handling InvalidChunkSizeError."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
mock_error = mock_errors.InvalidChunkSizeError("Chunk size must be multiple of 256KB")
|
|
|
|
result = self.adapter._handle_other_errors(mock_error, mock_errors)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 400
|
|
assert result.message == "Upstream Google API error: Invalid chunk size specified"
|
|
assert result.developer_message == "Chunk size must be multiple of 256KB"
|
|
assert result.extra["error_type"] == "InvalidChunkSizeError"
|
|
|
|
def test_handle_other_errors_invalid_notification_error(self):
|
|
"""Test handling InvalidNotificationError."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
mock_error = mock_errors.InvalidNotificationError("Invalid webhook URL")
|
|
|
|
result = self.adapter._handle_other_errors(mock_error, mock_errors)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 400
|
|
assert result.message == "Upstream Google API error: Invalid notification configuration"
|
|
assert result.developer_message == "Invalid webhook URL"
|
|
assert result.extra["error_type"] == "InvalidNotificationError"
|
|
|
|
def test_handle_other_errors_unhandled_exception(self):
|
|
"""Test handling non-Google API exceptions returns None."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
|
|
# Create a non-Google API exception
|
|
mock_error = ValueError("Not a Google API error")
|
|
|
|
result = self.adapter._handle_other_errors(mock_error, mock_errors)
|
|
assert result is None
|
|
|
|
def test_from_exception_googleapiclient_not_installed(self, caplog):
|
|
"""Test handling when googleapiclient is not installed."""
|
|
with (
|
|
patch("arcade_tdk.providers.google.error_adapter.logger") as mock_logger,
|
|
patch.dict("sys.modules", {"googleapiclient": None}),
|
|
patch(
|
|
"builtins.__import__",
|
|
side_effect=ImportError("No module named 'googleapiclient'"),
|
|
),
|
|
):
|
|
mock_exc = Exception("test exception")
|
|
result = self.adapter.from_exception(mock_exc)
|
|
|
|
assert result is None
|
|
mock_logger.info.assert_called_once()
|
|
warning_message = mock_logger.info.call_args[0][0]
|
|
assert "'googleapiclient' is not installed" in warning_message
|
|
assert "_google_api_client" in warning_message
|
|
|
|
def test_from_exception_http_error_handling(self):
|
|
"""Test full from_exception flow with HTTP error."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
|
|
# Create mock error instance
|
|
mock_error = mock_errors.HttpError()
|
|
mock_error.status_code = 403
|
|
mock_error.reason = "Forbidden"
|
|
mock_error.error_details = None
|
|
mock_error.uri = "https://www.googleapis.com/drive/v3/files"
|
|
mock_error.method_ = "get"
|
|
|
|
# Create mock googleapiclient module
|
|
mock_googleapiclient = Mock()
|
|
mock_googleapiclient.errors = mock_errors
|
|
|
|
with patch.dict(
|
|
"sys.modules",
|
|
{"googleapiclient": mock_googleapiclient, "googleapiclient.errors": mock_errors},
|
|
):
|
|
result = self.adapter.from_exception(mock_error)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 403
|
|
assert result.message == "Upstream Google API error: Forbidden"
|
|
|
|
def test_from_exception_other_error_handling(self):
|
|
"""Test full from_exception flow with other error types."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
mock_error = mock_errors.InvalidJsonError("Invalid JSON")
|
|
|
|
# Create mock googleapiclient module
|
|
mock_googleapiclient = Mock()
|
|
mock_googleapiclient.errors = mock_errors
|
|
|
|
with patch.dict(
|
|
"sys.modules",
|
|
{"googleapiclient": mock_googleapiclient, "googleapiclient.errors": mock_errors},
|
|
):
|
|
result = self.adapter.from_exception(mock_error)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 502
|
|
assert result.message == "Upstream Google API returned invalid JSON response"
|
|
|
|
def test_from_exception_fallback_for_unhandled_google_error(self):
|
|
"""Test fallback handling for unhandled Google API errors."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
|
|
# Create an unhandled Google API error
|
|
class MockUnhandledError(Exception):
|
|
pass
|
|
|
|
mock_error = MockUnhandledError("Some unhandled Google error")
|
|
mock_error.__module__ = "googleapiclient.errors"
|
|
|
|
# Create mock googleapiclient module
|
|
mock_googleapiclient = Mock()
|
|
mock_googleapiclient.errors = mock_errors
|
|
|
|
with patch.dict(
|
|
"sys.modules",
|
|
{"googleapiclient": mock_googleapiclient, "googleapiclient.errors": mock_errors},
|
|
):
|
|
result = self.adapter.from_exception(mock_error)
|
|
|
|
assert isinstance(result, UpstreamError)
|
|
assert result.status_code == 500
|
|
assert result.message == "Upstream Google API error: unhandled MockUnhandledError."
|
|
assert result.developer_message == "Some unhandled Google error"
|
|
assert result.extra["service"] == "_google_api_client"
|
|
assert result.extra["error_type"] == "MockUnhandledError"
|
|
|
|
def test_from_exception_non_google_error(self):
|
|
"""Test handling non-Google API errors returns None."""
|
|
mock_errors = self._create_mock_errors_module()
|
|
|
|
# Create a non-Google API error
|
|
mock_error = ValueError("Not a Google error")
|
|
mock_error.__module__ = "builtins"
|
|
|
|
# Create mock googleapiclient module
|
|
mock_googleapiclient = Mock()
|
|
mock_googleapiclient.errors = mock_errors
|
|
|
|
with patch.dict(
|
|
"sys.modules",
|
|
{"googleapiclient": mock_googleapiclient, "googleapiclient.errors": mock_errors},
|
|
):
|
|
result = self.adapter.from_exception(mock_error)
|
|
|
|
assert result is None
|