本文整理汇总了Python中webob.exc.HTTPError方法的典型用法代码示例。如果您正苦于以下问题:Python exc.HTTPError方法的具体用法?Python exc.HTTPError怎么用?Python exc.HTTPError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类webob.exc
的用法示例。
在下文中一共展示了exc.HTTPError方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_exceptions
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPError [as 别名]
def handle_exceptions(operation_name=u._('System')):
"""Decorator handling generic exceptions from REST methods."""
def exceptions_decorator(fn):
def handler(inst, *args, **kwargs):
try:
return fn(inst, *args, **kwargs)
except exc.HTTPError:
LOG.exception(u._LE('Webob error seen'))
raise # Already converted to Webob exception, just reraise
except Exception as e:
# In case intervening modules have disabled logging.
LOG.logger.disabled = False
status, message = api.generate_safe_exception_message(
operation_name, e)
LOG.exception(message)
pecan.abort(status, message)
return handler
return exceptions_decorator
示例2: translate
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPError [as 别名]
def translate(translatable, locale):
"""Translate the object to the given locale.
If the object is an exception its translatable elements are translated
in place, if the object is a translatable string it is translated and
returned. Otherwise, the object is returned as-is.
:param translatable: the object to be translated
:param locale: the locale to translate to
:returns: the translated object, or the object as-is if it
was not translated
"""
localize = oslo_i18n.translate
if isinstance(translatable, exceptions.TackerException):
translatable.msg = localize(translatable.msg, locale)
elif isinstance(translatable, exc.HTTPError):
translatable.detail = localize(translatable.detail, locale)
elif isinstance(translatable, Exception):
translatable.message = localize(translatable, locale)
else:
return localize(translatable, locale)
return translatable
示例3: handle_exceptions
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPError [as 别名]
def handle_exceptions(operation_name=u._('System')):
"""Decorator handling generic exceptions from REST methods."""
def exceptions_decorator(fn):
def handler(inst, *args, **kwargs):
try:
return fn(inst, *args, **kwargs)
except exc.HTTPError:
LOG.exception('Webob error seen')
raise # Already converted to Webob exception, just reraise
# In case PolicyNotAuthorized, we do not want to expose payload by
# logging exception, so just LOG.error
except policy.PolicyNotAuthorized as pna:
status, message = api.generate_safe_exception_message(
operation_name, pna)
LOG.error(message)
pecan.abort(status, message)
except Exception as e:
# In case intervening modules have disabled logging.
LOG.logger.disabled = False
status, message = api.generate_safe_exception_message(
operation_name, e)
LOG.exception(message)
pecan.abort(status, message)
return handler
return exceptions_decorator
示例4: translate_exception
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPError [as 别名]
def translate_exception(ex, locale):
"""Translate all translatable elements of the given exception."""
if isinstance(ex, exception.SenlinException):
ex.message = oslo_i18n.translate(ex.message, locale)
else:
ex.message = oslo_i18n.translate(str(ex), locale)
if isinstance(ex, exc.HTTPError):
ex.explanation = oslo_i18n.translate(ex.explanation, locale)
ex.detail = oslo_i18n.translate(getattr(ex, 'detail', ''), locale)
return ex
示例5: nav_data
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPError [as 别名]
def nav_data(self, admin_options=False, navbar_entries=None):
"""
Return data about project nav entries
:param bool admin_options: include admin options?
:param navbar_entries: for performance, include this if you already have grouped_navbar_entries data
:return:
"""
from allura.ext.admin.admin_main import ProjectAdminRestController
grouping_threshold = self.get_tool_data('allura', 'grouping_threshold', 1)
anchored_tools = self.neighborhood.get_anchored_tools()
children = []
def make_entry(s, app_config):
entry = dict(name=s.label,
url=s.url,
icon=s.ui_icon or 'tool-admin',
tool_name=s.tool_name or 'sub',
mount_point=s.mount_point,
is_anchored=s.tool_name in list(anchored_tools.keys()),
)
if admin_options and app_config:
try:
entry['admin_options'] = ProjectAdminRestController().admin_options(app_config)['options']
except exc.HTTPError:
log.debug('Could not get admin_options mount_point for tool: %s', s.url, exc_info=True)
if admin_options and not s.tool_name:
entry['admin_options'] = [dict(text='Subproject Admin', href=s.url + 'admin', className=None)]
return entry
if navbar_entries is None:
navbar_entries = self.grouped_navbar_entries()
tools_by_mount = {ac.options.mount_point: ac for ac in self.app_configs if ac.options.mount_point}
for s in navbar_entries:
entry = make_entry(s, tools_by_mount.get(s.mount_point))
if s.children:
entry['children'] = [make_entry(child, tools_by_mount.get(child.mount_point))
for child in s.children]
children.append(entry)
response = dict(grouping_threshold=grouping_threshold, menu=children)
if admin_options:
_href = '{}admin/install_tool?tool_name={}'
response['installable_tools'] = [dict(text=t['tool_label'],
href=_href.format(self.url(), t['name']),
tooltip=t['description'])
for t in ProjectAdminRestController().installable_tools()['tools']]
return response
示例6: test_check_permissions
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPError [as 别名]
def test_check_permissions(self, mock_get_user_id,
mock_is_authenticated,
mock_get_test_result,
mock_get_test_result_meta_key,
mock_pecan_abort,
mock_foundation_check):
@api_utils.check_permissions(level=const.ROLE_USER)
class ControllerWithPermissions(rest.RestController):
def get(self, test_id):
return test_id
@api_utils.check_permissions(level=const.ROLE_OWNER)
def delete(self, test_id):
return test_id
@api_utils.check_permissions(level='fake_role')
def post(self, test_id):
return test_id
fake_controller = ControllerWithPermissions()
public_test = 'fake_public_test'
private_test = 'fake_test'
mock_get_user_id.return_value = 'fake_openid'
mock_get_test_result.return_value = {}
mock_get_test_result_meta_key.side_effect = lambda *args: {
(public_test, const.USER): None,
(private_test, const.USER): 'fake_openid',
(private_test, const.SHARED_TEST_RUN): None,
}.get(args)
mock_is_authenticated.return_value = True
mock_foundation_check.return_value = False
self.assertEqual(public_test, fake_controller.get(public_test))
self.assertRaises(exc.HTTPError, fake_controller.delete, public_test)
self.assertEqual(private_test, fake_controller.get(private_test))
self.assertEqual(private_test, fake_controller.delete(private_test))
mock_is_authenticated.return_value = False
self.assertEqual(public_test, fake_controller.get(public_test))
self.assertRaises(exc.HTTPError, fake_controller.delete, public_test)
self.assertRaises(exc.HTTPError, fake_controller.get, private_test)
self.assertRaises(exc.HTTPError, fake_controller.delete, private_test)
self.assertRaises(ValueError, fake_controller.post, public_test)