Unverified Kaydet (Commit) dcd01f0f authored tarafından Ben Firshman's avatar Ben Firshman

Parse JSON API errors

Signed-off-by: 's avatarBen Firshman <ben@firshman.co.uk>
üst e6768409
...@@ -11,7 +11,10 @@ class APIError(requests.exceptions.HTTPError): ...@@ -11,7 +11,10 @@ class APIError(requests.exceptions.HTTPError):
self.explanation = explanation self.explanation = explanation
if self.explanation is None and response.content: if self.explanation is None and response.content:
self.explanation = response.content.strip() try:
self.explanation = response.json()['message']
except ValueError:
self.explanation = response.content.strip()
def __str__(self): def __str__(self):
message = super(APIError, self).__str__() message = super(APIError, self).__str__()
......
...@@ -118,7 +118,7 @@ class CreateContainerTest(helpers.BaseTestCase): ...@@ -118,7 +118,7 @@ class CreateContainerTest(helpers.BaseTestCase):
self.client.wait(id) self.client.wait(id)
with self.assertRaises(docker.errors.APIError) as exc: with self.assertRaises(docker.errors.APIError) as exc:
self.client.remove_container(id) self.client.remove_container(id)
err = exc.exception.response.text err = exc.exception.explanation
self.assertIn( self.assertIn(
'You cannot remove a running container', err 'You cannot remove a running container', err
) )
...@@ -289,7 +289,7 @@ class CreateContainerTest(helpers.BaseTestCase): ...@@ -289,7 +289,7 @@ class CreateContainerTest(helpers.BaseTestCase):
) )
self.client.start(container) self.client.start(container)
assert six.b(expected_msg) in excinfo.value.explanation assert excinfo.value.explanation == expected_msg
def test_valid_no_log_driver_specified(self): def test_valid_no_log_driver_specified(self):
log_config = docker.utils.LogConfig( log_config = docker.utils.LogConfig(
......
from docker.errors import APIError
from .. import helpers
class ErrorsTest(helpers.BaseTestCase):
def test_api_error_parses_json(self):
container = self.client.create_container(
helpers.BUSYBOX,
['sleep', '10']
)
self.client.start(container['Id'])
with self.assertRaises(APIError) as cm:
self.client.remove_container(container['Id'])
explanation = cm.exception.explanation
assert 'You cannot remove a running container' in explanation
assert '{"message":' not in explanation
self.client.remove_container(container['Id'], force=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment