|
| 1 | +# Error Handling |
| 2 | + |
| 3 | +The SDK raises typed exceptions for HTTP errors and for shape-related failures. All exceptions are importable from `tango.exceptions` (and re-exported from the top-level `tango` package for the API errors). |
| 4 | + |
| 5 | +For a compact reference of each class, see [`API_REFERENCE.md` § Error Handling](API_REFERENCE.md#error-handling). This guide covers the hierarchy, recovery patterns, and the shape-error classes that don't have a dedicated section there. |
| 6 | + |
| 7 | +## Exception Hierarchy |
| 8 | + |
| 9 | +``` |
| 10 | +TangoAPIError |
| 11 | +├── TangoAuthError (401 Unauthorized) |
| 12 | +├── TangoNotFoundError (404 Not Found) |
| 13 | +├── TangoValidationError (400 Bad Request) |
| 14 | +├── TangoRateLimitError (429 Too Many Requests) |
| 15 | +└── ShapeError |
| 16 | + ├── ShapeValidationError (invalid field names) |
| 17 | + ├── ShapeParseError (invalid shape syntax) |
| 18 | + ├── TypeGenerationError (dynamic type generation failure) |
| 19 | + └── ModelInstantiationError (model creation failure) |
| 20 | +``` |
| 21 | + |
| 22 | +## API Errors |
| 23 | + |
| 24 | +### TangoAPIError (base) |
| 25 | + |
| 26 | +All API errors inherit from this class. |
| 27 | + |
| 28 | +| Attribute | Type | Description | |
| 29 | +|---|---|---| |
| 30 | +| `status_code` | `int \| None` | HTTP status code | |
| 31 | +| `response_data` | `dict` | Parsed response body (credentials redacted) | |
| 32 | +| `message` | `str` | Human-readable error message | |
| 33 | + |
| 34 | +```python |
| 35 | +from tango import TangoClient |
| 36 | +from tango.exceptions import TangoAPIError |
| 37 | + |
| 38 | +client = TangoClient() |
| 39 | + |
| 40 | +try: |
| 41 | + resp = client.list_contracts(limit=10) |
| 42 | +except TangoAPIError as e: |
| 43 | + print(f"API error {e.status_code}: {e.message}") |
| 44 | +``` |
| 45 | + |
| 46 | +### TangoAuthError (401) |
| 47 | + |
| 48 | +Raised when the API key is missing, invalid, or expired. |
| 49 | + |
| 50 | +```python |
| 51 | +from tango.exceptions import TangoAuthError |
| 52 | + |
| 53 | +try: |
| 54 | + client = TangoClient(api_key="invalid-key") |
| 55 | + client.list_contracts(limit=1) |
| 56 | +except TangoAuthError: |
| 57 | + print("Check your API key") |
| 58 | +``` |
| 59 | + |
| 60 | +### TangoNotFoundError (404) |
| 61 | + |
| 62 | +Raised when a resource doesn't exist. |
| 63 | + |
| 64 | +```python |
| 65 | +from tango.exceptions import TangoNotFoundError |
| 66 | + |
| 67 | +try: |
| 68 | + entity = client.get_entity("INVALID_UEI") |
| 69 | +except TangoNotFoundError: |
| 70 | + print("Entity not found") |
| 71 | +``` |
| 72 | + |
| 73 | +### TangoValidationError (400) |
| 74 | + |
| 75 | +Raised for invalid request parameters (bad date format, unknown filter, etc.). |
| 76 | + |
| 77 | +### TangoRateLimitError (429) |
| 78 | + |
| 79 | +Raised when you exceed rate limits. Includes retry information. |
| 80 | + |
| 81 | +| Attribute | Type | Description | |
| 82 | +|---|---|---| |
| 83 | +| `wait_in_seconds` | `int \| None` | Seconds to wait before retrying | |
| 84 | +| `detail` | `str \| None` | Human-readable rate limit message | |
| 85 | +| `limit_type` | `str \| None` | `"burst"` or `"daily"` | |
| 86 | + |
| 87 | +```python |
| 88 | +import time |
| 89 | +from tango.exceptions import TangoRateLimitError |
| 90 | + |
| 91 | +try: |
| 92 | + resp = client.list_contracts(limit=10) |
| 93 | +except TangoRateLimitError as e: |
| 94 | + if e.wait_in_seconds: |
| 95 | + print(f"Rate limited ({e.limit_type}). Retrying in {e.wait_in_seconds}s...") |
| 96 | + time.sleep(e.wait_in_seconds) |
| 97 | + resp = client.list_contracts(limit=10) |
| 98 | +``` |
| 99 | + |
| 100 | +> **Note:** The SDK does not include built-in retry or backoff. You are responsible for handling rate limit errors. See the [Rate Limits guide](https://docs.makegov.com/guides/patterns/rate-limits/) for strategies. |
| 101 | +
|
| 102 | +## Shape Errors |
| 103 | + |
| 104 | +These are raised when there's a problem with the response shaping configuration, not the API itself. See [`SHAPES.md`](SHAPES.md) for shape syntax. |
| 105 | + |
| 106 | +### ShapeValidationError |
| 107 | + |
| 108 | +Raised when a shape string references field names that don't exist on the model. |
| 109 | + |
| 110 | +```python |
| 111 | +from tango.exceptions import ShapeValidationError |
| 112 | + |
| 113 | +try: |
| 114 | + resp = client.list_contracts(shape="key,piid,nonexistent_field", limit=1) |
| 115 | +except ShapeValidationError as e: |
| 116 | + print(f"Invalid shape: {e}") |
| 117 | + print(f"Shape string: {e.shape}") |
| 118 | +``` |
| 119 | + |
| 120 | +### ShapeParseError |
| 121 | + |
| 122 | +Raised when the shape string has invalid syntax (unbalanced parentheses, etc.). |
| 123 | + |
| 124 | +| Attribute | Type | Description | |
| 125 | +|---|---|---| |
| 126 | +| `shape` | `str` | The invalid shape string | |
| 127 | +| `position` | `int \| None` | Character position where parsing failed | |
| 128 | + |
| 129 | +### TypeGenerationError |
| 130 | + |
| 131 | +Raised when the SDK fails to generate a dynamic TypedDict for a shaped response. |
| 132 | + |
| 133 | +### ModelInstantiationError |
| 134 | + |
| 135 | +Raised when the SDK fails to create a model instance from API data. |
| 136 | + |
| 137 | +| Attribute | Type | Description | |
| 138 | +|---|---|---| |
| 139 | +| `field_name` | `str \| None` | Field that caused the failure | |
| 140 | +| `expected_type` | `type \| None` | Expected Python type | |
| 141 | +| `actual_value` | `Any` | Value that couldn't be coerced | |
| 142 | + |
| 143 | +## Catching Everything |
| 144 | + |
| 145 | +To handle any SDK-raised error in one place, catch `TangoAPIError` and `ShapeError` (or just `Exception` at the outermost boundary): |
| 146 | + |
| 147 | +```python |
| 148 | +from tango.exceptions import TangoAPIError, ShapeError |
| 149 | + |
| 150 | +try: |
| 151 | + resp = client.list_contracts(shape="key,piid", limit=10) |
| 152 | +except TangoAPIError as e: |
| 153 | + # HTTP-layer problems (auth, rate limit, validation, etc.) |
| 154 | + print(f"API error {e.status_code}: {e.message}") |
| 155 | +except ShapeError as e: |
| 156 | + # Shape-string or model-construction problems |
| 157 | + print(f"Shape error: {e}") |
| 158 | +``` |
0 commit comments