Python aiohttp.web.HTTPError() Examples
The following are 4
code examples of aiohttp.web.HTTPError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
aiohttp.web
, or try the search function
.
Example #1
Source File: api.py From tattle with Mozilla Public License 2.0 | 5 votes |
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
Example #2
Source File: handler.py From aiohttp-xmlrpc with MIT License | 5 votes |
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)
Example #3
Source File: app.py From vmaas with GNU General Public License v2.0 | 5 votes |
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)
Example #4
Source File: server.py From gd.py with MIT License | 5 votes |
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()