Browse Source

Handle error response for webob>=1.6.0

WebOb change https://github.com/Pylons/webob/pull/230 changed
the way in which the error response body is formatted such that
it's no longer a nested dict. So we have to handle both the
old convention of an error message key to the response body error
dict and the new way with just the error body dict.

This was reported upstream:
https://github.com/Pylons/webob/issues/235

But given this was apparently implemented as a long-overdue change
in WebOb the behavior is not likely to change.

Change-Id: Id8bfde6277640de48768bbe5a5e4252b97253ed2
Closes-Bug: #1559072
Co-Authored-By: Matt Riedemann <mriedem@us.ibm.com>
Signed-off-by: Zhao Chao <zhaochao1984@gmail.com>
Zhao Chao 1 year ago
parent
commit
0b28b04ebb

+ 20
- 4
troveclient/apiclient/exceptions.py View File

@@ -426,10 +426,26 @@ def from_response(response, method, url):
426 426
         except ValueError:
427 427
             pass
428 428
         else:
429
-            if isinstance(body, dict):
430
-                error = list(body.values())[0]
431
-                kwargs["message"] = error.get("message", None)
432
-                kwargs["details"] = error.get("details", None)
429
+            if hasattr(body, 'keys'):
430
+                # NOTE(mriedem): WebOb<1.6.0 will return a nested dict
431
+                # structure  where the error keys to the
432
+                # message/details/code. WebOb>=1.6.0 returns just a response
433
+                # body as a single dict, not nested, so we have to handle
434
+                # both cases (since we can't trust what we're given with
435
+                # content_type: application/json either way.
436
+                if 'message' in body:
437
+                    # WebOb 1.6.0 case
438
+                    message = body.get('message')
439
+                    details = body.get('details')
440
+                else:
441
+                    # WebOb<1.6.0 where we assume there is a single error
442
+                    # message key to the body that has the message and details.
443
+                    error = list(body.values())[0]
444
+                    message = error.get("message")
445
+                    details = error.get("details")
446
+
447
+                kwargs["message"] = message
448
+                kwargs["details"] = details
433 449
     elif content_type.startswith("text/"):
434 450
         kwargs["details"] = response.text
435 451
 

+ 51
- 0
troveclient/tests/test_apiclient_exceptions.py View File

@@ -0,0 +1,51 @@
1
+#   Licensed under the Apache License, Version 2.0 (the "License"); you may
2
+#   not use this file except in compliance with the License. You may obtain
3
+#   a copy of the License at
4
+#
5
+#        http://www.apache.org/licenses/LICENSE-2.0
6
+#
7
+#   Unless required by applicable law or agreed to in writing, software
8
+#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
+#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
+#   License for the specific language governing permissions and limitations
11
+#   under the License.
12
+
13
+import json
14
+
15
+from troveclient.apiclient import exceptions
16
+from troveclient.tests import utils as test_utils
17
+
18
+
19
+class ExceptionsTestCase(test_utils.TestCase):
20
+
21
+    def _test_from_response(self, body):
22
+        data = {
23
+            'status_code': 503,
24
+            'headers': {
25
+                'Content-Type': 'application/json',
26
+                'x-compute-request-id': (
27
+                    'req-65d6443c-5910-4eb4-b48a-e69849c26836'),
28
+            },
29
+            'text': json.dumps(body)
30
+        }
31
+        response = test_utils.TestResponse(data)
32
+        fake_url = 'http://localhost:8779/v1.0/fake/instances'
33
+        error = exceptions.from_response(response, 'GET', fake_url)
34
+        self.assertIsInstance(error, exceptions.ServiceUnavailable)
35
+
36
+    def test_from_response_webob_pre_1_6_0(self):
37
+        # Tests error responses before webob 1.6.0 where the error details
38
+        # are nested in the response body.
39
+        body = {
40
+            'serviceUnavailable': {
41
+                'message': 'Fake message.',
42
+                'code': 503
43
+            }
44
+        }
45
+        self._test_from_response(body)
46
+
47
+    def test_from_response_webob_post_1_6_0(self):
48
+        # Tests error responses from webob 1.6.0 where the error details
49
+        # are in the response body.
50
+        body = {'message': 'Fake message.', 'code': 503}
51
+        self._test_from_response(body)

+ 4
- 0
troveclient/tests/utils.py View File

@@ -95,3 +95,7 @@ class TestResponse(requests.Response):
95 95
             self._text = data.get('text')
96 96
         else:
97 97
             self.status_code = data
98
+
99
+    @property
100
+    def text(self):
101
+        return self._text

Loading…
Cancel
Save