| 1 |
# Copyright 2017 FUJITSU LIMITED |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
""" |
| 16 |
|
| 17 |
Patches method that transforms error responses. |
| 18 |
That is required to handle different format monasca follows. |
| 19 |
|
| 20 |
""" |
| 21 |
|
| 22 |
from keystoneauth1 import exceptions as exc |
| 23 |
from keystoneauth1.exceptions import http |
| 24 |
|
| 25 |
|
| 26 |
def mon_exc_from_response(response, method, url): |
| 27 |
req_id = response.headers.get('x-openstack-request-id') |
| 28 |
|
| 29 |
kwargs = { |
| 30 |
'http_status': response.status_code, |
| 31 |
'response': response, |
| 32 |
'method': method, |
| 33 |
'url': url, |
| 34 |
'request_id': req_id, |
| 35 |
} |
| 36 |
if 'retry-after' in response.headers: |
| 37 |
kwargs['retry_after'] = response.headers['retry-after'] |
| 38 |
|
| 39 |
content_type = response.headers.get('Content-Type', '') |
| 40 |
if content_type.startswith('application/json'): |
| 41 |
try: |
| 42 |
body = response.json() |
| 43 |
except ValueError: |
| 44 |
pass |
| 45 |
else: |
| 46 |
if isinstance(body, dict): |
| 47 |
if isinstance(body.get('error'), dict): |
| 48 |
error = body['error'] |
| 49 |
kwargs['message'] = error.get('message') |
| 50 |
kwargs['details'] = error.get('details') |
| 51 |
elif {'description', 'title'} <= set(body): |
| 52 |
# monasca-api error response structure |
| 53 |
kwargs['message'] = body.get('title') |
| 54 |
kwargs['details'] = body.get('description') |
| 55 |
elif content_type.startswith('text/'): |
| 56 |
kwargs['details'] = response.text |
| 57 |
|
| 58 |
try: |
| 59 |
cls = http._code_map[response.status_code] |
| 60 |
except KeyError: |
| 61 |
if 500 <= response.status_code < 600: |
| 62 |
cls = exc.HttpServerError |
| 63 |
elif 400 <= response.status_code < 500: |
| 64 |
cls = exc.HTTPClientError |
| 65 |
else: |
| 66 |
cls = exc.HttpError |
| 67 |
return cls(**kwargs) |
| 68 |
|
| 69 |
|
| 70 |
exc.from_response = mon_exc_from_response |