# Improvements to Arcade TDK Error Handling
I tried my very best to not make any breaking changes in this PR. So,
you will notice various "Deprecation" notices throughout.
### Instructions for PR reviewers
1. Pull down this PR's branch
2. Pull down the Engine's tool error handling PR's branch
3. Update your installed arcadepy to have the following:
- In `arcadepy/resources/tools/tools.py`, if you want to test out
including stacktraces, then you need to update `ToolsResource.execute`
to accept a `include_error_stacktrace` argument and also include the
"include_error_stacktrace" argument to the POST to the Engine inside of
the function's execute method's body.
- In `arcadepy/types/execute_tool_response.py` add the following enum
```py
class ErrorKind(str, Enum):
"""Error kind that is comprised of
- the who (toolkit, tool, upstream)
- the when (load time, definition parsing time, runtime)
- the what (bad_definition, bad_input, bad_output, retry,
context_required, fatal, etc.)"""
TOOLKIT_LOAD_FAILED = "TOOLKIT_LOAD_FAILED"
TOOL_DEFINITION_BAD_DEFINITION = "TOOL_DEFINITION_BAD_DEFINITION"
TOOL_DEFINITION_BAD_INPUT_SCHEMA = "TOOL_DEFINITION_BAD_INPUT_SCHEMA"
TOOL_DEFINITION_BAD_OUTPUT_SCHEMA = "TOOL_DEFINITION_BAD_OUTPUT_SCHEMA"
TOOL_RUNTIME_BAD_INPUT_VALUE = "TOOL_RUNTIME_BAD_INPUT_VALUE"
TOOL_RUNTIME_BAD_OUTPUT_VALUE = "TOOL_RUNTIME_BAD_OUTPUT_VALUE"
TOOL_RUNTIME_RETRY = "TOOL_RUNTIME_RETRY"
TOOL_RUNTIME_CONTEXT_REQUIRED = "TOOL_RUNTIME_CONTEXT_REQUIRED"
TOOL_RUNTIME_FATAL = "TOOL_RUNTIME_FATAL"
UPSTREAM_RUNTIME_BAD_REQUEST = "UPSTREAM_RUNTIME_BAD_REQUEST"
UPSTREAM_RUNTIME_AUTH_ERROR = "UPSTREAM_RUNTIME_AUTH_ERROR"
UPSTREAM_RUNTIME_NOT_FOUND = "UPSTREAM_RUNTIME_NOT_FOUND"
UPSTREAM_RUNTIME_VALIDATION_ERROR = "UPSTREAM_RUNTIME_VALIDATION_ERROR"
UPSTREAM_RUNTIME_RATE_LIMIT = "UPSTREAM_RUNTIME_RATE_LIMIT"
UPSTREAM_RUNTIME_SERVER_ERROR = "UPSTREAM_RUNTIME_SERVER_ERROR"
UPSTREAM_RUNTIME_UNMAPPED = "UPSTREAM_RUNTIME_UNMAPPED"
UNKNOWN = "UNKNOWN"
```
- In `arcadepy/types/execute_tool_response.py` add the following fields
to OutputError:
```py
kind: ErrorKind
status_code: Optional[int] = None
stacktrace: Optional[str] = None
extra: Optional[dict[str, Any]] = None
```
### Example Client Usage
```py
# Example of handling an upstream rate limit
error = response.output.error
if error and error.kind == ErrorKind.UPSTREAM_RUNTIME_RATE_LIMIT:
sleep_time = error.retry_after_ms / 1000
time.sleep(sleep_time)
# and then execute again
```
```py
# Examples of determining what type of runtime error it is
error = response.output.error
if error:
is_retryable_error = error.kind == ErrorKind.TOOL_RUNTIME_RETRY
is_a_bug_in_the_tool = error.kind == ErrorKind.TOOL_RUNTIME_FATAL
is_additional_context_required = error.kind == ErrorKind.TOOL_RUNTIME_CONTEXT_REQUIRED
```
### Example Tool Usage
```py
# EXAMPLE 1 letting Arcade handle upstream error handling for you
reddit_client.post(params) # Arcade's httpx adapter will handle error handling for you!
# ------------------------------------
# EXAMPLE 2 handling upstream bad request yourself, but letting Arcade handle the rest
try:
reddit_client.post(params)
except httpx.HTTPStatusError as e:
if e.status_code == 400:
raise UpstreamError("My extra custom message) from e
raise
```
```py
# EXAMPLE 1 letting Arcade handle it for you
risky_element = my_risky_list[42] # Arcade will raise a FatalToolError for you
# ------------------------------------
# EXAMPLE 2 handling it yourself for extra flexibility
try:
risky_element = my_risky_list[42]
except IndexError as e:
raise FatalToolError("My extra custom message") from e
```
### Non-runtime Error Message Examples
Example ToolkitLoadError Messages:
```
- [TOOLKIT_LOAD_FAILED] ToolkitLoadError when loading toolkit 'sample_tool': Could not import module mock_module. Reason: Mock import error
- [TOOLKIT_LOAD_FAILED] ToolkitLoadError when loading toolkit 'test_toolkit': Tool 'ValidTool' in toolkit 'test_toolkit' already exists in the catalog.
```
Example ToolDefinitionError Messages
```
- [TOOL_DEFINITION_BAD_DEFINITION] ToolDefinitionError in definition of tool 'tool_missing_description': Tool 'tool_missing_description' is missing a description
- [TOOL_DEFINITION_BAD_DEFINITION] ToolDefinitionError in definition of tool 'tool_with_invalid_secret_type': Secret keys must be strings (error in tool ToolWithInvalidSecretType).
- [TOOL_DEFINITION_BAD_DEFINITION] ToolDefinitionError in definition of tool 'tool_with_empty_secret': Secrets must have a non-empty key (error in tool ToolWithEmptySecret).
- [TOOL_DEFINITION_BAD_DEFINITION] ToolDefinitionError in definition of tool 'tool_with_invalid_metadata_type': Metadata must be strings (error in tool ToolWithInvalidMetadataType).
- [TOOL_DEFINITION_BAD_DEFINITION] ToolDefinitionError in definition of tool 'tool_with_metadata_requiring_auth_without_auth': Tool ToolWithMetadataRequiringAuthWithoutAuth declares metadata key 'client_id', which requires that the tool has an auth requirement, but no auth requirement was provided. Please specify an auth requirement.
- [TOOL_DEFINITION_BAD_DEFINITION] ToolDefinitionError in definition of tool 'tool_with_empty_metadata': Metadata must have a non-empty key (error in tool ToolWithEmptyMetadata).
- [TOOL_DEFINITION_BAD_DEFINITION] ToolDefinitionError in definition of tool 'tool_with_unsupported_param_type': Unsupported parameter type: <class 'test_catalog.MyFancyTestClass'>
```
Example ToolInputSchemaError Messages
```
- [TOOL_DEFINITION_BAD_INPUT_SCHEMA] ToolInputSchemaError in definition of tool 'tool_with_missing_input_parameter_annotation': Parameter 'input_text' is missing a description
- [TOOL_DEFINITION_BAD_INPUT_SCHEMA] ToolInputSchemaError in definition of tool 'tool_with_no_type_annotation': Parameter param has no type annotation.
- [TOOL_DEFINITION_BAD_INPUT_SCHEMA] ToolInputSchemaError in definition of tool 'tool_with_invalid_param_name': Invalid parameter name: '123invalid' is not a valid identifier. Identifiers must start with a letter or underscore, and can only contain letters, digits, or underscores.
- [TOOL_DEFINITION_BAD_INPUT_SCHEMA] ToolInputSchemaError in definition of tool 'tool_with_too_many_annotations': Parameter param: Annotated[str, 'name', 'desc', 'extra'] has too many string annotations. Expected 0, 1, or 2, got 3.
- [TOOL_DEFINITION_BAD_INPUT_SCHEMA] ToolInputSchemaError in definition of tool 'tool_with_required_union_param': Parameter param is a union type. Only optional types are supported.
- [TOOL_DEFINITION_BAD_INPUT_SCHEMA] ToolInputSchemaError in definition of tool 'tool_with_non_callable_default_factory': Default factory for parameter param: Annotated[str, 'Parameter'] = FieldInfo(annotation=NoneType, required=False, default_factory=str) is not callable.
- [TOOL_DEFINITION_BAD_INPUT_SCHEMA] ToolInputSchemaError in definition of tool 'tool_with_multiple_tool_contexts': Only one ToolContext parameter is supported, but tool tool_with_multiple_tool_contexts has multiple.
```
Example ToolOutputSchemaError Messages
```
- [TOOL_DEFINITION_BAD_OUTPUT_SCHEMA] ToolOutputSchemaError in definition of tool 'tool_missing_return_type_hint': Tool 'ToolMissingReturnTypeHint' must have a return type
- [TOOL_DEFINITION_BAD_OUTPUT_SCHEMA] ToolOutputSchemaError in definition of tool 'tool_with_unsupported_output_type': Unsupported output type '<class 'test_catalog.MyFancyTestClass'>'. Only built-in Python types, TypedDicts, Pydantic models, and standard collections are supported as tool output types.
```
### Runtime Error Message Examples
Example Tool Runtime Error Messages
```
- [TOOL_RUNTIME_FATAL] FatalToolError during execution of tool 'get_posts_in_subreddit': list index out of range
- [TOOL_RUNTIME_CONTEXT_REQUIRED] ContextRequiredToolError during execution of tool 'get_posts_in_subreddit': Ambiguous username. Please provide a more specific username
- [TOOL_RUNTIME_RETRY] RetryableToolError during execution of tool 'get_posts_in_subreddit': Retry with subreddit=learnpython or subreddit=learnprogramming
```
Example Upstream Runtime Error Messages
```
- [UPSTREAM_RUNTIME_RATE_LIMIT] UpstreamRateLimitError during execution of tool 'get_posts_in_subreddit': 429 Client Error: Too Many Requests
- [UPSTREAM_RUNTIME_BAD_REQUEST] UpstreamError during execution of tool 'get_posts_in_subreddit': 400 Client Error: Bad request. Missing 'id' parameter.
- [UPSTREAM_RUNTIME_BAD_REQUEST] UpstreamError during execution of tool 'search_files': Upstream Google API error: Invalid value '-23'. Values must be within the range: [value: 1\n, value: 1000\n]
```
228 lines
8.2 KiB
Python
228 lines
8.2 KiB
Python
import logging
|
|
from datetime import datetime, timezone
|
|
from typing import Any
|
|
from urllib.parse import urlparse
|
|
|
|
from arcade_core.errors import (
|
|
ToolRuntimeError,
|
|
UpstreamError,
|
|
UpstreamRateLimitError,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class GoogleErrorAdapter:
|
|
"""Error adapter for Google's API Python Client library."""
|
|
|
|
slug = "_google_api_client"
|
|
|
|
def _sanitize_uri(self, uri: str) -> str:
|
|
"""Strip query params and fragments from URI for privacy."""
|
|
|
|
parsed = urlparse(uri)
|
|
return f"{parsed.scheme}://{parsed.netloc.strip('/')}/{parsed.path.strip('/')}"
|
|
|
|
def _parse_retry_after(self, error: Any) -> int:
|
|
"""
|
|
Extract retry-after from Google API errors.
|
|
Returns milliseconds to wait before retry.
|
|
Defaults to 1000ms if not found.
|
|
|
|
Args:
|
|
error: The Google client error to parse
|
|
|
|
Returns:
|
|
The number of milliseconds to wait before retry
|
|
"""
|
|
if hasattr(error, "resp") and hasattr(error.resp, "headers"):
|
|
headers = error.resp.headers
|
|
|
|
retry_after = headers.get("Retry-After", headers.get("retry-after"))
|
|
if retry_after:
|
|
try:
|
|
# If it's a number, it's seconds
|
|
if retry_after.isdigit():
|
|
return int(retry_after) * 1000
|
|
# Otherwise try to parse as date
|
|
dt = datetime.strptime(retry_after, "%a, %d %b %Y %H:%M:%S %Z")
|
|
return int((dt - datetime.now(timezone.utc)).total_seconds() * 1000)
|
|
except Exception:
|
|
# TODO: Log?
|
|
return 1000
|
|
|
|
return 1000
|
|
|
|
def _map_http_error(self, error: Any) -> ToolRuntimeError | None:
|
|
"""Map Google HttpError to appropriate ToolRuntimeError."""
|
|
status_code = error.status_code
|
|
reason = str(error.reason) if error.reason else f"HTTP {status_code} error"
|
|
|
|
message = f"Upstream Google API error: {reason}"
|
|
|
|
developer_message = None
|
|
if error.error_details:
|
|
# str error details are added to the message
|
|
if isinstance(error.error_details, str):
|
|
message = f"{message} - Details: {error.error_details}"
|
|
else:
|
|
# structured error details are added to the developer message
|
|
developer_message = f"Upstream Google API error details: {error.error_details}"
|
|
|
|
# Build extra metadata
|
|
extra = {
|
|
"service": self.slug,
|
|
}
|
|
|
|
# Try to extract request details if available
|
|
if hasattr(error, "uri"):
|
|
extra["endpoint"] = self._sanitize_uri(error.uri)
|
|
if hasattr(error, "method_"):
|
|
extra["http_method"] = error.method_.upper()
|
|
|
|
# Special case for rate limiting
|
|
if status_code == 429:
|
|
return UpstreamRateLimitError(
|
|
retry_after_ms=self._parse_retry_after(error),
|
|
message=message,
|
|
developer_message=developer_message,
|
|
extra=extra,
|
|
)
|
|
|
|
return UpstreamError(
|
|
message=message,
|
|
status_code=status_code,
|
|
developer_message=developer_message,
|
|
extra=extra,
|
|
)
|
|
|
|
def _handle_http_errors(self, exc: Exception, errors_module: Any) -> ToolRuntimeError | None:
|
|
"""Handle HttpError and its subclasses."""
|
|
if isinstance(exc, errors_module.HttpError):
|
|
return self._map_http_error(exc)
|
|
|
|
if isinstance(exc, errors_module.BatchError):
|
|
# BatchError might not have status_code, so handle carefully
|
|
if hasattr(exc, "resp") and hasattr(exc.resp, "status"):
|
|
exc.status_code = exc.resp.status
|
|
return self._map_http_error(exc)
|
|
else:
|
|
# No status code available, treat as server error
|
|
extra = {
|
|
"service": "google_api",
|
|
"error_type": "BatchError",
|
|
}
|
|
return UpstreamError(
|
|
message=f"Upstream Google API batch operation failed: {exc.reason}",
|
|
status_code=500,
|
|
extra=extra,
|
|
)
|
|
return None
|
|
|
|
def _handle_other_errors(self, exc: Exception, errors_module: Any) -> ToolRuntimeError | None:
|
|
"""Handle non-HTTP Google API errors."""
|
|
if isinstance(exc, errors_module.InvalidJsonError):
|
|
return UpstreamError(
|
|
message="Upstream Google API returned invalid JSON response",
|
|
status_code=502,
|
|
developer_message=str(exc),
|
|
extra={
|
|
"service": self.slug,
|
|
"error_type": "InvalidJsonError",
|
|
},
|
|
)
|
|
|
|
if isinstance(exc, errors_module.UnknownApiNameOrVersion):
|
|
return UpstreamError(
|
|
message="Upstream Google API error: Unknown API name or version",
|
|
status_code=404,
|
|
developer_message=str(exc),
|
|
extra={
|
|
"service": self.slug,
|
|
"error_type": "UnknownApiNameOrVersion",
|
|
},
|
|
)
|
|
|
|
if isinstance(exc, errors_module.UnacceptableMimeTypeError):
|
|
return UpstreamError(
|
|
message="Upstream Google API error: Unacceptable MIME type for this operation",
|
|
status_code=400,
|
|
developer_message=str(exc),
|
|
extra={
|
|
"service": self.slug,
|
|
"error_type": "UnacceptableMimeTypeError",
|
|
},
|
|
)
|
|
|
|
if isinstance(exc, errors_module.MediaUploadSizeError):
|
|
return UpstreamError(
|
|
message="Upstream Google API error: Media file size exceeds allowed limit",
|
|
status_code=400,
|
|
developer_message=str(exc),
|
|
extra={
|
|
"service": self.slug,
|
|
"error_type": "MediaUploadSizeError",
|
|
},
|
|
)
|
|
|
|
if isinstance(exc, errors_module.InvalidChunkSizeError):
|
|
return UpstreamError(
|
|
message="Upstream Google API error: Invalid chunk size specified",
|
|
developer_message=str(exc),
|
|
status_code=400,
|
|
extra={
|
|
"service": self.slug,
|
|
"error_type": "InvalidChunkSizeError",
|
|
},
|
|
)
|
|
|
|
if isinstance(exc, errors_module.InvalidNotificationError):
|
|
return UpstreamError(
|
|
message="Upstream Google API error: Invalid notification configuration",
|
|
developer_message=str(exc),
|
|
status_code=400,
|
|
extra={
|
|
"service": self.slug,
|
|
"error_type": "InvalidNotificationError",
|
|
},
|
|
)
|
|
|
|
return None
|
|
|
|
def from_exception(self, exc: Exception) -> ToolRuntimeError | None:
|
|
"""
|
|
Translate a Google API client exception into a ToolRuntimeError.
|
|
"""
|
|
# Lazy import the Google API client errors module to avoid import errors for toolkits that don't use googleapiclient
|
|
try:
|
|
from googleapiclient import errors
|
|
except ImportError:
|
|
logger.info(
|
|
f"'googleapiclient' is not installed in the toolkit's environment, "
|
|
f"so the '{self.slug}' adapter was not used to handle the upstream error"
|
|
)
|
|
return None
|
|
|
|
# Try HTTP errors first
|
|
result = self._handle_http_errors(exc, errors)
|
|
if result:
|
|
return result
|
|
|
|
# Then try other error types
|
|
result = self._handle_other_errors(exc, errors)
|
|
if result:
|
|
return result
|
|
|
|
# Failsafe for any unhandled Google API client errors that are not mapped above
|
|
if hasattr(exc, "__module__") and exc.__module__ == "googleapiclient.errors":
|
|
return UpstreamError(
|
|
message=f"Upstream Google API error: {exc}",
|
|
status_code=500,
|
|
extra={
|
|
"service": self.slug,
|
|
"error_type": exc.__class__.__name__,
|
|
},
|
|
)
|
|
|
|
# Not a Google API client error
|
|
return None
|