Skip to content

Commit

Permalink
Merge pull request #87 from campaignmonitor/Issue-#70-Improve-Error-O…
Browse files Browse the repository at this point in the history
…utput

Issue #70 improve error output
  • Loading branch information
AlexisT-CM authored Jan 22, 2025
2 parents 39fcf75 + a78ecbe commit 777ee84
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
6 changes: 4 additions & 2 deletions lib/createsend/createsend.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ def handle_response(self, status, data):
elif status == 404:
raise NotFound()
elif status in range(400, 500):
raise ClientError()
json_data = json_to_py(data)
raise ClientError(f"Code: {json_data.Code} {json_data.Message}")
elif status in range(500, 600):
raise ServerError()
json_data = json_to_py(data)
raise ServerError(f"Code: {json_data.Code} {json_data.Message}")
return data

def _get(self, path, params={}, username=None, password=None):
Expand Down
5 changes: 3 additions & 2 deletions lib/createsend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ def connect(self):
raise


def json_to_py(j):
o = json.loads(j.decode('utf-8'))
def json_to_py(o):
if isinstance(o,bytes):
o = json.loads(o.decode('utf-8'))
if isinstance(o, dict):
return dict_to_object(o)
else:
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/sample_client_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Code": 418,
"Message": " I'm a teapot"
}
4 changes: 4 additions & 0 deletions test/fixtures/sample_server_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Code": 500,
"Message": "Internal Server Error"
}
16 changes: 8 additions & 8 deletions test/test_createsend.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ def test_not_found_on_get(self):
self.assertRaises(self.error_responses[404], self.cs.countries)

def test_other_client_error_on_get(self):
self.cs.stub_request('countries.json', None, status=418)
self.cs.stub_request('countries.json', 'sample_client_error.json', status=418)
self.assertRaises(self.error_responses[418], self.cs.countries)

def test_server_error_on_get(self):
self.cs.stub_request('countries.json', None, status=500)
self.cs.stub_request('countries.json', 'sample_server_error.json', status=500)
self.assertRaises(self.error_responses[500], self.cs.countries)

def test_bad_request_on_post(self):
Expand All @@ -176,13 +176,13 @@ def test_not_found_on_post(self):

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

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

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

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

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

def test_server_error_on_delete(self):
template = Template(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh")
template.stub_request(
'templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=500)
'templates/uhiuhiuhiuhiuhiuhiuh.json', 'sample_server_error.json', status=500)
self.assertRaises(self.error_responses[500], template.delete)


Expand Down

0 comments on commit 777ee84

Please sign in to comment.