|
1 | | -# Handling HTTP errors |
| 1 | +# Handling Server Errors |
2 | 2 |
|
3 | | -If there is an HTTP error during an API call, it will raise an `ApiException`. You can access different metadata from that exception: |
| 3 | +When building applications with the Groundlight SDK, you may encounter server errors during API calls. This page covers how to handle such errors and build robust code that can gracefully handle exceptions. |
4 | 4 |
|
5 | | -```python |
| 5 | +## Handling ApiException |
| 6 | + |
| 7 | +If there is an HTTP error during an API call, the SDK will raise an `ApiException`. You can access different metadata from that exception: |
| 8 | + |
| 9 | +```python notest |
| 10 | +import traceback |
6 | 11 | from groundlight import ApiException, Groundlight |
7 | 12 |
|
8 | 13 | gl = Groundlight() |
9 | 14 | try: |
10 | | - detectors = gl.list_detectors() |
| 15 | + d = gl.get_or_create_detector( |
| 16 | + "Road Checker", |
| 17 | + "Is the site access road blocked?") |
| 18 | + iq = gl.submit_image_query(d, get_image(), wait=60) |
11 | 19 | except ApiException as e: |
12 | | - # Many fields available to describe the error |
13 | | - print(e) |
14 | | - print(e.args) |
15 | | - print(e.body) |
16 | | - print(e.headers) |
17 | | - print(e.reason) |
18 | | - print(e.status) |
| 20 | + # Print a traceback for debugging |
| 21 | + traceback.print_exc() |
| 22 | + |
| 23 | + # e.reason contains a textual description of the error |
| 24 | + print(f"Error reason: {e.reason}") |
| 25 | + |
| 26 | + # e.status contains the HTTP status code |
| 27 | + print(f"HTTP status code: {e.status}") |
| 28 | + |
| 29 | + # Common HTTP status codes: |
| 30 | + # 400 Bad Request: The request was invalid or malformed |
| 31 | + # 401 Unauthorized: Your GROUNDLIGHT_API_TOKEN is missing or invalid |
| 32 | + # 403 Forbidden: The request is not allowed due to insufficient permissions |
| 33 | + # 404 Not Found: The requested resource was not found |
| 34 | + # 429 Too Many Requests: The rate limit for the API has been exceeded |
| 35 | + # 500 Internal Server Error: An error occurred on the server side |
19 | 36 | ``` |
| 37 | + |
| 38 | +## Best Practices for Handling Exceptions |
| 39 | + |
| 40 | +When working with the Groundlight SDK, follow these best practices to handle exceptions and build robust code: |
| 41 | + |
| 42 | +### Catch Specific Exceptions |
| 43 | + |
| 44 | +Catch only the specific exceptions that you expect to be raised, such as `ApiException`. Avoid catching broad exceptions like `Exception`, as it may make debugging difficult and obscure other unrelated issues. |
| 45 | + |
| 46 | +### Use Custom Exception Classes |
| 47 | + |
| 48 | +Consider creating custom exception classes for your application-specific errors. This can help you differentiate between errors originating from the Groundlight SDK and those from your application. |
| 49 | + |
| 50 | +### Log Exceptions |
| 51 | + |
| 52 | +Log exceptions with appropriate log levels (e.g., error, warning, etc.) and include relevant context information. This will help you debug issues more effectively and monitor the health of your application. |
| 53 | + |
| 54 | +### Implement Retry Logic |
| 55 | + |
| 56 | +When handling exceptions, implement retry logic with exponential backoff for transient errors, such as network issues or rate-limiting. This can help your application recover from temporary issues without manual intervention. |
| 57 | + |
| 58 | +### Handle Exceptions Gracefully |
| 59 | + |
| 60 | +In addition to logging exceptions, handle them gracefully to ensure that your application remains functional despite errors. This might include displaying an error message to users or falling back to a default behavior. |
| 61 | + |
| 62 | +### Test Your Error Handling |
| 63 | + |
| 64 | +Write tests to ensure that your error handling works as expected. This can help you catch issues early and ensure that your application can handle errors gracefully in production. |
| 65 | + |
| 66 | +By following these best practices, you can create robust and resilient applications that can handle server errors and other exceptions when using the Groundlight SDK. |
0 commit comments