68 lines
2.2 KiB
Python
Raw Normal View History

2015-01-05 09:15:26 +08:00
# 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.
from oslo_serialization import jsonutils
from openstack import exceptions as sdkexc
from senlinclient.common.i18n import _
verbose = False
2015-01-05 09:15:26 +08:00
class BaseException(Exception):
'''An error occurred.'''
2015-01-05 09:15:26 +08:00
def __init__(self, message=None):
self.message = message
def __str__(self):
return self.message or self.__class__.__doc__
2015-01-05 09:15:26 +08:00
class CommandError(BaseException):
'''Invalid usage of CLI.'''
2015-01-19 21:15:12 +08:00
2015-01-20 21:42:45 +08:00
2015-01-19 21:15:12 +08:00
class FileFormatError(BaseException):
'''Illegal file format detected.'''
2015-01-20 21:42:45 +08:00
class HTTPException(BaseException):
"""Base exception for all HTTP-derived exceptions."""
code = 'N/A'
def __init__(self, details=None):
super(HTTPException, self).__init__(details)
2015-01-20 21:42:45 +08:00
try:
self.error = jsonutils.loads(details)
2015-01-20 21:42:45 +08:00
if 'error' not in self.error:
raise KeyError(_('Key "error" not exists'))
except KeyError:
# If key 'error' does not exist, self.message becomes
# no sense. In this case, we return doc of current
# exception class instead.
self.error = {'error': { 'message': self.__class__.__doc__}}
2015-01-20 21:42:45 +08:00
except Exception:
self.error = {'error':
{'message': self.message or self.__class__.__doc__}}
def __str__(self):
message = self.error['error'].get('message', 'Internal Error')
if verbose:
traceback = self.error['error'].get('traceback', '')
return (_('ERROR: %(message)s\n%(traceback)s') %
{'message': message, 'traceback': traceback})
else:
return _('ERROR: %s') % message
class HTTPNotFound(HTTPException):
code = '404'