Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ If you can't find a solution below, please open an [issue](https://github.com/se
* [Using the Package Manager](#package-manager)
* [Version Convention](#versions)
* [Viewing the Request Body](#request-body)
* [Error Handling](#error-handling)

<a name="environment"></a>
## Environment Variables and Your SendGrid API Key
Expand Down Expand Up @@ -106,3 +107,8 @@ You can do this right before you call `response = sg.client.mail.send.post(reque
```python
print mail.get()
```

<a name="error-handling"></a>
# Error Handling

Please review [our use_cases](https://github.com/sendgrid/sendgrid-python/USE_CASES.md) for examples of error handling.
28 changes: 28 additions & 0 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This documentation provides examples for specific use cases. Please [open an iss
* [How to Setup a Domain Whitelabel](#domain_whitelabel)
* [How to View Email Statistics](#email_stats)
* [Asynchronous Mail Send](#asynchronous-mail-send)
* [Error Handling](#error-handling)

<a name="transactional-templates"></a>
# Transactional Templates
Expand Down Expand Up @@ -272,3 +273,30 @@ if __name__ == "__main__":
loop.run_until_complete(task)
```

<a name="error-handling"></a>
# Error Handling
[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.

Please see [here](https://github.com/sendgrid/python-http-client/blob/master/python_http_client/exceptions.py) for a list of supported exceptions.

```python
import sendgrid
import os
from sendgrid.helpers.mail import *
from python_http_client import exceptions

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("dx@sendgrid.com")
to_email = Email("elmer.thomas@sendgrid.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
try:
response = sg.client.mail.send.post(request_body=mail.get())
except exceptions.BadRequestsError as e:
print(e.body)
exit()
print(response.status_code)
print(response.body)
print(response.headers)
```