import os
from unstructured_client import UnstructuredClient
from unstructured_client.models.operations import ListWorkflowsRequest
from unstructured_client.models.errors import (
UnstructuredClientError,
HTTPValidationError
)
from unstructured_client.models.errors.servererror import ServerError
from unstructured_client.models.errors.responsevalidationerror import ResponseValidationError
import httpx
try:
client = UnstructuredClient(
# For example, intentionally leave out the API key to intentionally throw an error.
# api_key_auth=os.getenv("UNSTRUCTURED_API_KEY")
)
response = client.workflows.list_workflows(
request=ListWorkflowsRequest()
)
print(f"Found {len(response.response_list_workflows)} workflows.")
except HTTPValidationError as e:
print("Validation error (HTTP 422):", e)
except ServerError as e:
print("Server error (HTTP 5XX):", e)
except ResponseValidationError as e:
print("Response validation/type mismatch:", e)
except UnstructuredClientError as e:
# This catches any other UnstructuredClientError not already caught above.
# This and all of the other error classes in this example expose the following members:
print("Other Unstructured client error:")
print(f"Message: {e.message}")
print(f"Status code: {e.status_code}")
print(f"Body: {e.body}")
print(f"Raw response: {e.raw_response}")
print(f"Headers:")
for header in e.headers.raw:
key = header[0].decode('utf-8')
value = header[1].decode('utf-8')
print(f" {key}: {value}")
except httpx.ConnectError as e:
print("HTTP connection error:", e)
except httpx.TimeoutException as e:
print("HTTP timeout error:", e)
except httpx.RequestError as e:
# This catches catch-all network errors from HTTP not already caught above.
print("Other HTTPX request error:", e)
except Exception as e:
# Optional: this catches any other unforeseen errors.
print("Unexpected error:", e)