Download api-error-interceptor/instruction.md from slfs1991slfs/tb_osworld_test: direct link, hf CLI and curl.
- Browser
- Download file 2.33 kB
-
https://huggingface.co/datasets/slfs1991slfs/tb_osworld_test/resolve/main/api-error-interceptor/instruction.md
- Command line
-
hf download hf://datasets/slfs1991slfs/tb_osworld_test/api-error-interceptor/instruction.md
-
curl -L -o instruction.md https://huggingface.co/datasets/slfs1991slfs/tb_osworld_test/resolve/main/api-error-interceptor/instruction.md
Create error_handler.py in /app with an APIErrorHandler class that wraps HTTP requests. We're getting inconsistent error handling and need a consistent way to handle API failures.
The APIErrorHandler class should have an intercept_request method that takes an HTTP method, URL, and optional parameters like timeout and headers. Use requests.request() to make the actual HTTP calls. The class needs to be importable so tests can use it.
Response Format
When requests succeed (status 2xx), return:
{'success': True, 'data': response_data}
When they fail (status 4xx or 5xx), return:
{'error': error_message}
For 401 responses specifically, include an 'action': 'logout' key:
{'error': 'Unauthorized', 'action': 'logout'}
For 500 errors, return this exact generic message (do not leak server details):
{'error': 'Server Error', 'message': 'Something went wrong. Please try again.'}
Network Error Handling
Handle network errors and timeouts gracefully. When a requests.exceptions.Timeout exception occurs, catch it and return an error response. Other network errors (ConnectionError, RequestException) should also be caught and handled.
For network errors that don't have HTTP status codes (Timeout, ConnectionError, etc.), use status_code: 0 in the log entry.
Error Logging
Persist all errors to a JSON file at /app/error_log.json. Each error log entry must be a dictionary with exactly these keys:
timestamp: ISO 8601 format with 'Z' suffix (usedatetime.utcnow().isoformat() + 'Z')status_code: HTTP status code (integer), or 0 for network errors without status codesendpoint: The URL that was requested (string)error_message: A brief description of the error (string)
Example log entry:
{
'timestamp': '2024-01-15T10:30:00.123456Z',
'status_code': 404,
'endpoint': 'http://example.com/api/users',
'error_message': 'Not found'
}
Example log entry for network error:
{
'timestamp': '2024-01-15T10:30:00.123456Z',
'status_code': 0,
'endpoint': 'http://example.com/api/timeout',
'error_message': 'Timeout'
}
The log file should be loaded on initialization and updated after each error. Provide get_errors() and clear_errors() methods so tests can check and reset the log.