From f8d6fed06e68cd909174d5bf595d62266d38033b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 18 Mar 2024 23:09:23 +1100 Subject: [PATCH] Error handling fix (#6741) (#6744) * Handle case where error message is *not* a dict * Encode as list (cherry picked from commit ca8f0cee9f5009e0f28a4b9a46924cdbdcac65a0) Co-authored-by: Oliver --- InvenTree/InvenTree/serializers.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/serializers.py b/InvenTree/InvenTree/serializers.py index 4745647ea5..3b4978d15d 100644 --- a/InvenTree/InvenTree/serializers.py +++ b/InvenTree/InvenTree/serializers.py @@ -352,7 +352,12 @@ class InvenTreeModelSerializer(serializers.ModelSerializer): try: instance.full_clean() except (ValidationError, DjangoValidationError) as exc: - data = exc.message_dict + if hasattr(exc, 'message_dict'): + data = exc.message_dict + elif hasattr(exc, 'message'): + data = {'non_field_errors': [str(exc.message)]} + else: + data = {'non_field_errors': [str(exc)]} # Change '__all__' key (django style) to 'non_field_errors' (DRF style) if '__all__' in data: