本文整理汇总了Python中webob.exc函数的典型用法代码示例。如果您正苦于以下问题:Python exc函数的具体用法?Python exc怎么用?Python exc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了exc函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
def __call__(self, environ, start_response):
try:
request = self.get_request(environ)
path, realpath, statinfo = self.check_path(request.path)
app = self.serve_file(path, realpath, statinfo, request)
if app:
return app(environ, start_response)
exc = get_http_exception(httplib.NOT_FOUND)
return exc(environ, start_response)
except webob.exc.HTTPException, exc:
return exc(environ, start_response)
示例2: test_413_fault_json
def test_413_fault_json(self):
# Test fault serialized to JSON via file-extension and/or header.
requests = [
webob.Request.blank('/.json'),
webob.Request.blank('/', headers={"Accept": "application/json"}),
]
for request in requests:
exc = webob.exc.HTTPRequestEntityTooLarge
# NOTE(aloga): we intentionally pass an integer for the
# 'Retry-After' header. It should be then converted to a str
fault = wsgi.Fault(exc(explanation='sorry',
headers={'Retry-After': 4}))
response = request.get_response(fault)
expected = {
"overLimit": {
"message": "sorry",
"code": 413,
"retryAfter": "4",
},
}
actual = jsonutils.loads(response.body)
self.assertEqual(response.content_type, "application/json")
self.assertEqual(expected, actual)
示例3: __call__
def __call__(self, environ, start_response):
"""
Call the application and catch exceptions.
"""
app_iter = None
# Just call the application and send the output back
# unchanged and catch relevant HTTP exceptions
try:
app_iter = self.app(environ, start_response)
except (HTTPClientError, HTTPRedirection), exc:
app_iter = exc(environ, start_response)
示例4: test_HTTPException
def test_HTTPException():
import warnings
_called = []
_result = object()
def _response(environ, start_response):
_called.append((environ, start_response))
return _result
environ = {}
start_response = object()
exc = HTTPException('testing', _response)
ok_(exc.wsgi_response is _response)
result = exc(environ, start_response)
ok_(result is result)
assert_equal(_called, [(environ, start_response)])
示例5: test_413_fault_json
def test_413_fault_json(self):
"""Test fault serialized to JSON via file-extension and/or header."""
requests = [webob.Request.blank("/.json"), webob.Request.blank("/", headers={"Accept": "application/json"})]
for request in requests:
exc = webob.exc.HTTPRequestEntityTooLarge
fault = wsgi.Fault(exc(explanation="sorry", headers={"Retry-After": 4}))
response = request.get_response(fault)
expected = {"overLimit": {"message": "sorry", "code": 413, "retryAfter": 4}}
actual = jsonutils.loads(response.body)
self.assertEqual(response.content_type, "application/json")
self.assertEqual(expected, actual)
示例6: exception_from_response
def exception_from_response(response):
"""Convert an OpenStack V2 Fault into a webob exception.
Since we are calling the OpenStack API we should process the Faults
produced by them. Extract the Fault information according to [1] and
convert it back to a webob exception.
[1] http://docs.openstack.org/developer/nova/v2/faults.html
:param response: a webob.Response containing an exception
:returns: a webob.exc.exception object
"""
exceptions = {
400: webob.exc.HTTPBadRequest,
401: webob.exc.HTTPUnauthorized,
403: webob.exc.HTTPForbidden,
404: webob.exc.HTTPNotFound,
405: webob.exc.HTTPMethodNotAllowed,
406: webob.exc.HTTPNotAcceptable,
409: webob.exc.HTTPConflict,
413: webob.exc.HTTPRequestEntityTooLarge,
415: webob.exc.HTTPUnsupportedMediaType,
429: webob.exc.HTTPTooManyRequests,
501: webob.exc.HTTPNotImplemented,
503: webob.exc.HTTPServiceUnavailable,
}
message = ('Unexpected API Error. Please report this at '
'http://bugs.launchpad.net/ooi/ and attach the ooi '
'API log if possible.')
code = response.status_int
exc = exceptions.get(code, webob.exc.HTTPInternalServerError)
if code in exceptions:
try:
message = response.json_body.popitem()[1].get("message")
except Exception:
LOG.exception("Unknown error happenened processing response %s"
% response)
exc = webob.exc.HTTPInternalServerError
else:
LOG.error("Nova returned an internal server error %s"
% response)
return exc(explanation=message)
示例7: __call__
def __call__(self, environ, start_response):
# Note that catching the webob exception is for Python 2.4 support.
# In the brave new world of new-style exceptions (derived from object)
# multiple inheritance works like you'd expect: the NotAuthenticatedError
# is caught because it descends from the past and webob exceptions.
# In the old world (2.4-), the webob exception needs to be in the catch list
environ['paste.httpexceptions'] = self
environ.setdefault('paste.expected_exceptions',
[]).extend([paste.httpexceptions.HTTPException,
webob.exc.HTTPException])
try:
return self.application(environ, start_response)
except (paste.httpexceptions.HTTPException,
webob.exc.HTTPException), exc:
return exc(environ, start_response)
示例8: test_HTTPException
def test_HTTPException():
import warnings
_called = []
_result = object()
def _response(environ, start_response):
_called.append((environ, start_response))
return _result
environ = {}
start_response = object()
exc = HTTPException('testing', _response)
ok_(exc.wsgi_response is _response)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
assert(exc.exception is exc)
assert(len(w) == 1)
result = exc(environ, start_response)
ok_(result is result)
assert_equal(_called, [(environ, start_response)])
示例9: test_413_fault_xml
def test_413_fault_xml(self):
requests = [
webob.Request.blank('/.xml'),
webob.Request.blank('/', headers={"Accept": "application/xml"}),
]
for request in requests:
exc = webob.exc.HTTPRequestEntityTooLarge
fault = faults.Fault(exc(explanation='sorry',
headers={'Retry-After': 4}))
response = request.get_response(fault)
expected = self._prepare_xml("""
<overLimit code="413" xmlns="%s">
<message>sorry</message>
<retryAfter>4</retryAfter>
</overLimit>
""" % common.XML_NS_V10)
actual = self._prepare_xml(response.body)
self.assertEqual(expected, actual)
self.assertEqual(response.content_type, "application/xml")
self.assertEqual(response.headers['Retry-After'], 4)
示例10: manage_http_exception
def manage_http_exception(code, message):
exc = default_exceptions.get(code, webob.exc.HTTPInternalServerError)
return exc("%s" % message)