From a3940cfc414a6732e3179d1b7a93cddc6d80fb24 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 14 Jun 2023 13:07:05 +1000 Subject: [PATCH] Improve error logging (#5039) - Truncate path to < 200 chars - Prevents exception when creating new Error object --- InvenTree/InvenTree/middleware.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/middleware.py b/InvenTree/InvenTree/middleware.py index b31f6c96f0..a8a51404d2 100644 --- a/InvenTree/InvenTree/middleware.py +++ b/InvenTree/InvenTree/middleware.py @@ -167,4 +167,31 @@ class InvenTreeExceptionProcessor(ExceptionProcessor): if kind in settings.IGNORED_ERRORS: return - return super().process_exception(request, exception) + import traceback + + from django.views.debug import ExceptionReporter + + from error_report.models import Error + from error_report.settings import ERROR_DETAIL_SETTINGS + + # Error reporting is disabled + if not ERROR_DETAIL_SETTINGS.get('ERROR_DETAIL_ENABLE', True): + return + + path = request.build_absolute_uri() + + # Truncate the path to a reasonable length + # Otherwise we get a database error, + # because the path field is limited to 200 characters + if len(path) > 200: + path = path[:195] + '...' + + error = Error.objects.create( + kind=kind.__name__, + html=ExceptionReporter(request, kind, info, data).get_traceback_html(), + path=path, + info=info, + data='\n'.join(traceback.format_exception(kind, info, data)), + ) + + error.save()