From 1020ed30099cbd36bd8e8b7f491ef502e5c8d36c Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 10 Aug 2026 23:23:07 +1000 Subject: [PATCH] Fix for API exception_handler (#12604) - Only log unhandled exceptions to sentry.io - Check for valid handling first - Report second --- src/backend/InvenTree/InvenTree/exceptions.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/exceptions.py b/src/backend/InvenTree/InvenTree/exceptions.py index 76aaf53868..633612922d 100644 --- a/src/backend/InvenTree/InvenTree/exceptions.py +++ b/src/backend/InvenTree/InvenTree/exceptions.py @@ -115,13 +115,6 @@ def exception_handler(exc, context): response = None - # Pass exception to sentry.io handler - try: - InvenTree.sentry.report_exception(exc) - except Exception: - # If sentry.io fails, we don't want to crash the server! - pass - # The Django app registry can be transiently un-ready while the plugin # registry is reloading apps (see plugin.registry.PluginsRegistry._reload_apps). # Any request handled by another thread/worker during that window can trip @@ -141,9 +134,17 @@ def exception_handler(exc, context): return response # Catch any django validation error, and re-throw a DRF validation error - if isinstance(exc, DjangoValidationError): + elif isinstance(exc, DjangoValidationError): exc = DRFValidationError(detail=serializers.as_serializer_error(exc)) + else: + # Pass any other (unhandled) exception to sentry.io handler + try: + InvenTree.sentry.report_exception(exc) + except Exception: + # If sentry.io fails, we don't want to crash the server! + pass + # Default to the built-in DRF exception handler response = drfviews.exception_handler(exc, context)