Skip to content

Commit bbad864

Browse files
alex-drshin-
authored andcommitted
Fix APIError status_code property for client/server errors
requests.Response objects evaluate as falsy when the status_code attribute is in the 400-500 range. Therefore we are assured that prior to this change, APIError would show `is_server_error() == False` when generated with a 500-level response and `is_client_error() == False` when generated with a 400-level response. This is not desirable. Added some seemingly dry (not DRY) unit tests to ensure nothing silly slips back in here. Signed-off-by: alex-dr <[email protected]>
1 parent b474ea2 commit bbad864

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

docker/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __str__(self):
5959

6060
@property
6161
def status_code(self):
62-
if self.response:
62+
if self.response is not None:
6363
return self.response.status_code
6464

6565
def is_client_error(self):

tests/unit/errors_test.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import unittest
22

3+
import requests
4+
35
from docker.errors import (APIError, DockerException,
46
create_unexpected_kwargs_error)
57

@@ -11,6 +13,69 @@ def test_api_error_is_caught_by_dockerexception(self):
1113
except DockerException:
1214
pass
1315

16+
def test_status_code_200(self):
17+
"""The status_code property is present with 200 response."""
18+
resp = requests.Response()
19+
resp.status_code = 200
20+
err = APIError('', response=resp)
21+
assert err.status_code == 200
22+
23+
def test_status_code_400(self):
24+
"""The status_code property is present with 400 response."""
25+
resp = requests.Response()
26+
resp.status_code = 400
27+
err = APIError('', response=resp)
28+
assert err.status_code == 400
29+
30+
def test_status_code_500(self):
31+
"""The status_code property is present with 500 response."""
32+
resp = requests.Response()
33+
resp.status_code = 500
34+
err = APIError('', response=resp)
35+
assert err.status_code == 500
36+
37+
def test_is_server_error_200(self):
38+
"""Report not server error on 200 response."""
39+
resp = requests.Response()
40+
resp.status_code = 200
41+
err = APIError('', response=resp)
42+
assert err.is_server_error() is False
43+
44+
def test_is_server_error_300(self):
45+
"""Report not server error on 300 response."""
46+
resp = requests.Response()
47+
resp.status_code = 300
48+
err = APIError('', response=resp)
49+
assert err.is_server_error() is False
50+
51+
def test_is_server_error_400(self):
52+
"""Report not server error on 400 response."""
53+
resp = requests.Response()
54+
resp.status_code = 400
55+
err = APIError('', response=resp)
56+
assert err.is_server_error() is False
57+
58+
def test_is_server_error_500(self):
59+
"""Report server error on 500 response."""
60+
resp = requests.Response()
61+
resp.status_code = 500
62+
err = APIError('', response=resp)
63+
assert err.is_server_error() is True
64+
65+
def test_is_client_error_500(self):
66+
"""Report not client error on 500 response."""
67+
resp = requests.Response()
68+
resp.status_code = 500
69+
err = APIError('', response=resp)
70+
assert err.is_client_error() is False
71+
72+
def test_is_client_error_400(self):
73+
"""Report client error on 400 response."""
74+
resp = requests.Response()
75+
resp.status_code = 400
76+
err = APIError('', response=resp)
77+
assert err.is_client_error() is True
78+
1479

1580
class CreateUnexpectedKwargsErrorTest(unittest.TestCase):
1681
def test_create_unexpected_kwargs_error_single(self):

0 commit comments

Comments
 (0)