Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documented the new error handling functionality from python-http-client #481

Merged
merged 2 commits into from
Oct 29, 2017
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("[email protected]")
to_email = Email("[email protected]")
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)
```