The GAME SDK provides a comprehensive error handling system to help you handle errors gracefully and provide meaningful feedback to users.
GameSDKError
├── APIError
│ └── AuthenticationError
├── ConfigurationError
├── ValidationError
├── FunctionExecutionError
├── WorkerError
└── AgentError
Base exception class for all GAME SDK errors. All other SDK exceptions inherit from this class.
Raised when there's an error communicating with the GAME API.
try:
result = post(base_url, api_key, "endpoint", data)
except APIError as e:
print(f"Status code: {e.status_code}")
print(f"Response: {e.response}")Raised when there's an authentication error with the API.
try:
token = get_access_token(api_key)
except AuthenticationError as e:
print("Invalid API key or authentication failed")Raised when there's an error in the SDK configuration.
try:
config.set("invalid_key", "value")
except ConfigurationError as e:
print(f"Configuration error: {e}")Raised when there's an error validating input data.
try:
create_agent(base_url, api_key, "", "description", "goal")
except ValidationError as e:
print(f"Invalid input: {e}")Raised when there's an error executing a function.
try:
result = function.execute()
except FunctionExecutionError as e:
print(f"Function '{e.function_name}' failed: {e}")
if e.original_error:
print(f"Original error: {e.original_error}")Raised when there's an error with worker operations.
try:
workers = create_workers(base_url, api_key, worker_list)
except WorkerError as e:
print(f"Worker error: {e}")Raised when there's an error with agent operations.
try:
agent = create_agent(base_url, api_key, name, description, goal)
except AgentError as e:
print(f"Agent error: {e}")-
Always Catch Specific Exceptions:
try: # SDK operation except AuthenticationError: # Handle authentication errors except ValidationError: # Handle validation errors except APIError: # Handle other API errors except GameSDKError: # Handle any other SDK errors
-
Provide Meaningful Error Messages:
try: result = create_agent(...) except ValidationError as e: logger.error(f"Failed to create agent: {e}") raise UserFriendlyError("Please check the agent configuration")
-
Log Errors Appropriately:
import logging logger = logging.getLogger(__name__) try: result = api_call() except APIError as e: logger.error(f"API call failed: {e.status_code} - {e.response}") # Handle error
-
Use Error Context:
try: token = get_access_token(api_key) except AuthenticationError as e: print(f"Status code: {e.status_code}") print(f"Error details: {e.response}")
Some errors can be recovered from automatically:
-
Retry on Temporary Failures:
from game_sdk.game.config import config # Configure retry settings config.set("max_retries", 3) config.set("retry_delay", 1)
-
Handle Rate Limiting:
try: result = api_call() except APIError as e: if e.status_code == 429: # Too Many Requests time.sleep(int(e.response.get("retry_after", 60))) result = api_call() # Retry
The SDK provides tools for testing error handling:
def test_error_handling(mock_response):
# Mock an API error
mock = mock_response(500, {"error": "Server Error"})
with patch('requests.post', return_value=mock):
with pytest.raises(APIError) as exc:
result = api_call()
assert exc.value.status_code == 500-
Invalid API Key:
try: token = get_access_token(api_key) except AuthenticationError: print("Please check your API key")
-
Invalid Input Data:
try: worker = create_worker(invalid_data) except ValidationError as e: print(f"Invalid worker configuration: {e}")
-
Network Issues:
try: result = api_call() except APIError as e: if "timed out" in str(e): print("Network connection is slow or unavailable")