def write_json(self, chunk, code=None, headers=None): """A convenient method that binds `chunk`, `code`, `headers` together chunk could be any type of (str, dict, list) """ assert chunk is not None, 'None cound not be written in write_json' self.set_header("Content-Type", "application/json; charset=UTF-8") if isinstance(chunk, dict) or isinstance(chunk, list): chunk = self.json_encode(chunk) # convert chunk to utf8 before `RequestHandler.write()` # so that if any error occurs, we can catch and log it try: chunk = utf8(chunk) except Exception: logging.error('chunk encoding error, repr: %s' % repr(chunk)) raise_exc_info(sys.exc_info()) self.write(chunk) if code: self.set_status(code) if headers: for k, v in headers.iteritems(): self.set_header(k, v)
def write_json(self, chunk, code=None, headers=None): """A convenient method that binds `chunk`, `code`, `headers` together chunk could be any type of (str, dict, list) """ assert chunk is not None, 'None cound not be written in write_json' self.set_header("Content-Type", "application/json; charset=UTF-8") if isinstance(chunk, dict) or isinstance(chunk, list): chunk = self.json_encode(chunk) # convert chunk to utf8 before `RequestHandler.write()` # so that if any error occurs, we can catch and log it try: chunk = utf8(chunk) except Exception: app_log.error('chunk encoding error, repr: %s' % repr(chunk)) raise_exc_info(sys.exc_info()) self.write(chunk) if code: self.set_status(code) if headers: for k, v in headers.items(): self.set_header(k, v)
def get_handler_exc(self): if self._handler_exc_info: raise_exc_info(self._handler_exc_info)
def __rethrow(self): if self.__failure is not None: failure = self.__failure self.__failure = None raise_exc_info(failure)