Skip to content

Commit

Permalink
Add better handling of 500 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Feb 14, 2013
1 parent 7379084 commit fd6e31b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions @test_19350_tmp_dir/inspect_fodder3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class X:
pass # No EOL
14 changes: 12 additions & 2 deletions kazoo/request_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def execute(self, base_url, method=None, data=None, token=None, files=None, **kw
kwargs["files"] = files
raw_response = req_func(full_url, headers=headers, **kwargs)
if raw_response.status_code == 500:
request_id = raw_response.headers["X-Request-Id"]
raise exceptions.KazooApiError("Internal Server Error, Request ID was {0}".format(request_id))
self._handle_500_error(raw_response)
response = raw_response.json
if response["status"] == "error":
logger.debug("There was an error, full error text is: {0}".format(
Expand All @@ -91,6 +90,17 @@ def _handle_error(self, error_data):
error_data["request_id"],
))

def _handle_500_error(self, raw_response):
request_id = raw_response.headers["X-Request-Id"]
if raw_response.json:
message = raw_response.json["data"]
else:
message = "There was no error message"
raise exceptions.KazooApiError("Internal Server Error, "
"Request ID was {0}"
" message was {1}".format(
request_id, message))


class UsernamePasswordAuthRequest(KazooRequest):

Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/bad_billing_status_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"auth_token": "6dc29b060e9aa03f58501cb57068a27",
"request_id": "420d47706e945720a951d2b1d0793cb1",
"status": "error",
"message": "no_payment_token",
"error": "500",
"data": "Unable to continue due to billing account 646637e16ee7de87ae617e6581a20c54 status"

}
2 changes: 1 addition & 1 deletion tests/fixtures/good_auth_response.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"status": "success",
"auth_token": "9f19181bf42bc66ab6d881ae4e1591d4",
"auth_token": "9f19081bf420c66a06d8810e4e1591d4",
"data": {
"account_id": "c141ff162bdc844e5669fa8536f6d9ef",
"owner_id": "0ccfa9a69be1fd06a2d50c95e2f42000"
Expand Down
18 changes: 17 additions & 1 deletion tests/test_request_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,33 @@ def test_kazoo_api_error_raised_on_error_response(self):
self.error_response["message"])
self.assertTrue(expected_errors in cm.exception.message)

def test_internal_server_error(self):
def test_internal_server_error_unparseable(self):
req_obj = KazooRequest("/somepath", auth_required=False)
with mock.patch('requests.get') as mock_get:
mock_response = mock.Mock()
mock_response.status_code = 500
mock_response.headers = {"X-Request-Id": "sdfaskldfjaosdf"}
mock_response.json = None
mock_get.return_value = mock_response
with self.assertRaises(exceptions.KazooApiError) as cm:
req_obj.execute("http://testserver")
self.assertTrue("Request ID" in cm.exception.message)

def test_internal_server_error_has_error_message_if_parseable(self):
req_obj = KazooRequest("/somepath", auth_required=False)
with mock.patch('requests.get') as mock_get:
mock_response = mock.Mock()
mock_response.status_code = 500
mock_response.headers = {"X-Request-Id": "sdfaskldfjaosdf"}
mock_response.json = utils.load_fixture_as_dict(
"bad_billing_status_response.json")
mock_get.return_value = mock_response
with self.assertRaises(exceptions.KazooApiError) as cm:
req_obj.execute("http://testserver")
ex = cm.exception
self.assertTrue("Unable to continue due to billing" in ex.message)


def test_invalid_data_displays_invalid_field_data(self):
req_obj = KazooRequest("/somepath", auth_required=False)
with mock.patch('requests.get') as mock_get:
Expand Down

0 comments on commit fd6e31b

Please sign in to comment.