本文整理汇总了Python中webob.exc.HTTPException方法的典型用法代码示例。如果您正苦于以下问题:Python exc.HTTPException方法的具体用法?Python exc.HTTPException怎么用?Python exc.HTTPException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类webob.exc
的用法示例。
在下文中一共展示了exc.HTTPException方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPException [as 别名]
def __call__(self, req):
action = req.urlvars.get("action")
try:
try:
method = getattr(self, "json_" + action)
except AttributeError:
raise exc.HTTPNotFound("No action '%s'" % action)
resp = method(req)
if isinstance(resp, (dict, list)):
try:
resp = json.dumps(resp, sort_keys=True)
except (TypeError, ValueError, IndexError, AttributeError) as json_exc:
raise exc.HTTPInternalServerError("JSON serialization error (%s)" % json_exc)
if isinstance(resp, basestring):
resp = Response(body=resp, content_type="application/json")
except exc.HTTPException as http_exc:
resp = http_exc
return resp
示例2: __call__
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPException [as 别名]
def __call__(self, req, *args, **kw):
"""Call this as a WSGI application or with a request"""
func = self.func
if func is None:
if args or kw:
raise TypeError(
"Unbound %s can only be called with the function it "
"will wrap" % self.__class__.__name__)
func = req
return self.clone(func)
if isinstance(req, dict):
if len(args) != 1 or kw:
raise TypeError(
"Calling %r as a WSGI app with the wrong signature")
environ = req
start_response = args[0]
req = self.RequestClass(environ)
req.response = req.ResponseClass()
try:
args = self.args
if self.middleware_wraps:
args = (self.middleware_wraps,) + args
resp = self.call_func(req, *args, **self.kwargs)
except HTTPException as exc:
resp = exc
if resp is None:
## FIXME: I'm not sure what this should be?
resp = req.response
if isinstance(resp, text_type):
resp = bytes_(resp, req.charset)
if isinstance(resp, bytes):
body = resp
resp = req.response
resp.write(body)
if resp is not req.response:
resp = req.response.merge_cookies(resp)
return resp(environ, start_response)
else:
if self.middleware_wraps:
args = (self.middleware_wraps,) + args
return self.func(req, *args, **kw)
示例3: __execute_request
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPException [as 别名]
def __execute_request(self, func, args, req, environ):
args = self.__build_args(func, args, req, environ)
try:
result = func(**args)
except exc.HTTPException as e:
result = e
return result
示例4: _init_flow_exceptions
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPException [as 别名]
def _init_flow_exceptions():
"""Internal helper to initialize _flow_exceptions.
This automatically adds webob.exc.HTTPException, if it can be imported.
"""
global _flow_exceptions
_flow_exceptions = ()
add_flow_exception(datastore_errors.Rollback)
try:
from webob import exc
except ImportError:
pass
else:
add_flow_exception(exc.HTTPException)
示例5: convert_exception_to_http_exc
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPException [as 别名]
def convert_exception_to_http_exc(e, faults, language):
serializer = wsgi.JSONDictSerializer()
e = translate(e, language)
body = serializer.serialize(
{'TackerError': get_exception_data(e)})
kwargs = {'body': body, 'content_type': 'application/json'}
if isinstance(e, exc.HTTPException):
# already an HTTP error, just update with content type and body
e.body = body
e.content_type = kwargs['content_type']
return e
if isinstance(e, (exceptions.TackerException, netaddr.AddrFormatError,
oslo_policy.PolicyNotAuthorized)):
for fault in faults:
if isinstance(e, fault):
mapped_exc = faults[fault]
break
else:
mapped_exc = exc.HTTPInternalServerError
return mapped_exc(**kwargs)
if isinstance(e, NotImplementedError):
# NOTE(armando-migliaccio): from a client standpoint
# it makes sense to receive these errors, because
# extensions may or may not be implemented by
# the underlying plugin. So if something goes south,
# because a plugin does not implement a feature,
# returning 500 is definitely confusing.
kwargs['body'] = serializer.serialize(
{'NotImplementedError': get_exception_data(e)})
return exc.HTTPNotImplemented(**kwargs)
# NOTE(jkoelker) Everything else is 500
# Do not expose details of 500 error to clients.
msg = _('Request Failed: internal server error while '
'processing your request.')
msg = translate(msg, language)
kwargs['body'] = serializer.serialize(
{'TackerError': get_exception_data(exc.HTTPInternalServerError(msg))})
return exc.HTTPInternalServerError(**kwargs)
示例6: test_http_exception
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPException [as 别名]
def test_http_exception(self):
req = wsgi_resource.Request({})
language = req.best_match_language()
e = exc.HTTPException
result = common.convert_exception_to_http_exc(e, {}, language)
except_res = {"message": "Request Failed: internal server error "
"while processing your request.",
"type": "HTTPInternalServerError",
"detail": ""}
self.assertEqual(
except_res, jsonutils.loads(result.body)["TackerError"])
self.assertEqual(500, result.code)
示例7: _handleRequest
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPException [as 别名]
def _handleRequest(self, request):
self.request = request
try:
try:
return self._getResponse()
except exc.HTTPException, err:
return err
finally:
self.repServer.reset()
self.repos = None
示例8: handleRequest
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPException [as 别名]
def handleRequest(self, request):
try:
return self._handleRequest(request)
except web_exc.HTTPException, err:
return err
示例9: __call__
# 需要导入模块: from webob import exc [as 别名]
# 或者: from webob.exc import HTTPException [as 别名]
def __call__(self, environ, start_response):
try:
return self.app(environ, start_response)
except Exception as exc:
# get a formatted exception
out = StringIO()
print_exc(file=out)
LOG.exception(exc)
# get formatted WSGI environment
formatted_environ = pformat(environ)
# render our template
result = debug_template.render(
traceback=out.getvalue(),
environment=formatted_environ
)
# construct and return our response
response = Response()
if isinstance(exc, HTTPException):
response.status_int = exc.status
else:
response.status_int = 500
response.unicode_body = result
return response(environ, start_response)