|
| 1 | +--- |
| 2 | +title: Errors |
| 3 | +--- |
| 4 | + |
| 5 | +For the [Unstructured Python SDK](/api-reference/workflow/overview#unstructured-python-sdk), |
| 6 | +the [Unstructured Workflow Endpoint](/api-reference/workflow/overview) returns errors primarily through |
| 7 | +the `UnstructuredClientError` class (the base class for all errors raised by the Unstructured Python SDK) and |
| 8 | +the `HTTPValidationError` class (inherited from `UnstructuredClientError`). Less common errors are returned through the following classes: |
| 9 | + |
| 10 | +- `httpx.RequestError`, the base class for request errors. |
| 11 | +- `httpx.ConnectError`, for HTTP connection request errors. |
| 12 | +- `httpx.TimeoutException`, for HTTP request timeout errors. |
| 13 | +- `ServerError` (inherited from `UnstructuredClientError`), for server-side errors. |
| 14 | +- `ResponseValidationError` (inherited from `UnstructuredClientError`), for type mismatches between the response data and the expected Pydantic model. |
| 15 | + |
| 16 | +Each of the preceding classes has the following members: |
| 17 | + |
| 18 | +| Member | Type | Description | |
| 19 | +|--------|------|-------------| |
| 20 | +| `message` | `str` | The eror message. | |
| 21 | +| `status_code` | `int` | The HTTP response status code, for example `401`. | |
| 22 | +| `headers` | `httpx.Headers` | A collection of HTTP response headers. | |
| 23 | +| `body` | `str` | The HTTP body. This can be an empty string if no body is returned. | |
| 24 | +| `raw_response` | `httpx.Response` | The raw HTTP response. |
| 25 | + |
| 26 | +The following example shows how to handle the preceding errors. In this example, |
| 27 | +a required Unstructured API key is intentionally commented out of the code, so that a |
| 28 | +`401 Unauthorized` error is intentionally thrown. |
| 29 | + |
| 30 | +```python |
| 31 | +import os |
| 32 | + |
| 33 | +from unstructured_client import UnstructuredClient |
| 34 | +from unstructured_client.models.operations import ListWorkflowsRequest |
| 35 | +from unstructured_client.models.errors import ( |
| 36 | + UnstructuredClientError, |
| 37 | + HTTPValidationError |
| 38 | +) |
| 39 | +from unstructured_client.models.errors.servererror import ServerError |
| 40 | +from unstructured_client.models.errors.responsevalidationerror import ResponseValidationError |
| 41 | +import httpx |
| 42 | + |
| 43 | +try: |
| 44 | + client = UnstructuredClient( |
| 45 | + # For example, intentionally leave out the API key to intentionally throw an error. |
| 46 | + # api_key_auth=os.getenv("UNSTRUCTURED_API_KEY") |
| 47 | + ) |
| 48 | + |
| 49 | + response = client.workflows.list_workflows( |
| 50 | + request=ListWorkflowsRequest() |
| 51 | + ) |
| 52 | + |
| 53 | + print(f"Found {len(response.response_list_workflows)} workflows.") |
| 54 | + |
| 55 | +except HTTPValidationError as e: |
| 56 | + print("Validation error (HTTP 422):", e) |
| 57 | +except ServerError as e: |
| 58 | + print("Server error (HTTP 5XX):", e) |
| 59 | +except ResponseValidationError as e: |
| 60 | + print("Response validation/type mismatch:", e) |
| 61 | +except UnstructuredClientError as e: |
| 62 | + # This catches any other UnstructuredClientError not already caught above. |
| 63 | + # This and all of the other error classes in this example expose the following members: |
| 64 | + print("Other Unstructured client error:") |
| 65 | + print(f"Message: {e.message}") |
| 66 | + print(f"Status code: {e.status_code}") |
| 67 | + print(f"Body: {e.body}") |
| 68 | + print(f"Raw response: {e.raw_response}") |
| 69 | + print(f"Headers:") |
| 70 | + |
| 71 | + for header in e.headers.raw: |
| 72 | + key = header[0].decode('utf-8') |
| 73 | + value = header[1].decode('utf-8') |
| 74 | + print(f" {key}: {value}") |
| 75 | + |
| 76 | +except httpx.ConnectError as e: |
| 77 | + print("HTTP connection error:", e) |
| 78 | +except httpx.TimeoutException as e: |
| 79 | + print("HTTP timeout error:", e) |
| 80 | +except httpx.RequestError as e: |
| 81 | + # This catches catch-all network errors from HTTP not already caught above. |
| 82 | + print("Other HTTPX request error:", e) |
| 83 | +except Exception as e: |
| 84 | + # Optional: this catches any other unforeseen errors. |
| 85 | + print("Unexpected error:", e) |
| 86 | +``` |
| 87 | + |
| 88 | +The results of running the preceding code are similar to the following: |
| 89 | + |
| 90 | +```text |
| 91 | +Message: API error occurred: Status 401. Body: {"detail":"API key is missing, please provide an API key in the header."} |
| 92 | +Status code: 401 |
| 93 | +Body: {"detail":"API key is missing, please provide an API key in the header."} |
| 94 | +Raw response: <Response [401 Unauthorized]> |
| 95 | +Headers: |
| 96 | + date: <date-and-time-of-the-error> |
| 97 | + server: <server-identifier> |
| 98 | + content-length: 73 |
| 99 | + content-type: application/json |
| 100 | +``` |
0 commit comments