diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md index 9988524ca..a68431ae5 100644 --- a/TROUBLESHOOTING.md +++ b/TROUBLESHOOTING.md @@ -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) ## 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 ```python print mail.get() ``` + + +# Error Handling + +Please review [our use_cases](https://github.com/sendgrid/sendgrid-python/USE_CASES.md) for examples of error handling. diff --git a/USE_CASES.md b/USE_CASES.md index 810d345ca..11dd594b0 100644 --- a/USE_CASES.md +++ b/USE_CASES.md @@ -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) # Transactional Templates @@ -272,3 +273,30 @@ if __name__ == "__main__": loop.run_until_complete(task) ``` + +# 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) +```