Skip to content

Latest commit

 

History

History
29 lines (25 loc) · 1.24 KB

error_handling.md

File metadata and controls

29 lines (25 loc) · 1.24 KB

Error Handling

Custom exceptions for python_http_client are now supported.

Please see here for a list of supported exceptions.

There are also email specific exceptions located here

import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (From, To, Subject, PlainTextContent, HtmlContent, Mail)
from python_http_client import exceptions

sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
from_email = From("[email protected]")
to_email = To("[email protected]")
subject = Subject("Sending with Twilio SendGrid is Fun")
plain_text_content = PlainTextContent("and easy to do anywhere, even with Python")
html_content = HtmlContent("<strong>and easy to do anywhere, even with Python</strong>")
message = Mail(from_email, to_email, subject, plain_text_content, html_content)
try:
    response = sendgrid_client.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except exceptions.BadRequestsError as e:
    print(e.body)
    exit()