]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Fixes handling of exception chains in the util
authorTrenton H <797416+stumpylog@users.noreply.github.com>
Fri, 8 Sep 2023 18:48:53 +0000 (11:48 -0700)
committerTrenton H <797416+stumpylog@users.noreply.github.com>
Sat, 9 Sep 2023 02:20:08 +0000 (19:20 -0700)
src/documents/tests/utils.py

index c679a8f0234d35de780e2f40972671eed1720e4a..815a5d0b650b9dc7c122c1a12c585058014a9b7d 100644 (file)
@@ -24,6 +24,7 @@ from django.test import override_settings
 
 from documents.data_models import ConsumableDocument
 from documents.data_models import DocumentMetadataOverrides
+from documents.parsers import ParseError
 
 
 def setup_directories():
@@ -89,7 +90,7 @@ def util_call_with_backoff(
     method_or_callable: Callable,
     args: Union[List, Tuple],
     *,
-    skip_on_503=True,
+    skip_on_50x_err=True,
 ) -> Tuple[bool, Any]:
     """
     For whatever reason, the images started during the test pipeline like to
@@ -118,24 +119,30 @@ def util_call_with_backoff(
             result = method_or_callable(*args)
 
             succeeded = True
-        except httpx.HTTPError as exc:
-            warnings.warn(f"HTTP Exception for {exc.request.url} - {exc}")
-
-            if isinstance(exc, httpx.HTTPStatusError):
-                status_codes.append(exc.response.status_code)
-
-            retry_count = retry_count + 1
-
-            time.sleep(retry_time)
-            retry_time = retry_time * 2.0
+        except ParseError as e:
+            cause_exec = e.__cause__
+            if cause_exec is not None and isinstance(cause_exec, httpx.HTTPStatusError):
+                status_codes.append(cause_exec.response.status_code)
+                warnings.warn(
+                    f"HTTP Exception for {cause_exec.request.url} - {cause_exec}",
+                )
+            else:
+                warnings.warn(f"Unexpected error: {e}")
+        except Exception as e:
+            warnings.warn(f"Unexpected error: {e}")
+
+        retry_count = retry_count + 1
+
+        time.sleep(retry_time)
+        retry_time = retry_time * 2.0
 
     if (
         not succeeded
         and status_codes
-        and skip_on_503
-        and all(element == httpx.codes.SERVICE_UNAVAILABLE for element in status_codes)
+        and skip_on_50x_err
+        and all(httpx.codes.is_server_error(code) for code in status_codes)
     ):
-        pytest.skip("Repeated HTTP 503 for service")
+        pytest.skip("Repeated HTTP 50x for service")
 
     return succeeded, result