/usr/lib/python3/dist-packages/sushy/exceptions.py is in python3-sushy 1.3.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | # Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# 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.
import logging
from six.moves import http_client
LOG = logging.getLogger(__name__)
class SushyError(Exception):
"""Basic exception for errors raised by Sushy"""
message = None
def __init__(self, **kwargs):
if self.message and kwargs:
self.message = self.message % kwargs
super(SushyError, self).__init__(self.message)
class ConnectionError(SushyError):
message = 'Unable to connect to %(url)s. Error: %(error)s'
class MissingAttributeError(SushyError):
message = ('The attribute %(attribute)s is missing from the '
'resource %(resource)s')
class MalformedAttributeError(SushyError):
message = ('The attribute %(attribute)s is malformed in the '
'resource %(resource)s: %(error)s')
class MissingActionError(SushyError):
message = ('The action %(action)s is missing from the '
'resource %(resource)s')
class InvalidParameterValueError(SushyError):
message = ('The parameter "%(parameter)s" value "%(value)s" is invalid. '
'Valid values are: %(valid_values)s')
class HTTPError(SushyError):
"""Basic exception for HTTP errors"""
status_code = None
"""HTTP status code."""
body = None
"""Error JSON body, if present."""
code = 'Base.1.0.GeneralError'
"""Error code defined in the Redfish specification, if present."""
detail = None
"""Error message defined in the Redfish specification, if present."""
message = ('HTTP %(method)s %(url)s returned code %(code)s. %(error)s')
def __init__(self, method, url, response):
self.status_code = response.status_code
try:
body = response.json()
except ValueError:
LOG.warning('Error response from %(method)s %(url)s '
'with status code %(code)s has no JSON body',
{'method': method, 'url': url, 'code':
self.status_code})
error = 'unknown error'
else:
# TODO(dtantsur): parse @Message.ExtendedInfo
self.body = body.get('error', {})
self.code = self.body.get('code', 'Base.1.0.GeneralError')
self.detail = self.body.get('message')
error = '%s: %s' % (self.code, self.detail or 'unknown error')
kwargs = {'method': method, 'url': url, 'code': self.status_code,
'error': error}
LOG.debug('HTTP response for %(method)s %(url)s: '
'status code: %(code)s, error: %(error)s', kwargs)
super(HTTPError, self).__init__(**kwargs)
class BadRequestError(HTTPError):
pass
class ResourceNotFoundError(HTTPError):
# Overwrite the complex generic message with a simpler one.
message = 'Resource %(url)s not found'
class ServerSideError(HTTPError):
pass
class AccessError(HTTPError):
pass
class MissingXAuthToken(HTTPError):
message = ('No X-Auth-Token returned from remote host when '
'attempting to establish a session. Error: %(error)s')
def raise_for_response(method, url, response):
"""Raise a correct error class, if needed."""
if response.status_code < http_client.BAD_REQUEST:
return
elif response.status_code == http_client.NOT_FOUND:
raise ResourceNotFoundError(method, url, response)
elif response.status_code == http_client.BAD_REQUEST:
raise BadRequestError(method, url, response)
elif response.status_code in (http_client.UNAUTHORIZED,
http_client.FORBIDDEN):
raise AccessError(method, url, response)
elif response.status_code >= http_client.INTERNAL_SERVER_ERROR:
raise ServerSideError(method, url, response)
else:
raise HTTPError(method, url, response)
|