Skip to content

Commit 5588839

Browse files
committed
Add error handling section to the docs
1 parent 5273f38 commit 5588839

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

examples/ConnectedAccounts.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,56 @@ connected_accounts = await client.delete_connected_account(
169169
)
170170
```
171171

172+
## Error Handling
173+
174+
All SDK errors inherit from `Auth0Error`. For most cases, catch `Auth0Error` to handle all errors uniformly. Only catch specific error types when you need to take different actions based on the error.
175+
176+
### Basic Error Handling (Recommended)
177+
178+
```python
179+
from auth0_server_python.error import Auth0Error
180+
181+
try:
182+
connect_url = await client.start_connect_account(
183+
ConnectAccountOptions(connection="google-oauth2", redirect_uri="https://example.com/callback"),
184+
store_options={"request": request, "response": response}
185+
)
186+
except Auth0Error as e:
187+
print(f"Error: {str(e)}")
188+
return {"error": "Failed to connect account"}
189+
```
190+
191+
### Advanced Error Handling (When Specific Actions Required)
192+
193+
Catch specific types only when you need to handle different error conditions differently:
194+
195+
```python
196+
from auth0_server_python.error import Auth0Error, MyAccountApiError
197+
198+
try:
199+
connect_url = await client.start_connect_account(
200+
ConnectAccountOptions(connection="google-oauth2", redirect_uri="https://example.com/callback"),
201+
store_options={"request": request, "response": response}
202+
)
203+
except MyAccountApiError as e:
204+
if e.status == 401:
205+
return redirect_to_login() # Token expired
206+
elif e.status == 403:
207+
return {"error": "Missing required permission"} # Missing scope
208+
elif e.status == 400 and e.validation_errors:
209+
return {"error": "Validation failed", "details": e.validation_errors}
210+
raise # Re-raise other cases
211+
except Auth0Error as e:
212+
return {"error": str(e)}
213+
```
214+
215+
### Common Error Types
216+
217+
- **`Auth0Error`** (base): Catch this for general error handling
218+
- **`MyAccountApiError`**: My Account API errors with `status`, `detail`, and optional `validation_errors`
219+
- **`InvalidArgumentError`**: Invalid parameter value
220+
- **`MissingRequiredArgumentError`**: Required parameter not provided
221+
172222
## A note about scopes
173223

174224
If multiple pieces of Connected Account functionality are intended to be used, it is recommended that you set the default `scope` for the My Account audience when creating you `ServerClient`. This will avoid multiple token requests as without it a new token will be requested for each scope used. This can be done by configuring the `scope` dictionary in the `authorization_params` when configuring the SDK. Each value in the dictionary corresponds to an `audience` and sets the `default` requested scopes for that audience.

0 commit comments

Comments
 (0)