2010-06-23 22:04:16 -07:00
|
|
|
# Copyright 2010 United States Government as represented by the
|
2010-06-23 23:15:06 -07:00
|
|
|
# Administrator of the National Aeronautics and Space Administration.
|
2010-06-23 22:04:16 -07:00
|
|
|
# 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
|
2010-05-30 15:21:34 -07:00
|
|
|
#
|
2010-06-23 22:04:16 -07:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2010-05-30 15:21:34 -07:00
|
|
|
#
|
2010-05-27 23:05:26 -07:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
2010-06-23 22:04:16 -07:00
|
|
|
# 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.
|
2010-05-27 23:05:26 -07:00
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
"""VersionedObjects base exception handling.
|
2011-04-20 12:08:22 -07:00
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
Includes decorator for re-raising VersionedObjects-type exceptions.
|
2011-04-20 12:08:22 -07:00
|
|
|
|
|
|
|
SHOULD include dedicated exception logging.
|
|
|
|
|
2010-05-27 23:05:26 -07:00
|
|
|
"""
|
|
|
|
|
2016-04-18 17:41:18 +08:00
|
|
|
import inspect
|
2015-02-02 16:24:14 -05:00
|
|
|
import logging
|
2013-06-14 17:53:53 +04:00
|
|
|
import sys
|
2011-04-20 12:08:22 -07:00
|
|
|
|
2015-02-02 16:24:14 -05:00
|
|
|
from oslo_config import cfg
|
|
|
|
from oslo_utils import excutils
|
2015-02-04 15:59:34 +01:00
|
|
|
import six
|
2011-12-29 10:57:38 -05:00
|
|
|
import webob.exc
|
2011-09-20 01:59:05 -04:00
|
|
|
|
2015-02-02 16:24:14 -05:00
|
|
|
from oslo_versionedobjects._i18n import _, _LE
|
2011-04-20 12:08:22 -07:00
|
|
|
|
2012-02-14 12:07:02 -06:00
|
|
|
LOG = logging.getLogger(__name__)
|
2010-05-27 23:05:26 -07:00
|
|
|
|
2012-12-18 09:50:56 -05:00
|
|
|
exc_log_opts = [
|
|
|
|
cfg.BoolOpt('fatal_exception_format_errors',
|
|
|
|
default=False,
|
2014-01-18 09:45:48 +01:00
|
|
|
help='Make exception message format errors fatal'),
|
2012-12-18 09:50:56 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
CONF = cfg.CONF
|
2015-03-19 13:57:41 -04:00
|
|
|
CONF.register_opts(exc_log_opts, group='oslo_versionedobjects')
|
2012-12-18 09:50:56 -05:00
|
|
|
|
2010-08-16 14:16:21 +02:00
|
|
|
|
2011-12-29 10:57:38 -05:00
|
|
|
class ConvertedException(webob.exc.WSGIHTTPException):
|
|
|
|
def __init__(self, code=0, title="", explanation=""):
|
|
|
|
self.code = code
|
|
|
|
self.title = title
|
|
|
|
self.explanation = explanation
|
|
|
|
super(ConvertedException, self).__init__()
|
|
|
|
|
|
|
|
|
2013-02-21 10:21:39 -04:00
|
|
|
def _cleanse_dict(original):
|
|
|
|
"""Strip all admin_password, new_pass, rescue_pass keys from a dict."""
|
2015-12-02 16:34:35 +08:00
|
|
|
return {k: v for k, v in original.items() if "_pass" not in k}
|
2013-02-21 10:21:39 -04:00
|
|
|
|
|
|
|
|
2013-08-29 22:38:31 +01:00
|
|
|
def wrap_exception(notifier=None, get_notifier=None):
|
2015-04-03 14:08:13 +00:00
|
|
|
"""Catch all exceptions in wrapped method
|
|
|
|
|
|
|
|
This decorator wraps a method to catch any exceptions that may
|
2015-01-07 08:03:46 -08:00
|
|
|
get thrown. It also optionally sends the exception to the notification
|
|
|
|
system.
|
2011-06-29 18:30:15 -07:00
|
|
|
"""
|
|
|
|
def inner(f):
|
2013-01-08 16:20:12 -06:00
|
|
|
def wrapped(self, context, *args, **kw):
|
|
|
|
# Don't store self or context in the payload, it now seems to
|
|
|
|
# contain confidential information.
|
2011-06-29 18:30:15 -07:00
|
|
|
try:
|
2013-01-08 16:20:12 -06:00
|
|
|
return f(self, context, *args, **kw)
|
2013-05-18 00:18:18 +02:00
|
|
|
except Exception as e:
|
2012-05-03 14:29:50 -04:00
|
|
|
with excutils.save_and_reraise_exception():
|
2013-08-29 22:38:31 +01:00
|
|
|
if notifier or get_notifier:
|
|
|
|
payload = dict(exception=e)
|
2016-04-18 17:41:18 +08:00
|
|
|
call_dict = inspect.getcallargs(f, self, context,
|
|
|
|
*args, **kw)
|
2013-08-29 22:38:31 +01:00
|
|
|
cleansed = _cleanse_dict(call_dict)
|
|
|
|
payload.update({'args': cleansed})
|
|
|
|
|
|
|
|
# If f has multiple decorators, they must use
|
2015-02-13 12:44:50 -08:00
|
|
|
# six.wraps to ensure the name is
|
2013-08-29 22:38:31 +01:00
|
|
|
# propagated.
|
|
|
|
event_type = f.__name__
|
|
|
|
|
|
|
|
(notifier or get_notifier()).error(context,
|
|
|
|
event_type,
|
|
|
|
payload)
|
2011-06-29 18:30:15 -07:00
|
|
|
|
2015-02-13 12:44:50 -08:00
|
|
|
return six.wraps(f)(wrapped)
|
2011-06-29 18:30:15 -07:00
|
|
|
return inner
|
2011-04-14 17:34:09 -04:00
|
|
|
|
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
class VersionedObjectsException(Exception):
|
|
|
|
"""Base VersionedObjects Exception
|
2011-04-15 14:33:24 -04:00
|
|
|
|
|
|
|
To correctly use this class, inherit from it and define
|
2013-07-01 11:31:06 +01:00
|
|
|
a 'msg_fmt' property. That msg_fmt will get printf'd
|
2011-04-15 14:33:24 -04:00
|
|
|
with the keyword arguments provided to the constructor.
|
|
|
|
|
|
|
|
"""
|
2013-07-01 11:31:06 +01:00
|
|
|
msg_fmt = _("An unknown exception occurred.")
|
2012-07-10 08:16:27 +01:00
|
|
|
code = 500
|
|
|
|
headers = {}
|
2012-07-06 10:10:28 +00:00
|
|
|
safe = False
|
2011-04-14 17:34:09 -04:00
|
|
|
|
2011-09-28 15:01:27 +00:00
|
|
|
def __init__(self, message=None, **kwargs):
|
2011-08-23 15:14:09 -07:00
|
|
|
self.kwargs = kwargs
|
2012-02-09 00:15:10 +00:00
|
|
|
|
|
|
|
if 'code' not in self.kwargs:
|
|
|
|
try:
|
|
|
|
self.kwargs['code'] = self.code
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2011-09-28 15:01:27 +00:00
|
|
|
if not message:
|
|
|
|
try:
|
2013-07-01 11:31:06 +01:00
|
|
|
message = self.msg_fmt % kwargs
|
2011-04-15 14:33:24 -04:00
|
|
|
|
2013-06-14 17:53:53 +04:00
|
|
|
except Exception:
|
|
|
|
exc_info = sys.exc_info()
|
2012-04-02 10:01:04 -04:00
|
|
|
# kwargs doesn't match a variable in the message
|
|
|
|
# log the issue and the kwargs
|
2014-09-22 15:39:12 +03:00
|
|
|
LOG.exception(_LE('Exception in string format operation'))
|
2015-07-29 15:15:19 +02:00
|
|
|
for name, value in kwargs.items():
|
2014-03-02 00:04:24 -08:00
|
|
|
LOG.error("%s: %s" % (name, value)) # noqa
|
2012-12-18 09:50:56 -05:00
|
|
|
|
2015-03-19 13:57:41 -04:00
|
|
|
if CONF.oslo_versionedobjects.fatal_exception_format_errors:
|
2015-02-04 15:59:34 +01:00
|
|
|
raise six.reraise(*exc_info)
|
2012-12-18 09:50:56 -05:00
|
|
|
else:
|
|
|
|
# at least get the core message out if something happened
|
2013-07-01 11:31:06 +01:00
|
|
|
message = self.msg_fmt
|
2011-04-14 17:34:09 -04:00
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
super(VersionedObjectsException, self).__init__(message)
|
2011-04-14 17:34:09 -04:00
|
|
|
|
2013-03-25 17:10:23 +01:00
|
|
|
def format_message(self):
|
2013-09-23 12:27:04 +00:00
|
|
|
# NOTE(mrodden): use the first argument to the python Exception object
|
2015-02-03 11:52:41 +01:00
|
|
|
# which should be our full VersionedObjectsException message,
|
|
|
|
# (see __init__)
|
2013-09-23 12:27:04 +00:00
|
|
|
return self.args[0]
|
2013-03-25 17:10:23 +01:00
|
|
|
|
2011-04-14 17:34:09 -04:00
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
class ObjectActionError(VersionedObjectsException):
|
2015-02-02 16:24:14 -05:00
|
|
|
msg_fmt = _('Object action %(action)s failed because: %(reason)s')
|
2013-05-21 09:44:03 -07:00
|
|
|
|
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
class ObjectFieldInvalid(VersionedObjectsException):
|
2015-02-02 16:24:14 -05:00
|
|
|
msg_fmt = _('Field %(field)s of %(objname)s is not an instance of Field')
|
2013-05-21 09:44:03 -07:00
|
|
|
|
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
class OrphanedObjectError(VersionedObjectsException):
|
2013-07-01 11:31:06 +01:00
|
|
|
msg_fmt = _('Cannot call %(method)s on orphaned %(objtype)s object')
|
2013-05-21 09:44:03 -07:00
|
|
|
|
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
class IncompatibleObjectVersion(VersionedObjectsException):
|
2013-07-01 11:31:06 +01:00
|
|
|
msg_fmt = _('Version %(objver)s of %(objname)s is not supported')
|
2013-05-18 12:48:44 +09:30
|
|
|
|
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
class ReadOnlyFieldError(VersionedObjectsException):
|
2014-04-16 15:08:51 -07:00
|
|
|
msg_fmt = _('Cannot modify readonly field %(field)s')
|
|
|
|
|
|
|
|
|
2015-02-03 11:52:41 +01:00
|
|
|
class UnsupportedObjectError(VersionedObjectsException):
|
2015-02-02 16:24:14 -05:00
|
|
|
msg_fmt = _('Unsupported object type %(objtype)s')
|
2015-03-28 22:16:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class EnumRequiresValidValuesError(VersionedObjectsException):
|
|
|
|
msg_fmt = _('Enum fields require a list of valid_values')
|
|
|
|
|
|
|
|
|
|
|
|
class EnumValidValuesInvalidError(VersionedObjectsException):
|
|
|
|
msg_fmt = _('Enum valid values are not valid')
|
2015-05-15 17:18:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EnumFieldInvalid(VersionedObjectsException):
|
|
|
|
msg_fmt = _('%(typename)s in %(fieldname)s is not an instance of Enum')
|
|
|
|
|
|
|
|
|
|
|
|
class EnumFieldUnset(VersionedObjectsException):
|
|
|
|
msg_fmt = _('%(fieldname)s missing field type')
|
2015-07-31 19:55:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidTargetVersion(VersionedObjectsException):
|
|
|
|
msg_fmt = _('Invalid target version %(version)s')
|
2015-07-15 21:21:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TargetBeforeSubobjectExistedException(VersionedObjectsException):
|
|
|
|
msg_fmt = _("No subobject existed at version %(target_version)s")
|