From d915f2fecbc0e629b67873d4923c4aba9f0bcdcc Mon Sep 17 00:00:00 2001 From: Tao Zou Date: Fri, 27 May 2022 14:40:23 +0800 Subject: [PATCH] Handle response is not a json format Change-Id: I62a6c6d248301576176be5631a4ef4900460082d --- vmware_nsxlib/tests/unit/v3/test_client.py | 4 ++++ vmware_nsxlib/tests/unit/v3/test_cluster.py | 18 ++++++++++++++++++ vmware_nsxlib/v3/client.py | 13 ++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/vmware_nsxlib/tests/unit/v3/test_client.py b/vmware_nsxlib/tests/unit/v3/test_client.py index fce1aebf..6456e980 100644 --- a/vmware_nsxlib/tests/unit/v3/test_client.py +++ b/vmware_nsxlib/tests/unit/v3/test_client.py @@ -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): diff --git a/vmware_nsxlib/tests/unit/v3/test_cluster.py b/vmware_nsxlib/tests/unit/v3/test_cluster.py index 7b4aac8c..cd97af8d 100644 --- a/vmware_nsxlib/tests/unit/v3/test_cluster.py +++ b/vmware_nsxlib/tests/unit/v3/test_cluster.py @@ -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(): diff --git a/vmware_nsxlib/v3/client.py b/vmware_nsxlib/v3/client.py index f29e7227..fedddeed 100644 --- a/vmware_nsxlib/v3/client.py +++ b/vmware_nsxlib/v3/client.py @@ -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 = []