async def handler(self, request): """Main handler function for aiohttp.""" request._view_error = False await notify(BeforeRenderViewEvent(request, self.view)) request.record("viewrender") if app_settings["check_writable_request"](request): try: # We try to avoid collisions on the same instance of # guillotina view_result = await self.view() await commit(warn=False) except (ConflictError, TIDConflictError): await abort() # bubble this error up raise except (response.Response, aiohttp.web_exceptions.HTTPException) as exc: await abort() view_result = exc request._view_error = True except Exception as e: await abort() view_result = generate_error_response(e, request, "ServiceError") request._view_error = True else: try: view_result = await self.view() except (response.Response, aiohttp.web_exceptions.HTTPException) as exc: view_result = exc request._view_error = True except Exception as e: request._view_error = True view_result = generate_error_response(e, request, "ViewError") finally: await abort() request.record("viewrendered") if IAioHTTPResponse.providedBy(view_result): resp = view_result else: resp = await apply_rendering(self.view, self.request, view_result) request.record("renderer") resp = await _apply_cors(request, resp) if not request._view_error: request.execute_futures() else: request.execute_futures("failure") self.debug(request, resp) request.record("finish") _clean_request(request, resp) del self.view del self.resource request.clear_futures() return resp
async def handler(self, request): """Main handler function""" request._view_error = False await notify(BeforeRenderViewEvent(request, self.view)) request.record("viewrender") try: # We try to avoid collisions on the same instance of # guillotina view_result = await self.view() if app_settings["check_writable_request"](request): await commit(warn=False) else: await abort() except (ConflictError, TIDConflictError): await abort() # bubble this error up raise except response.Response as exc: await abort() view_result = exc request._view_error = True except Exception: await abort() raise request.record("viewrendered") resp = view_result if resp is None: resp = Response(status=200) if not IResponse.providedBy(resp) or not resp.prepared: resp = await apply_rendering(self.view, self.request, resp) request.record("renderer") resp = await apply_cors(request, resp) if not request._view_error: task = request.execute_futures() else: task = request.execute_futures("failure") if task is not None: await self.wait(request, resp, task) self.debug(request, resp) request.record("finish") del self.view del self.resource request.clear_futures() return resp