def test_should_render_json(self): """Ensure it determines render_json correctly.""" self.assertTrue( helpers.should_render_json('adsasd;application/json;sdf', '')) self.assertTrue(helpers.should_render_json('', 'application/json')) self.assertFalse( helpers.should_render_json('text/plain;0.8', 'text/html'))
def handle_exception_exception(self): """Catch exception in handle_exception and format it properly.""" exception = sys.exc_info()[1] values = {'message': exception.message, 'traceDump': traceback.format_exc()} logging.exception(exception) if helpers.should_render_json( self.request.headers.get('accept', ''), self.response.headers.get('Content-Type')): self.render_json(values, 500) else: self.render('error.html', values, 500)
def handle_exception(self, exception, _): """Catch exception and format it properly.""" try: status = 500 values = { 'message': str(exception), 'email': helpers.get_user_email(), 'traceDump': traceback.format_exc(), 'status': status, 'type': exception.__class__.__name__ } if isinstance(exception, helpers.EarlyExitException): status = exception.status values = exception.to_dict() values['params'] = self.request.params.dict_of_lists() # 4XX is not our fault. Therefore, we hide the trace dump and log on # the INFO level. if 400 <= status <= 499: logging.info(json.dumps(values, cls=JsonEncoder)) del values['traceDump'] else: # Other error codes should be logged with the EXCEPTION level. logging.exception(exception) if helpers.should_render_json( self.request.headers.get('accept', ''), self.response.headers.get('Content-Type')): self.render_json(values, status) else: if status in (403, 401): self.render_forbidden(str(exception)) else: self.render('error.html', values, status) except Exception: self.handle_exception_exception()