Skip to content

Commit

Permalink
SSO Error Improvememts (#6246) (#6304)
Browse files Browse the repository at this point in the history
* Improve exception handling

* Extract extra information from SSO auth failure

* Revert order of ignore check
  • Loading branch information
SchrodingersGat authored Jan 21, 2024
1 parent 2c6bc88 commit e81349e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
36 changes: 26 additions & 10 deletions InvenTree/InvenTree/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from django.conf import settings
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db.utils import IntegrityError, OperationalError
from django.utils.translation import gettext_lazy as _

import rest_framework.views as drfviews
Expand All @@ -23,33 +22,50 @@
logger = logging.getLogger('inventree')


def log_error(path):
def log_error(path, error_name=None, error_info=None, error_data=None):
"""Log an error to the database.
- Uses python exception handling to extract error details
Arguments:
path: The 'path' (most likely a URL) associated with this error (optional)
kwargs:
error_name: The name of the error (optional, overrides 'kind')
error_info: The error information (optional, overrides 'info')
error_data: The error data (optional, overrides 'data')
"""
kind, info, data = sys.exc_info()

# Check if the error is on the ignore list
if kind in settings.IGNORED_ERRORS:
return

if error_name:
kind = error_name
else:
kind = getattr(kind, '__name__', 'Unknown Error')

if error_info:
info = error_info

if error_data:
data = error_data
else:
data = '\n'.join(traceback.format_exception(kind, info, data))

# Log error to stderr
logger.error(info)

# Ensure the error information does not exceed field size limits
path = path[:200]
kind = kind[:128]

try:
Error.objects.create(
kind=kind.__name__,
info=info,
data='\n'.join(traceback.format_exception(kind, info, data)),
path=path,
)
except (OperationalError, IntegrityError):
Error.objects.create(kind=kind, info=info or '', data=data or '', path=path)
except Exception:
# Not much we can do if logging the error throws a db exception
pass
logger.exception('Failed to log exception to database')


def exception_handler(exc, context):
Expand Down
9 changes: 8 additions & 1 deletion InvenTree/InvenTree/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,16 @@ def login(self, request, user):

def authentication_error(self, request, provider_id, error=None, exception=None, extra_context=None):
"""Callback method for authentication errors."""
if not error:
error = request.GET.get('error', None)

if not exception:
exception = request.GET.get('error_description', None)

path = request.path or 'sso'

# Log the error to the database
log_error(request.path if request else 'sso')
log_error(path, error_name=error, error_data=exception)
logger.error("SSO error for provider '%s' - check admin error log", provider_id)


Expand Down

0 comments on commit e81349e

Please sign in to comment.