# Imports
from django.conf import settings
from django.utils.timezone import now
from django.urls import reverse, NoReverseMatch
import os
from superdjango.utils import append_file, write_file
from uuid import uuid4
from .base import ErrorHandler
# Exports
__all__ = (
"CSV",
)
# Classes
[docs]class CSV(ErrorHandler):
"""Write errors to a CSV file."""
required_settings = [
"SUPERDJANGO_ERROR_CAPTURE_OUTPUT_PATH",
]
# def handle(self, request, exception, traceback):
[docs] def handle(self, report):
"""Write meta data to a CSV file and traceback to text file."""
base_path = settings.SUPERDJANGO_ERROR_CAPTURE_OUTPUT_PATH
file_path = os.path.join(base_path, "current.csv")
if not os.path.exists(file_path):
write_file(file_path, "id,date,error,path,line\n")
current_dt = now()
error_id = uuid4()
line = None
path = None
if 'lastframe' in self.context:
line = self.context['lastframe']['lineno']
path = self.context['lastframe']['filename']
content = [
str(error_id),
str(current_dt),
report.exc_type.__name__,
path,
str(line),
]
append_file(file_path, ",".join(content) + "\n")
detail_path = os.path.join(base_path, "tracebacks", "%s.txt" % error_id)
write_file(detail_path, report.get_traceback_text(), make_directories=True)
try:
url = reverse("captured_error_detail", kwargs={'uuid': error_id})
return {'issue_url': url}
except NoReverseMatch:
pass
return {
'identifier': error_id,
}