Skip to content

Commit b20f800

Browse files
author
Constantine Peresypkin
committed
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: psf/requests#3179 Signed-off-by: Constantine Peresypkin <[email protected]>
1 parent 94e3d3d commit b20f800

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

docker/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def create_api_error_from_http_exception(e):
1818
try:
1919
explanation = response.json()['message']
2020
except ValueError:
21-
explanation = response.content.strip()
21+
explanation = (response.content or '').strip()
2222
cls = APIError
2323
if response.status_code == 404:
2424
if explanation and ('No such image' in str(explanation) or

tests/unit/errors_test.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import requests
44

55
from docker.errors import (APIError, ContainerError, DockerException,
6-
create_unexpected_kwargs_error)
6+
create_unexpected_kwargs_error,
7+
create_api_error_from_http_exception)
78
from .fake_api import FAKE_CONTAINER_ID, FAKE_IMAGE_ID
89
from .fake_api_client import make_fake_client
910

@@ -78,6 +79,19 @@ def test_is_client_error_400(self):
7879
err = APIError('', response=resp)
7980
assert err.is_client_error() is True
8081

82+
def test_create_error_from_exception(self):
83+
resp = requests.Response()
84+
resp.status_code = 500
85+
err = APIError('')
86+
try:
87+
resp.raise_for_status()
88+
except requests.exceptions.HTTPError as e:
89+
try:
90+
create_api_error_from_http_exception(e)
91+
except APIError as e:
92+
err = e
93+
assert err.is_server_error() is True
94+
8195

8296
class ContainerErrorTest(unittest.TestCase):
8397
def test_container_without_stderr(self):

0 commit comments

Comments
 (0)