From faed3097e966879fc566808cebda19c9590799bf Mon Sep 17 00:00:00 2001 From: ghazi Date: Sat, 10 Aug 2024 17:06:02 +0100 Subject: [PATCH] stop ignoring exceptions with detail as an empty string when returning api errors fixes #82 --- docs/changelog.md | 2 ++ drf_standardized_errors/formatter.py | 2 +- tests/test_flatten_errors.py | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 0892787..bf7575b 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [UNRELEASED] +### Fixed +- stop ignoring exceptions with detail as an empty string when returning api errors. ## [0.14.0] - 2024-06-19 ### Added diff --git a/drf_standardized_errors/formatter.py b/drf_standardized_errors/formatter.py index ec08352..c13268d 100644 --- a/drf_standardized_errors/formatter.py +++ b/drf_standardized_errors/formatter.py @@ -109,7 +109,7 @@ def flatten_errors( errors = [] while fifo: detail, attr, index = fifo.pop(0) - if not detail: + if not detail and detail != "": continue elif isinstance(detail, list): for item in detail: diff --git a/tests/test_flatten_errors.py b/tests/test_flatten_errors.py index cf34bd2..92d58c9 100644 --- a/tests/test_flatten_errors.py +++ b/tests/test_flatten_errors.py @@ -118,3 +118,11 @@ def test_does_not_raise_recursion_error(): "Failed due to a recursion error. Use an iterative approach rather than " "a recursive one to avoid reaching the maximum recursion depth in python." ) + + +def test_exception_with_detail_empty(): + detail = {"some_field": [ErrorDetail("", code="invalid")]} + errors = flatten_errors(detail) + assert len(errors) == 1 + assert errors[0].attr == "some_field" + assert errors[0].detail == ""