Skip to content

Commit d631acc

Browse files
committed
chore(error): parse error in a standard way
1 parent 49ed7eb commit d631acc

File tree

2 files changed

+17
-46
lines changed

2 files changed

+17
-46
lines changed

ibm_cloud_sdk_core/api_exception.py

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,18 @@ def __init__(self, code, message=None, info=None, http_response=None):
2828
super(ApiException, self).__init__(message)
2929
self.message = message
3030
self.code = code
31-
self.info = info
3231
self.http_response = http_response
3332
self.global_transaction_id = None
3433
if http_response is not None:
3534
self.global_transaction_id = http_response.headers.get('X-Global-Transaction-ID')
36-
self.info = self.info if self.info else self._get_error_info(http_response)
3735
self.message = self.message if self.message else self._get_error_message(http_response)
3836

3937
def __str__(self):
4038
msg = 'Error: ' + str(self.message) + ', Code: ' + str(self.code)
41-
if self.info is not None:
42-
msg += ' , Information: ' + str(self.info)
4339
if self.global_transaction_id is not None:
4440
msg += ' , X-global-transaction-id: ' + str(self.global_transaction_id)
4541
return msg
4642

47-
def _get_error_info(self, response):
48-
"""
49-
Gets the error info (if any) from a JSON response.
50-
:return: A `dict` containing additional information about the error.
51-
:rtype: dict
52-
"""
53-
info_keys = ['code_description', 'description', 'errors', 'help',
54-
'sub_code', 'warnings']
55-
error_info = {}
56-
try:
57-
error_json = response.json()
58-
error_info = {k:v for k, v in error_json.items() if k in info_keys}
59-
except:
60-
pass
61-
return error_info if any(error_info) else None
62-
6343
def _get_error_message(self, response):
6444
"""
6545
Gets the error message from a JSON response.
@@ -69,20 +49,14 @@ def _get_error_message(self, response):
6949
error_message = 'Unknown error'
7050
try:
7151
error_json = response.json()
72-
if 'error' in error_json:
73-
if isinstance(error_json['error'], dict) and 'description' in \
74-
error_json['error']:
75-
error_message = error_json['error']['description']
76-
else:
77-
error_message = error_json['error']
78-
elif 'error_message' in error_json:
79-
error_message = error_json['error_message']
80-
elif 'errorMessage' in error_json:
81-
error_message = error_json['errorMessage']
82-
elif 'msg' in error_json:
83-
error_message = error_json['msg']
84-
elif 'statusInfo' in error_json:
85-
error_message = error_json['statusInfo']
52+
if 'errors' in error_json:
53+
if isinstance(error_json['errors'], list):
54+
err = error_json['errors'][0]
55+
error_message = err.get('message')
56+
elif 'error' in error_json:
57+
error_message = error_json['error']
58+
elif 'message' in error_json:
59+
error_message = error_json['message']
8660
return error_message
8761
except:
8862
return response.text or error_message

test/test_api_exception.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ def test_api_exception():
2020
responses.add(responses.GET,
2121
'https://test-again.com',
2222
status=500,
23-
body=json.dumps({'error_message': 'sorry again', 'msg': 'serious error'}),
23+
headers={'global_transaction_id': 'xx'},
24+
body=json.dumps({
25+
"errors": [
26+
{
27+
"message": "sorry again",
28+
}],
29+
}),
2430
content_type='application/json')
2531
mock_response = requests.get('https://test-again.com')
2632
exception = ApiException(500, http_response=mock_response)
@@ -29,7 +35,7 @@ def test_api_exception():
2935
responses.add(responses.GET,
3036
'https://test-once-more.com',
3137
status=500,
32-
body=json.dumps({'errorMessage': 'sorry once more', 'msg': 'serious error'}),
38+
body=json.dumps({'message': 'sorry once more'}),
3339
content_type='application/json')
3440
mock_response = requests.get('https://test-once-more.com')
3541
exception = ApiException(500, http_response=mock_response)
@@ -42,16 +48,7 @@ def test_api_exception():
4248
content_type='application/json')
4349
mock_response = requests.get('https://test-msg.com')
4450
exception = ApiException(500, http_response=mock_response)
45-
assert exception.message == 'serious error'
46-
47-
responses.add(responses.GET,
48-
'https://test-status.com',
49-
status=500,
50-
body=json.dumps({'statusInfo': 'not yet provisioned'}),
51-
content_type='application/json')
52-
mock_response = requests.get('https://test-status.com')
53-
exception = ApiException(500, http_response=mock_response)
54-
assert exception.message == 'not yet provisioned'
51+
assert exception.message == 'Unknown error'
5552

5653
responses.add(responses.GET,
5754
'https://test-for-text.com',

0 commit comments

Comments
 (0)