当前位置: 首页>>代码示例>>Python>>正文


Python web.HTTPError方法代码示例

本文整理汇总了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 
开发者ID:kippandrew,项目名称:tattle,代码行数:39,代码来源:api.py

示例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) 
开发者ID:mosquito,项目名称:aiohttp-xmlrpc,代码行数:12,代码来源:handler.py

示例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) 
开发者ID:RedHatInsights,项目名称:vmaas,代码行数:40,代码来源:app.py

示例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() 
开发者ID:NeKitDS,项目名称:gd.py,代码行数:7,代码来源:server.py


注:本文中的aiohttp.web.HTTPError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。