Skip to content

Commit 3ec799f

Browse files
Merge pull request #481 from mptap/document-error-handling
Documented the new error handling functionality from python-http-client
2 parents 4f0d6f2 + f15bb99 commit 3ec799f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

TROUBLESHOOTING.md

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ If you can't find a solution below, please open an [issue](https://github.com/se
1212
* [Using the Package Manager](#package-manager)
1313
* [Version Convention](#versions)
1414
* [Viewing the Request Body](#request-body)
15+
* [Error Handling](#error-handling)
1516

1617
<a name="environment"></a>
1718
## Environment Variables and Your SendGrid API Key
@@ -106,3 +107,8 @@ You can do this right before you call `response = sg.client.mail.send.post(reque
106107
```python
107108
print mail.get()
108109
```
110+
111+
<a name="error-handling"></a>
112+
# Error Handling
113+
114+
Please review [our use_cases](https://github.com/sendgrid/sendgrid-python/USE_CASES.md) for examples of error handling.

USE_CASES.md

+28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This documentation provides examples for specific use cases. Please [open an iss
77
* [How to Setup a Domain Whitelabel](#domain_whitelabel)
88
* [How to View Email Statistics](#email_stats)
99
* [Asynchronous Mail Send](#asynchronous-mail-send)
10+
* [Error Handling](#error-handling)
1011

1112
<a name="transactional-templates"></a>
1213
# Transactional Templates
@@ -272,3 +273,30 @@ if __name__ == "__main__":
272273
loop.run_until_complete(task)
273274
```
274275

276+
<a name="error-handling"></a>
277+
# Error Handling
278+
[Custom exceptions](https://github.com/sendgrid/python-http-client/blob/master/python_http_client/exceptions.py) for `python_http_client` are now supported, which can be imported by consuming libraries.
279+
280+
Please see [here](https://github.com/sendgrid/python-http-client/blob/master/python_http_client/exceptions.py) for a list of supported exceptions.
281+
282+
```python
283+
import sendgrid
284+
import os
285+
from sendgrid.helpers.mail import *
286+
from python_http_client import exceptions
287+
288+
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
289+
from_email = Email("[email protected]")
290+
to_email = Email("[email protected]")
291+
subject = "Sending with SendGrid is Fun"
292+
content = Content("text/plain", "and easy to do anywhere, even with Python")
293+
mail = Mail(from_email, subject, to_email, content)
294+
try:
295+
response = sg.client.mail.send.post(request_body=mail.get())
296+
except exceptions.BadRequestsError as e:
297+
print(e.body)
298+
exit()
299+
print(response.status_code)
300+
print(response.body)
301+
print(response.headers)
302+
```

0 commit comments

Comments
 (0)