Skip to content

Commit 6ce4b62

Browse files
authored
Added an example showing how to access provider errors (#403)
1 parent a080a34 commit 6ce4b62

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Provider Error Handling Example
2+
3+
This example demonstrates how to properly handle provider errors when working with the Nylas API. It specifically shows how to catch and process errors that occur when trying to access a non-existent calendar.
4+
5+
## Features
6+
7+
- Demonstrates proper error handling for Nylas API provider errors
8+
- Shows how to access error details including:
9+
- Error message
10+
- Error type
11+
- Provider error message
12+
- Request ID
13+
- Status code
14+
- Includes clear output and status messages
15+
16+
## Prerequisites
17+
18+
1. A Nylas account with API access
19+
2. Python 3.x installed
20+
3. Local installation of the Nylas Python SDK (this repository)
21+
22+
## Setup
23+
24+
1. Install the SDK in development mode from the repository root:
25+
```bash
26+
cd /path/to/nylas-python
27+
pip install -e .
28+
```
29+
30+
2. Set your environment variables:
31+
```bash
32+
export NYLAS_API_KEY="your_api_key"
33+
export NYLAS_GRANT_ID="your_grant_id"
34+
```
35+
36+
3. Run the example from the repository root:
37+
```bash
38+
python examples/provider_error_demo/provider_error_example.py
39+
```
40+
41+
## Example Output
42+
43+
```
44+
Demonstrating Provider Error Handling
45+
====================================
46+
47+
Attempting to fetch events from non-existent calendar: non-existent-calendar-123
48+
------------------------------------------------------------------
49+
50+
Caught NylasApiError:
51+
✗ Error Message: Calendar not found
52+
✗ Error Type: invalid_request_error
53+
✗ Provider Error: The calendar ID provided does not exist
54+
✗ Request ID: req-abc-123
55+
✗ Status Code: 404
56+
57+
Example completed!
58+
```
59+
60+
## Error Handling
61+
62+
The example demonstrates how to handle:
63+
- Missing environment variables
64+
- API authentication errors
65+
- Provider-specific errors
66+
- Non-existent resource errors
67+
68+
## Documentation
69+
70+
For more information about the Nylas Python SDK and its features, visit:
71+
- [Nylas Python SDK Documentation](https://developer.nylas.com/docs/sdks/python/)
72+
- [Nylas API Reference](https://developer.nylas.com/docs/api/)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Nylas SDK Example: Handling Provider Errors
4+
5+
This example demonstrates how to handle provider errors when working with the Nylas API,
6+
specifically when trying to access a non-existent calendar.
7+
8+
Required Environment Variables:
9+
NYLAS_API_KEY: Your Nylas API key
10+
NYLAS_GRANT_ID: Your Nylas grant ID
11+
12+
Usage:
13+
First, install the SDK in development mode:
14+
cd /path/to/nylas-python
15+
pip install -e .
16+
17+
Then set environment variables and run:
18+
export NYLAS_API_KEY="your_api_key"
19+
export NYLAS_GRANT_ID="your_grant_id"
20+
python examples/provider_error_demo/provider_error_example.py
21+
"""
22+
23+
import os
24+
import sys
25+
from typing import Optional
26+
27+
from nylas import Client
28+
from nylas.models.errors import NylasApiError
29+
30+
31+
def get_env_or_exit(var_name: str) -> str:
32+
"""Get an environment variable or exit if not found."""
33+
value = os.getenv(var_name)
34+
if not value:
35+
print(f"Error: {var_name} environment variable is required")
36+
sys.exit(1)
37+
return value
38+
39+
40+
def demonstrate_provider_error(client: Client, grant_id: str) -> None:
41+
"""Demonstrate how to handle provider errors."""
42+
# Use a non-existent calendar ID to trigger a provider error
43+
non_existent_calendar_id = "non-existent-calendar-123"
44+
45+
try:
46+
print(f"\nAttempting to fetch events from non-existent calendar: {non_existent_calendar_id}")
47+
print("------------------------------------------------------------------")
48+
49+
# Attempt to list events with the invalid calendar ID
50+
events, request_id = client.events.list(
51+
identifier=grant_id,
52+
query_params={"calendar_id": non_existent_calendar_id}
53+
)
54+
55+
# Note: We won't reach this code due to the error
56+
print("Events retrieved:", events)
57+
58+
except NylasApiError as e:
59+
print("\nCaught NylasApiError:")
60+
print(f"✗ Error Type: {e.type}")
61+
print(f"✗ Provider Error: {e.provider_error}")
62+
print(f"✗ Request ID: {e.request_id}")
63+
print(f"✗ Status Code: {e.status_code}")
64+
65+
66+
def main():
67+
"""Main function demonstrating provider error handling."""
68+
# Get required environment variables
69+
api_key = get_env_or_exit("NYLAS_API_KEY")
70+
grant_id = get_env_or_exit("NYLAS_GRANT_ID")
71+
72+
# Initialize Nylas client
73+
client = Client(
74+
api_key=api_key,
75+
)
76+
77+
print("\nDemonstrating Provider Error Handling")
78+
print("====================================")
79+
80+
# Demonstrate provider error handling
81+
demonstrate_provider_error(client, grant_id)
82+
83+
print("\nExample completed!")
84+
85+
86+
if __name__ == "__main__":
87+
main()

0 commit comments

Comments
 (0)