2011-02-25 02:00:13 -08:00
|
|
|
# Copyright 2010 Jacob Kaplan-Moss
|
2013-12-06 10:47:41 +10:30
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2011-02-24 17:59:42 -04:00
|
|
|
"""
|
2011-02-24 13:54:10 -04:00
|
|
|
Exception definitions.
|
|
|
|
"""
|
|
|
|
|
2014-01-29 13:03:26 +02:00
|
|
|
import inspect
|
|
|
|
import sys
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2014-01-29 13:03:26 +02:00
|
|
|
import six
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2014-01-29 13:03:26 +02:00
|
|
|
from novaclient.openstack.common.apiclient.exceptions import * # noqa
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2014-01-29 13:03:26 +02:00
|
|
|
# NOTE(akurilin): Since v.2.17.0 this alias should be left here to support
|
|
|
|
# backwards compatibility.
|
|
|
|
OverLimit = RequestEntityTooLarge
|
2012-04-09 20:32:37 +00:00
|
|
|
|
|
|
|
|
2014-05-19 13:48:29 +03:00
|
|
|
def _deprecate_code_attribute(slf):
|
|
|
|
import warnings
|
|
|
|
warnings.warn("'code' attribute is deprecated since v.2.17.0. Use "
|
|
|
|
"'http_status' instead of this one.", UserWarning)
|
|
|
|
return slf.http_status
|
|
|
|
|
|
|
|
HttpError.code = property(_deprecate_code_attribute)
|
|
|
|
|
|
|
|
|
2014-01-29 13:03:26 +02:00
|
|
|
class NoTokenLookupException(ClientException):
|
2011-09-09 06:33:38 -07:00
|
|
|
"""This form of authentication does not support looking up
|
2013-12-12 03:55:50 +00:00
|
|
|
endpoints from an existing token.
|
|
|
|
"""
|
2011-09-09 06:33:38 -07:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-14 15:32:10 -04:00
|
|
|
class InstanceInErrorState(Exception):
|
|
|
|
"""Instance is in the error state."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-01-29 13:03:26 +02:00
|
|
|
class RateLimit(RequestEntityTooLarge):
|
2013-07-16 09:35:42 +00:00
|
|
|
"""
|
|
|
|
HTTP 429 - Rate limit: you've sent too many requests for this time period.
|
|
|
|
"""
|
|
|
|
http_status = 429
|
|
|
|
message = "Rate limit"
|
|
|
|
|
|
|
|
|
2014-01-29 13:03:26 +02:00
|
|
|
_code_map = dict(
|
|
|
|
(getattr(obj, 'http_status', None), obj)
|
|
|
|
for name, obj in six.iteritems(vars(sys.modules[__name__]))
|
|
|
|
if inspect.isclass(obj) and getattr(obj, 'http_status', False)
|
|
|
|
)
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
|
2014-04-08 11:40:41 +10:00
|
|
|
class InvalidUsage(RuntimeError):
|
|
|
|
"""This function call is invalid in the way you are using this client.
|
|
|
|
|
|
|
|
Due to the transition to using keystoneclient some function calls are no
|
|
|
|
longer available. You should make a similar call to the session object
|
|
|
|
instead.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-01-23 11:14:41 -06:00
|
|
|
def from_response(response, body, url, method=None):
|
2011-01-25 14:01:22 -06:00
|
|
|
"""
|
2014-05-19 13:48:29 +03:00
|
|
|
Return an instance of an HttpError or subclass
|
2012-12-18 14:05:29 -06:00
|
|
|
based on an requests response.
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
Usage::
|
|
|
|
|
2012-12-18 14:05:29 -06:00
|
|
|
resp, body = requests.request(...)
|
|
|
|
if resp.status_code != 200:
|
|
|
|
raise exception_from_response(resp, rest.text)
|
2011-01-25 14:01:22 -06:00
|
|
|
"""
|
2013-04-29 11:33:32 -07:00
|
|
|
kwargs = {
|
2014-01-29 13:03:26 +02:00
|
|
|
'http_status': response.status_code,
|
2013-04-29 11:33:32 -07:00
|
|
|
'method': method,
|
|
|
|
'url': url,
|
|
|
|
'request_id': None,
|
|
|
|
}
|
|
|
|
|
2012-12-18 14:05:29 -06:00
|
|
|
if response.headers:
|
2013-04-29 11:33:32 -07:00
|
|
|
kwargs['request_id'] = response.headers.get('x-compute-request-id')
|
|
|
|
|
2013-04-29 11:34:24 -07:00
|
|
|
if 'retry-after' in response.headers:
|
|
|
|
kwargs['retry_after'] = response.headers.get('retry-after')
|
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
if body:
|
|
|
|
message = "n/a"
|
|
|
|
details = "n/a"
|
2013-04-29 11:33:32 -07:00
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
if hasattr(body, 'keys'):
|
2013-09-22 22:06:41 +08:00
|
|
|
error = body[list(body)[0]]
|
2014-02-19 21:02:11 +08:00
|
|
|
message = error.get('message')
|
|
|
|
details = error.get('details')
|
2013-04-29 11:33:32 -07:00
|
|
|
|
|
|
|
kwargs['message'] = message
|
|
|
|
kwargs['details'] = details
|
|
|
|
|
2014-05-19 13:48:29 +03:00
|
|
|
cls = _code_map.get(response.status_code, HttpError)
|
|
|
|
|
2013-04-29 11:33:32 -07:00
|
|
|
return cls(**kwargs)
|