本文整理汇总了Python中aiohttp.web.HTTPError方法的典型用法代码示例。如果您正苦于以下问题:Python web.HTTPError方法的具体用法?Python web.HTTPError怎么用?Python web.HTTPError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.web
的用法示例。
在下文中一共展示了web.HTTPError方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: error_middleware
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import HTTPError [as 别名]
def error_middleware():
# noinspection PyUnusedLocal
@asyncio.coroutine
def _middleware(app, handler):
def _write_exception_json(status_code=500, exc_info=None):
if exc_info is None:
exc_info = sys.exc_info()
# exception = exc_info[2]
error = {
'error': "Internal Server Error",
'traceback': [t for t in traceback.format_exception(*exc_info)]
}
return web.Response(status=status_code,
body=json.to_json(error).encode('utf-8'),
content_type='application/json')
def _write_error_json(status_code, message=None):
return web.Response(status=status_code,
body=json.to_json({'error': message}).encode('utf-8'),
content_type='application/json')
@asyncio.coroutine
def _middleware_handler(request):
try:
response = yield from handler(request)
return response
except APIError as ex:
return _write_error_json(ex.status_code, ex.message or ex.reason)
except web.HTTPError as ex:
return _write_error_json(ex.status_code, ex.reason)
except Exception as ex:
return _write_exception_json()
return _middleware_handler
return _middleware
示例2: post
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import HTTPError [as 别名]
def post(self, *args, **kwargs):
try:
xml_response = await self._handle()
except HTTPError:
raise
except Exception as e:
xml_response = self._format_error(e)
log.exception(e)
return self._make_response(xml_response)
示例3: handle_request
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import HTTPError [as 别名]
def handle_request(cls, api_endpoint, api_version, param_name=None, param=None, **kwargs):
"""Takes care of validation of input and execution of request."""
# If refreshing cache, return 503 so apps can detect this state
if cls.refreshing:
return web.json_response("Data refresh in progress, please try again later.", status=503)
request = kwargs.get('request', None)
if request is None:
raise ValueError('request not provided')
data = None
try:
if request.method == 'POST':
data = await cls.get_post_data(request)
else:
data = {param_name: [param]}
res = api_endpoint.process_list(api_version, data)
code = 200
except web.HTTPError as exc:
# We cant return the e as response, this is being deprecated in aiohttp
response = {"detail": exc.reason, "status": exc.status_code}
return web.json_response(response, status=exc.status_code)
except ValidationError as valid_err:
if valid_err.absolute_path:
res = '%s : %s' % (valid_err.absolute_path.pop(), valid_err.message)
else:
res = '%s' % valid_err.message
code = 400
except (ValueError, sre_constants.error) as ex:
res = repr(ex)
code = 400
except Exception as err: # pylint: disable=broad-except
err_id = err.__hash__()
res = 'Internal server error <%s>: please include this error id in bug report.' % err_id
code = 500
LOGGER.exception(res)
LOGGER.error("Input data for <%s>: %s", err_id, data)
return web.json_response(res, status=code)
示例4: error_middleware
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import HTTPError [as 别名]
def error_middleware(request: web.Request, handler: Function) -> web.Response:
try:
return await handler(request)
except web.HTTPError as error:
return Error(error.status, "{error.status}: {error.reason}").set_error(error).into_resp()