Skip to content

Commit 2527735

Browse files
author
Rich Leland
committed
Add example for exception handling
1 parent c874555 commit 2527735

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
This script demonstrates how to use the SparkPostAPIException class. This
3+
particular example will yield output similar to the following:
4+
5+
$ python send_transmission_exception.py
6+
400
7+
{u'errors': [{u'message': u'Invalid domain', u'code': u'7001', u'description':
8+
u'Unconfigured Sending Domain <some-domain-you-havent-configured.com> '}]}
9+
['Invalid domain Code: 7001 Description: Unconfigured Sending Domain
10+
<some-domain-you-havent-configured.com> \n']
11+
"""
12+
13+
from sparkpost import SparkPost
14+
from sparkpost.exceptions import SparkPostAPIException
15+
16+
sp = SparkPost()
17+
18+
try:
19+
response = sp.transmissions.send(
20+
recipients=['john.doe@example.com'],
21+
text='Hello there',
22+
from_email='Testing <test@some-domain-you-havent-configured.com>',
23+
subject='Testing python-sparkpost exceptions'
24+
)
25+
except SparkPostAPIException as err:
26+
# http response status code
27+
print(err.status)
28+
# python requests library response object
29+
# http://docs.python-requests.org/en/master/api/#requests.Response
30+
print(err.response.json())
31+
# list of formatted errors
32+
print(err.errors)

0 commit comments

Comments
 (0)