summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConstantine Peresypkin <constantine.peresypkin@datarobot.com>2017-12-02 17:18:09 -0500
committerConstantine Peresypkin <constantine.peresypkin@datarobot.com>2017-12-18 17:15:48 +0200
commitb20f800db6f6521995268a5d7a4746c017fc6d9e (patch)
tree029ab002e88821a641e0eae362074778d4d7906f
parent94e3d3dcb9d75d2302c5946d83ecae1e54a3b7c9 (diff)
downloaddocker-py-b20f800db6f6521995268a5d7a4746c017fc6d9e.tar.gz
fixes create_api_error_from_http_exception()
`create_api_error_from_http_exception()` is never tested in the original code and will fail miserably when fed with empty `HTTPError` object see fixes in requests for this behaviour: https://github.com/requests/requests/pull/3179 Signed-off-by: Constantine Peresypkin <pconstantine@gmail.com>
-rw-r--r--docker/errors.py2
-rw-r--r--tests/unit/errors_test.py16
2 files changed, 16 insertions, 2 deletions
diff --git a/docker/errors.py b/docker/errors.py
index 2a2f871..50423a2 100644
--- a/docker/errors.py
+++ b/docker/errors.py
@@ -18,7 +18,7 @@ def create_api_error_from_http_exception(e):
try:
explanation = response.json()['message']
except ValueError:
- explanation = response.content.strip()
+ explanation = (response.content or '').strip()
cls = APIError
if response.status_code == 404:
if explanation and ('No such image' in str(explanation) or
diff --git a/tests/unit/errors_test.py b/tests/unit/errors_test.py
index 9678669..e27a9b1 100644
--- a/tests/unit/errors_test.py
+++ b/tests/unit/errors_test.py
@@ -3,7 +3,8 @@ import unittest
import requests
from docker.errors import (APIError, ContainerError, DockerException,
- create_unexpected_kwargs_error)
+ create_unexpected_kwargs_error,
+ create_api_error_from_http_exception)
from .fake_api import FAKE_CONTAINER_ID, FAKE_IMAGE_ID
from .fake_api_client import make_fake_client
@@ -78,6 +79,19 @@ class APIErrorTest(unittest.TestCase):
err = APIError('', response=resp)
assert err.is_client_error() is True
+ def test_create_error_from_exception(self):
+ resp = requests.Response()
+ resp.status_code = 500
+ err = APIError('')
+ try:
+ resp.raise_for_status()
+ except requests.exceptions.HTTPError as e:
+ try:
+ create_api_error_from_http_exception(e)
+ except APIError as e:
+ err = e
+ assert err.is_server_error() is True
+
class ContainerErrorTest(unittest.TestCase):
def test_container_without_stderr(self):