async def __call__(self, scope, receive, send): """ This method always returns a response object or raises a Exception for unhandled errors """ request_settings = { k: v for k, v in self.asgi_app.server_settings.items() if k in ("client_max_size",) } request = Request.factory(scope, send, receive, **request_settings) task_vars.request.set(request) try: return await self.request_handler(request) except Response as exc: return exc except Exception as exc: # Try to render exception using IErrorResponseException eid = uuid.uuid4().hex view_result = query_adapter( exc, IErrorResponseException, kwargs={"error": "ServiceError", "eid": eid} ) if view_result is not None: return await apply_rendering(View(None, request), request, view_result) # Raise unhandled exceptions to ErrorMiddleware raise
async def handler(self, request): """Main handler function""" resp = self.resp request.record("finish") self.debug(request, resp) if not IResponse.providedBy(resp) or not resp.prepared: resp = await apply_rendering(View(None, request), request, resp) resp = await apply_cors(request, resp) return resp
async def handler(self, request): """Main handler function for aiohttp.""" request.record('finish') self.debug(request, self.resp) if IAioHTTPResponse.providedBy(self.resp): return self.resp else: return await apply_rendering( View(None, request), request, self.resp)
async def __call__(self, scope, receive, send): headers_sent = False async def _send(msg): nonlocal headers_sent headers_sent = True await send(msg) try: resp = await self.next_app(scope, receive, _send) except Exception as exc: request = task_vars.request.get() view_result = generate_error_response(exc, request=request) resp = await apply_rendering(View(None, request), request, view_result) if headers_sent: # Too late to send status 500, headers already sent raise return resp