Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.conf import settings
4from django.utils.timezone import now
5from django.urls import reverse, NoReverseMatch
6import os
7from superdjango.utils import append_file, write_file
8from uuid import uuid4
9from .base import ErrorHandler
11# Exports
13__all__ = (
14 "CSV",
15)
17# Classes
20class CSV(ErrorHandler):
21 """Write errors to a CSV file."""
23 required_settings = [
24 "SUPERDJANGO_ERROR_CAPTURE_OUTPUT_PATH",
25 ]
27 # def handle(self, request, exception, traceback):
28 def handle(self, report):
29 """Write meta data to a CSV file and traceback to text file."""
30 base_path = settings.SUPERDJANGO_ERROR_CAPTURE_OUTPUT_PATH
31 file_path = os.path.join(base_path, "current.csv")
33 if not os.path.exists(file_path):
34 write_file(file_path, "id,date,error,path,line\n")
36 current_dt = now()
37 error_id = uuid4()
39 line = None
40 path = None
41 if 'lastframe' in self.context:
42 line = self.context['lastframe']['lineno']
43 path = self.context['lastframe']['filename']
45 content = [
46 str(error_id),
47 str(current_dt),
48 report.exc_type.__name__,
49 path,
50 str(line),
51 ]
52 append_file(file_path, ",".join(content) + "\n")
54 detail_path = os.path.join(base_path, "tracebacks", "%s.txt" % error_id)
55 write_file(detail_path, report.get_traceback_text(), make_directories=True)
57 try:
58 url = reverse("captured_error_detail", kwargs={'uuid': error_id})
59 return {'issue_url': url}
60 except NoReverseMatch:
61 pass
63 return {
64 'identifier': error_id,
65 }