Skip to content

Commit 2d0f9dd

Browse files
committed
fix: use the correct attribute name for status code on error
Signed-off-by: Norbert Biczo <[email protected]>
1 parent 3f41abf commit 2d0f9dd

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

ibm_cloud_sdk_core/api_exception.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import warnings
1718
from http import HTTPStatus
1819
from typing import Optional
1920

@@ -39,15 +40,28 @@ def __init__(self, code: int, *, message: Optional[str] = None, http_response: O
3940
# Call the base class constructor with the parameters it needs
4041
super().__init__(message)
4142
self.message = message
42-
self.code = code
43+
self.status_code = code
4344
self.http_response = http_response
4445
self.global_transaction_id = None
4546
if http_response is not None:
4647
self.global_transaction_id = http_response.headers.get('X-Global-Transaction-ID')
4748
self.message = self.message if self.message else self._get_error_message(http_response)
4849

50+
# pylint: disable=fixme
51+
# TODO: delete this by the end of 2024.
52+
@property
53+
def code(self):
54+
"""The old `code` property with a deprecation warning."""
55+
56+
warnings.warn(
57+
'Using the `code` attribute on the `ApiException` is deprecated and'
58+
'will be removed in the future. Use `status_code` instead.',
59+
DeprecationWarning,
60+
)
61+
return self.status_code
62+
4963
def __str__(self) -> str:
50-
msg = 'Error: ' + str(self.message) + ', Code: ' + str(self.code)
64+
msg = 'Error: ' + str(self.message) + ', Status code: ' + str(self.status_code)
5165
if self.global_transaction_id is not None:
5266
msg += ' , X-global-transaction-id: ' + str(self.global_transaction_id)
5367
return msg

test/test_api_exception.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ def test_api_exception():
8585
mock_response = requests.get('https://test-for-text.com', timeout=None)
8686
exception = ApiException(500, http_response=mock_response)
8787
assert exception.message == 'plain text error'
88-
assert str(exception) == 'Error: plain text error, Code: 500 , X-global-transaction-id: xx'
88+
assert str(exception) == 'Error: plain text error, Status code: 500 , X-global-transaction-id: xx'

test/test_base_service.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def test_request_server_error():
356356
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
357357
prepped = service.prepare_request('GET', url='')
358358
service.send(prepped)
359-
assert err.value.code == 500
359+
assert err.value.status_code == 500
360360
assert err.value.http_response.headers['Content-Type'] == 'application/json'
361361
assert err.value.message == 'internal server error'
362362

@@ -398,7 +398,7 @@ def test_request_success_invalid_json():
398398
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
399399
prepped = service.prepare_request('GET', url='')
400400
service.send(prepped)
401-
assert err.value.code == 200
401+
assert err.value.status_code == 200
402402
assert err.value.http_response.headers['Content-Type'] == 'application/json; charset=utf8'
403403
assert isinstance(err.value.__cause__, requests.exceptions.JSONDecodeError)
404404
assert "Expecting ':' delimiter: line 1" in str(err.value.__cause__)
@@ -452,7 +452,7 @@ def test_request_fail_401_nonerror_json():
452452
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
453453
prepped = service.prepare_request('GET', url='')
454454
service.send(prepped)
455-
assert err.value.code == 401
455+
assert err.value.status_code == 401
456456
assert err.value.http_response.headers['Content-Type'] == 'application/json'
457457
assert err.value.message == error_msg
458458

@@ -472,7 +472,7 @@ def test_request_fail_401_error_json():
472472
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
473473
prepped = service.prepare_request('GET', url='')
474474
service.send(prepped)
475-
assert err.value.code == 401
475+
assert err.value.status_code == 401
476476
assert err.value.http_response.headers['Content-Type'] == 'application/json'
477477
assert err.value.message == error_msg
478478

@@ -491,7 +491,7 @@ def test_request_fail_401_nonjson():
491491
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
492492
prepped = service.prepare_request('GET', url='')
493493
service.send(prepped)
494-
assert err.value.code == 401
494+
assert err.value.status_code == 401
495495
assert err.value.http_response.headers['Content-Type'] == 'text/plain'
496496
assert err.value.message == response_body
497497

@@ -513,7 +513,7 @@ def test_request_fail_401_badjson():
513513
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
514514
prepped = service.prepare_request('GET', url='')
515515
service.send(prepped)
516-
assert err.value.code == 401
516+
assert err.value.status_code == 401
517517
assert err.value.http_response.headers['Content-Type'] == 'application/json'
518518
assert err.value.message == response_body
519519

test/test_container_token_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def test_authenticate_fail_iam():
251251
with pytest.raises(ApiException) as err:
252252
authenticator.authenticate(request)
253253

254-
assert str(err.value) == 'Error: Bad Request, Code: 400'
254+
assert str(err.value) == 'Error: Bad Request, Status code: 400'
255255

256256

257257
@mock_iam_response

0 commit comments

Comments
 (0)