Handle response is not a json format

Change-Id: I62a6c6d248301576176be5631a4ef4900460082d
This commit is contained in:
Tao Zou 2022-05-27 14:40:23 +08:00
parent d660b484f3
commit d915f2fecb
3 changed files with 34 additions and 1 deletions

View File

@ -329,6 +329,10 @@ class NsxV3RESTClientTestCase(nsxlib_testcase.NsxClientTestCase):
500157, [777, 500045])
self.assertEqual(exc, nsxlib_exc.NsxPendingDelete)
code = requests.codes.SERVICE_UNAVAILABLE
exc = client.http_error_to_exception(code, None)
self.assertEqual(exc, nsxlib_exc.ServiceUnavailable)
class NsxV3JSONClientTestCase(nsxlib_testcase.NsxClientTestCase):

View File

@ -416,6 +416,24 @@ class ClusteredAPITestCase(nsxlib_testcase.NsxClientTestCase):
# This exception does not ground endpoint
self.assertEqual(cluster.ClusterHealth.GREEN, api.health)
def test_max_retry_attempts_on_none_json_error(self):
# handle response not json format
def server1_error():
return mocks.MockRequestsResponse(
codes.SERVICE_UNAVAILABLE,
"python-requests/2.27.1 d50561a7-7540-4342-88e3-c235e587a19")
conf_managers = ['8.9.10.11', '9.10.11.12', '10.11.12.13']
max_attempts = 2
api = self.mock_nsx_clustered_api(
nsx_api_managers=conf_managers,
max_attempts=max_attempts,
session_response=[server1_error for i in range(0, max_attempts)])
self.assertRaises(nsxlib_exc.ServiceUnavailable,
api.get, 'api/v1/transport-zones')
self.assertEqual(cluster.ClusterHealth.GREEN, api.health)
def test_regenerate_error(self):
def server_error():

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
#
import json
import re
import time
from urllib import parse as urlparse
@ -32,7 +33,17 @@ NULL_CURSOR_PREFIX = '0000'
def get_http_error_details(response):
msg = response.json() if response.content else ''
try:
msg = response.json() if response.content else ''
except json.decoder.JSONDecodeError as e:
LOG.debug("decode response %s error %s", response.content, e)
# error_code can't be None else it can't be casted to an exception
return {'status_code': response.status_code,
'error_code': response.status_code,
'related_error_codes': [],
'related_status_codes': [],
'details': response.content}
error_code = None
related_error_codes = []
related_status_codes = []