Files
neutron/neutron/pecan_wsgi/hooks/translation.py
Kevin Benton e433c2870a Add metrics notifier to Pecan
This adds the standard 'object.(create|update|delete).(start|end)'
notifications to the Pecan notification hook and adds unit tests
to exercise them.

This patch also corrects the on_error handler for untranslated
exceptions which was incorrectly raising the exception rather than
returning it. This was resulting in the other hooks not getting
the correct status code on an untranslated exception.

Closes-Bug: #1552979
Change-Id: I400f8d3988db204caed25e7c848a415b45d47172
2016-03-11 15:47:11 -08:00

40 lines
1.5 KiB
Python

# Copyright (c) 2015 Mirantis, 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.
from oslo_log import log as logging
from pecan import hooks
import webob.exc
from neutron._i18n import _, _LE
from neutron.api.v2 import base as v2base
LOG = logging.getLogger(__name__)
class ExceptionTranslationHook(hooks.PecanHook):
def on_error(self, state, e):
# if it's already an http error, just return to let it go through
if isinstance(e, webob.exc.WSGIHTTPException):
return
for exc_class, to_class in v2base.FAULT_MAP.items():
if isinstance(e, exc_class):
raise to_class(getattr(e, 'msg', e.message))
# leaked unexpected exception, convert to boring old 500 error and
# hide message from user in case it contained sensitive details
LOG.exception(_LE("An unexpected exception was caught: %s"), e)
return webob.exc.HTTPInternalServerError(
_("An unexpected internal error occurred."))