Skip to content

Commit 777ee84

Browse files
authored
Merge pull request #87 from campaignmonitor/Issue-#70-Improve-Error-Output
Issue #70 improve error output
2 parents 39fcf75 + a78ecbe commit 777ee84

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

lib/createsend/createsend.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,11 @@ def handle_response(self, status, data):
229229
elif status == 404:
230230
raise NotFound()
231231
elif status in range(400, 500):
232-
raise ClientError()
232+
json_data = json_to_py(data)
233+
raise ClientError(f"Code: {json_data.Code} {json_data.Message}")
233234
elif status in range(500, 600):
234-
raise ServerError()
235+
json_data = json_to_py(data)
236+
raise ServerError(f"Code: {json_data.Code} {json_data.Message}")
235237
return data
236238

237239
def _get(self, path, params={}, username=None, password=None):

lib/createsend/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ def connect(self):
107107
raise
108108

109109

110-
def json_to_py(j):
111-
o = json.loads(j.decode('utf-8'))
110+
def json_to_py(o):
111+
if isinstance(o,bytes):
112+
o = json.loads(o.decode('utf-8'))
112113
if isinstance(o, dict):
113114
return dict_to_object(o)
114115
else:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"Code": 418,
3+
"Message": " I'm a teapot"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"Code": 500,
3+
"Message": "Internal Server Error"
4+
}

test/test_createsend.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ def test_not_found_on_get(self):
147147
self.assertRaises(self.error_responses[404], self.cs.countries)
148148

149149
def test_other_client_error_on_get(self):
150-
self.cs.stub_request('countries.json', None, status=418)
150+
self.cs.stub_request('countries.json', 'sample_client_error.json', status=418)
151151
self.assertRaises(self.error_responses[418], self.cs.countries)
152152

153153
def test_server_error_on_get(self):
154-
self.cs.stub_request('countries.json', None, status=500)
154+
self.cs.stub_request('countries.json', 'sample_server_error.json', status=500)
155155
self.assertRaises(self.error_responses[500], self.cs.countries)
156156

157157
def test_bad_request_on_post(self):
@@ -176,13 +176,13 @@ def test_not_found_on_post(self):
176176

177177
def test_other_client_error_on_post(self):
178178
client = Client(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh")
179-
client.stub_request('clients.json', None, status=418)
179+
client.stub_request('clients.json', 'sample_client_error.json', status=418)
180180
self.assertRaises(self.error_responses[418], client.create, "Client Company Name",
181181
"(GMT+10:00) Canberra, Melbourne, Sydney", "Australia")
182182

183183
def test_server_error_on_post(self):
184184
client = Client(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh")
185-
client.stub_request('clients.json', None, status=500)
185+
client.stub_request('clients.json', 'sample_server_error.json', status=500)
186186
self.assertRaises(self.error_responses[500], client.create, "Client Company Name",
187187
"(GMT+10:00) Canberra, Melbourne, Sydney", "Australia")
188188

@@ -210,14 +210,14 @@ def test_not_found_on_put(self):
210210
def test_other_client_error_on_put(self):
211211
template = Template(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh")
212212
template.stub_request(
213-
'templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=418)
213+
'templates/uhiuhiuhiuhiuhiuhiuh.json', 'sample_client_error.json', status=418)
214214
self.assertRaises(self.error_responses[418], template.update, "Template One Updated", "http://templates.org/index.html",
215215
"http://templates.org/files.zip")
216216

217217
def test_server_error_on_put(self):
218218
template = Template(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh")
219219
template.stub_request(
220-
'templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=500)
220+
'templates/uhiuhiuhiuhiuhiuhiuh.json', 'sample_server_error.json', status=500)
221221
self.assertRaises(self.error_responses[500], template.update, "Template One Updated", "http://templates.org/index.html",
222222
"http://templates.org/files.zip")
223223

@@ -242,13 +242,13 @@ def test_not_found_on_delete(self):
242242
def test_other_client_error_on_delete(self):
243243
template = Template(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh")
244244
template.stub_request(
245-
'templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=418)
245+
'templates/uhiuhiuhiuhiuhiuhiuh.json', 'sample_client_error.json', status=418)
246246
self.assertRaises(self.error_responses[418], template.delete)
247247

248248
def test_server_error_on_delete(self):
249249
template = Template(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh")
250250
template.stub_request(
251-
'templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=500)
251+
'templates/uhiuhiuhiuhiuhiuhiuh.json', 'sample_server_error.json', status=500)
252252
self.assertRaises(self.error_responses[500], template.delete)
253253

254254

0 commit comments

Comments
 (0)