Kaydet (Commit) b20f800d authored tarafından Constantine Peresypkin's avatar Constantine Peresypkin

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/3179Signed-off-by: 's avatarConstantine Peresypkin <pconstantine@gmail.com>
üst 94e3d3dc
...@@ -18,7 +18,7 @@ def create_api_error_from_http_exception(e): ...@@ -18,7 +18,7 @@ def create_api_error_from_http_exception(e):
try: try:
explanation = response.json()['message'] explanation = response.json()['message']
except ValueError: except ValueError:
explanation = response.content.strip() explanation = (response.content or '').strip()
cls = APIError cls = APIError
if response.status_code == 404: if response.status_code == 404:
if explanation and ('No such image' in str(explanation) or if explanation and ('No such image' in str(explanation) or
......
...@@ -3,7 +3,8 @@ import unittest ...@@ -3,7 +3,8 @@ import unittest
import requests import requests
from docker.errors import (APIError, ContainerError, DockerException, 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 import FAKE_CONTAINER_ID, FAKE_IMAGE_ID
from .fake_api_client import make_fake_client from .fake_api_client import make_fake_client
...@@ -78,6 +79,19 @@ class APIErrorTest(unittest.TestCase): ...@@ -78,6 +79,19 @@ class APIErrorTest(unittest.TestCase):
err = APIError('', response=resp) err = APIError('', response=resp)
assert err.is_client_error() is True 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): class ContainerErrorTest(unittest.TestCase):
def test_container_without_stderr(self): def test_container_without_stderr(self):
......
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