def __await__(self): try: self.response = web.Response() yield from HandlerBase.prepare(self).__await__() yield from super(Handler, self).__await__() except asyncio.CancelledError: raise except error.UserFacingError as e: self.response.set_status(e.http_status, None) if isinstance(e, error.PermissionError): e.args = (self.translate(e.args[0]), *e.args[1:]) if self.prefer_json: self.response.content_type = 'application/json' message = self.translate(e.message).format(*e.args) self.response.text = json.encode({'error': {**e.to_dict(), 'message': message}}) else: self.render(e.template_name, error=e, page_name='error', page_title=self.translate('error'), path_components=self.build_path((self.translate('error'), None))) uid = self.user['_id'] if hasattr(self, 'user') else None _logger.warning('User facing error by %s %s %s: %s', self.url, self.remote_ip, uid, repr(e)) except Exception as e: uid = self.user['_id'] if hasattr(self, 'user') else None _logger.error('System error by %s %s %s: %s', self.url, self.remote_ip, uid, repr(e)) raise return self.response
def __iter__(self): try: self.response = web.Response() yield from HandlerBase.prepare(self) yield from super(Handler, self).__iter__() except asyncio.CancelledError: raise except error.UserFacingError as e: self.response.set_status(e.http_status, None) if self.prefer_json: self.response.content_type = 'application/json' message = self.translate(e.message).format(*e.args) self.response.text = json.encode( {'error': { **e.to_dict(), 'message': message }}) else: self.render(e.template_name, error=e, page_name='error', page_title=self.translate('error'), path_components=self.build_path( (self.translate('error'), None))) uid = self.user['_id'] if hasattr(self, 'user') else None _logger.warning('User facing error by %s %s %s: %s', self.url, self.remote_ip, uid, repr(e)) except Exception as e: uid = self.user['_id'] if hasattr(self, 'user') else None _logger.error('System error by %s %s %s: %s', self.url, self.remote_ip, uid, repr(e)) raise return self.response
def __iter__(self): try: self.response = web.Response() yield from HandlerBase.prepare(self) yield from super(Handler, self).__iter__() except error.UserFacingError as e: _logger.warning("User facing error: %s", repr(e)) self.response.set_status(e.http_status, None) if self.prefer_json: self.response.content_type = 'application/json' message = self.translate(e.message).format(*e.args) self.response.text = json.encode( {'error': { **e.to_dict(), 'message': message }}) else: self.render(e.template_name, error=e, page_name='error', page_title=self.translate('error'), path_components=self.build_path( (self.translate('error'), None))) except Exception as e: _logger.error( "Unexpected exception occurred when handling %s (IP = %s, UID = %d): %s", self.url, self.remote_ip, (self.user and self.user['_id']) or None, repr(e)) raise return self.response
def __iter__(self): try: self.response = web.Response() yield from HandlerBase.prepare(self) yield from super(Handler, self).__iter__() except error.UserFacingError as e: _logger.warning("User facing error: %s", repr(e)) self.response.set_status(e.http_status, None) if self.prefer_json: self.response.content_type = 'application/json' self.response.text = json.encode({'error': e.to_dict()}) else: self.render(e.template_name, error=e, page_name='error', page_title=self.translate('error'), path_components=self.build_path((self.translate('error'), None))) return self.response
def send(self, **kwargs): super(Connection, self).send(json.encode(kwargs))
def json(self, obj): self.response.content_type = 'application/json' self.response.headers.add('Cache-Control', 'no-store, no-cache, must-revalidate') self.response.headers.add('Pragma', 'no-cache') self.response.text = json.encode(obj)
def json_or_redirect(self, redirect_url, **kwargs): if self.prefer_json: self.response.content_type = 'application/json' self.response.text = json.encode(kwargs) else: self.redirect(redirect_url)
def json(self, obj): self.response.content_type = 'application/json' self.response.text = json.encode(obj)