def handle_exception(self, exception, debug): """ Override handle_exception() to provide JSON encoded debug messages. Also logs any errors to a sentry server if configured to do so. """ # If the exception is a HTTPException, use its error code. # Otherwise use a generic 500 error code. if isinstance(exception, webapp2.HTTPException): self.response.set_status(exception.code) status_code = exception.code else: self.response.set_status(500) status_code = 500 # collect our error data exc_info = sys.exc_info() # Log the exception logging.exception(exception) # log error to sentry sentry.capture_exception(self.request, exc_info) # Set a custom message. if settings.DEBUG: # format exception as JSON object exc_info_str = { 'exc_type': str(exc_info[0]), 'exc_value': str(exc_info[1]), } response = {'exc_info': exc_info_str} self.json_response(response) else: # return simple error msg if 500 if status_code == 500: self.json_response({'error': 'A server error has occurred'}) # otherwise return the error message's value else: self.json_response({'error': exc_info[1]})
def handle_exception(self, exception, debug): """ Override handle_exception() to provide nicely rendered debug pages when in DEBUG mode. Also logs any errors to a sentry server if configured to do so. """ # collect our error data exc_info = sys.exc_info() # Log the exception logging.exception(exception) # log error to sentry sentry.capture_exception(self.request, exc_info) # If the exception is a HTTPException, use its error code. # Otherwise use a generic 500 error code. if isinstance(exception, webapp2.HTTPException): self.response.set_status(exception.code) status_code = exception.code else: self.response.set_status(500) status_code = 500 # Set a custom message. if settings.DEBUG: # render debug error page error.render_html(self) else: # render 404 page if not found if status_code == 404: self.template_response('404.html') # otherwise render the standard 500 page else: self.template_response('500.html')