|
| 1 | +""" |
| 2 | +This example demonstrates error handling when using the StackOne SDK. |
| 3 | +
|
| 4 | +Run the following command to see the output: |
| 5 | +
|
| 6 | +```bash |
| 7 | +uv run examples/error_handling.py |
| 8 | +``` |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | + |
1 | 13 | from dotenv import load_dotenv |
2 | 14 | from stackone_ai import StackOneToolSet |
| 15 | +from stackone_ai.models import StackOneAPIError |
3 | 16 | from stackone_ai.toolset import ToolsetConfigError, ToolsetLoadError |
4 | 17 |
|
5 | 18 | load_dotenv() |
6 | 19 |
|
7 | 20 |
|
8 | 21 | def error_handling() -> None: |
| 22 | + # Example 1: Configuration error - missing API key |
9 | 23 | try: |
10 | | - # Example 1: Handle missing API key |
11 | | - invalid_toolset = StackOneToolSet(api_key=None) |
| 24 | + print("\n1. Testing configuration error (missing API key)...") |
| 25 | + original_api_key = os.environ.pop("STACKONE_API_KEY", None) |
| 26 | + try: |
| 27 | + StackOneToolSet(api_key=None) |
| 28 | + raise AssertionError("Expected ToolsetConfigError") |
| 29 | + finally: |
| 30 | + if original_api_key: |
| 31 | + os.environ["STACKONE_API_KEY"] = original_api_key |
12 | 32 | except ToolsetConfigError as e: |
13 | | - print("Config Error:", e) |
14 | | - # Config Error: API key must be provided either through api_key parameter or STACKONE_API_KEY environment variable |
15 | | - |
16 | | - toolset = StackOneToolSet() |
| 33 | + print("✗ Config Error:", e) |
17 | 34 |
|
18 | | - # Example 2: Handle unknown vertical |
| 35 | + # Example 2: Invalid vertical error |
19 | 36 | try: |
20 | | - tools = toolset.get_tools(vertical="unknown_vertical") |
| 37 | + print("\n2. Testing invalid vertical...") |
| 38 | + toolset = StackOneToolSet() |
| 39 | + toolset.get_tools(vertical="invalid_vertical") |
| 40 | + raise AssertionError("Expected ToolsetLoadError") |
21 | 41 | except ToolsetLoadError as e: |
22 | | - print("Vertical Load Error:", e) |
23 | | - # Vertical Load Error: No spec file found for vertical: unknown_vertical |
| 42 | + print("✗ Load Error:", e) |
24 | 43 |
|
25 | | - # Example 3: Handle API errors with account_id |
26 | | - tools = toolset.get_tools(vertical="crm", account_id="test_id") |
27 | | - try: |
28 | | - # Try with invalid ID |
29 | | - contacts_tool = tools.get_tool("get_contact") |
30 | | - if contacts_tool: |
31 | | - result = contacts_tool.execute({"id": "invalid_id"}) |
32 | | - except Exception as e: |
33 | | - print(f"API Error: {e}") |
34 | | - # API Error: 400 Client Error: Bad Request for url: https://api.stackone.com/unified/crm/contacts/invalid_id |
35 | | - |
36 | | - # Example 4: Handle missing account ID |
37 | | - tools_no_account = toolset.get_tools(vertical="crm", account_id=None) |
| 44 | + # Example 3: API error - invalid request |
38 | 45 | try: |
39 | | - list_contacts_tool = tools_no_account.get_tool("list_contacts") |
40 | | - if list_contacts_tool: |
41 | | - result = list_contacts_tool.execute() |
42 | | - print("Result without account ID:", result) |
43 | | - except Exception as e: |
44 | | - print(f"Error when account ID is missing: {e}") |
45 | | - # Error when account ID is missing: 501 Server Error: Not Implemented for url: https://api.stackone.com/unified/crm/contacts |
| 46 | + print("\n3. Testing API error...") |
| 47 | + toolset = StackOneToolSet() |
| 48 | + tools = toolset.get_tools(vertical="crm") |
| 49 | + |
| 50 | + # Try to make an API call without required parameters |
| 51 | + list_contacts = tools.get_tool("crm_list_contacts") |
| 52 | + assert list_contacts is not None, "Expected crm_list_contacts tool to exist" |
| 53 | + |
| 54 | + list_contacts.execute() |
| 55 | + raise AssertionError("Expected StackOneAPIError") |
| 56 | + except StackOneAPIError as e: |
| 57 | + print("✗ API Error:", e) |
| 58 | + print(" Status:", e.status_code) |
| 59 | + print(" Response:", e.response_body) |
46 | 60 |
|
47 | 61 |
|
48 | 62 | if __name__ == "__main__": |
|
0 commit comments