def log_exception(request=None, exc_type=None, exc_val=None, tb=None): django_request = None if isinstance(request, http.HttpRequest): django_request = request if tb is None: exc_type, exc_val, tb = sys.exc_info() reporter = debug.ExceptionReporter(django_request, exc_type, exc_val, Traceback(tb) or None) tb_desc = traceback.extract_tb(tb) tb_file, tb_line_num, tb_function, tb_text = tb_desc[-1] data = {} if request is not None: data = request_data(request) data.update({ 'exception_type' : exc_type.__name__, 'exception_str' : str(exc_val), 'source_file' : utils.TruncateBeginning(tb_file), 'source_line_num': tb_line_num, 'source_function': tb_function, 'source_text' : tb_text, }) kwargs = utils.clean_data_for_insert(data, models.ServerError) models.ServerError.objects.create( issue = '', technical_response = get_technical_response(reporter), **kwargs )
def technical_500_response(request, exc_type, exc_value, tb, status_code=500): """ Create a technical server error response. The last three arguments are the values returned from sys.exc_info() and friends. """ reporter = debug.ExceptionReporter(request, exc_type, exc_value, tb) html = reporter.get_traceback_html() return debug.HttpResponse(html, status=status_code, content_type='text/html')
def exception_handler(exc, context): """ Returns the response that should be used for any given exception. By default we handle the REST framework `APIException`, and also Django's built-in `Http404` and `PermissionDenied` exceptions. Any unhandled exceptions may return `None`, which will cause a 500 error to be raised. """ if isinstance(exc, exceptions.APIException): headers = {} if getattr(exc, 'auth_header', None): headers['WWW-Authenticate'] = exc.auth_header if getattr(exc, 'wait', None): headers['Retry-After'] = '%d' % exc.wait if isinstance(exc.detail, (list, dict)): data = exc.detail else: data = {'detail': exc.detail} set_rollback() return Response(data, status=exc.status_code, headers=headers) elif isinstance(exc, Http404): msg = _('Not found.') data = {'detail': six.text_type(msg)} set_rollback() return Response(data, status=status.HTTP_404_NOT_FOUND) elif isinstance(exc, PermissionDenied): msg = _('Permission denied.') data = {'detail': six.text_type(msg)} set_rollback() return Response(data, status=status.HTTP_403_FORBIDDEN) # throw django's error page if debug is True if settings.DEBUG: exception_reporter = debug.ExceptionReporter(context.get('request'), *sys.exc_info()) return HttpResponse(exception_reporter.get_traceback_html(), status=500) return None
def process_exception(self, request, exc): if isinstance(exc, exception.RedirectException): url = exc.build_url(request) return HttpResponseRedirect(url) if isinstance(exc, exception.Error): logging.warning("RedirectError: %s", traceback.format_exc()) return util.RedirectError(exc.message) if not isinstance(exc, Http404): logging.error("5xx: %s", traceback.format_exc()) if settings.DEBUG and not isinstance(exc, Http404): # fake out the technical_500_response because app engine # is annoying when it tries to rollback our stuff on 500 import sys from django.views import debug exc_info = sys.exc_info() reporter = debug.ExceptionReporter(request, *exc_info) html = reporter.get_traceback_html() return HttpResponse(html, mimetype='text/html') return None
def emit(self, record): try: request = record.request except Exception: request = None # Since we add a nicely formatted traceback on our own, create a copy # of the log record without the exception data. no_exc_record = copy(record) no_exc_record.exc_info = None no_exc_record.exc_text = None if record.exc_info: exc_info = record.exc_info else: exc_info = (None, record.getMessage(), None) reporter = debug.ExceptionReporter(request, is_email=True, *exc_info) message = "%s\n\n%s" % (self.format(no_exc_record), reporter.get_traceback_text()) self.stream.write(message)
def process_exception(self, request, exception): request.uncaught_exception = exception request.uncaught_exception_format = traceback.format_exc() exc_type, exc_value, tb = sys.exc_info() reporter = debug.ExceptionReporter(request, exc_type, exc_value, tb) request.django_error_page = reporter.get_traceback_html()