From ffdaf1c721a04e716d69517852b8744affc5cb0c Mon Sep 17 00:00:00 2001 From: Stommer Date: Sun, 25 Oct 2026 06:58:07 +0100 Subject: [PATCH] Weise bei einem Paperless-Fehler auf die Kombination aus allen Tags und Zusatzfeld hin, die Paperless selbst mit HTTP 500 beantwortet. Co-Authored-By: Claude Sonnet 5.5 --- backend/app/sources.py | 10 +++++++--- backend/tests/test_connector_followup.py | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/backend/app/sources.py b/backend/app/sources.py index 169526a..13f79ab 100644 --- a/backend/app/sources.py +++ b/backend/app/sources.py @@ -417,7 +417,10 @@ def paperless_documents(conn, owner_id: UUID, query: str, connector_id: UUID | N release_for_external(conn) names = _paperless_lookup(row["id"], base, row["secret"]) params = _paperless_params(query, options, names["custom_fields"]) - payload = _paperless_get(base, row["secret"], "/api/documents/", params) + # Observed on a real instance: all-tags together with a custom field makes Paperless answer 500. + clash = "," in params.get("tags__id__all", "") and "custom_field_query" in params + hint = "Das tritt auf, wenn „Alle gewählten Tags“ mit einem Zusatzfeld zusammentrifft. Wähle „Mindestens eines“ oder lass das Zusatzfeld weg." if clash else "" + payload = _paperless_get(base, row["secret"], "/api/documents/", params, hint) assert_still_owned(conn, row["id"], owner_id) terms = _search_terms(query, (options or {}).get("title")) items = [] @@ -1005,7 +1008,7 @@ def _filename(disposition: str | None) -> str: return value.replace("\\", "").replace("/", "")[:180] -def _paperless_get(base: str, token: str, path: str, params: dict) -> dict: +def _paperless_get(base: str, token: str, path: str, params: dict, hint: str = "") -> dict: url = f"{base}{path}?{urlencode(params)}" request = Request(url, headers={"Authorization": f"Token {token}", "Accept": "application/json"}) try: @@ -1014,7 +1017,8 @@ def _paperless_get(base: str, token: str, path: str, params: dict) -> dict: except HTTPError as exc: if exc.code in {401, 403}: raise unprocessable("Paperless hat den Schlüssel abgelehnt.") from exc - raise unprocessable(f"Paperless hat die Anfrage nicht beantwortet ({_refusal(exc, path)}).") from exc + advice = f" {hint}" if hint and exc.code >= 500 else "" + raise unprocessable(f"Paperless hat die Anfrage nicht beantwortet ({_refusal(exc, path)}).{advice}") from exc except (URLError, TimeoutError, json.JSONDecodeError) as exc: raise unprocessable("Paperless ist nicht erreichbar.") from exc diff --git a/backend/tests/test_connector_followup.py b/backend/tests/test_connector_followup.py index 78f5d72..dbd57e6 100644 --- a/backend/tests/test_connector_followup.py +++ b/backend/tests/test_connector_followup.py @@ -677,6 +677,13 @@ def test_paperless_refusal_names_the_status_and_reason(monkeypatch): with pytest.raises(Problem) as caught: sources._paperless_get("https://paper.example.test", "token-alpha", "/api/documents/", {}) assert caught.value.detail.endswith("(HTTP 500 auf /api/documents/).") + with pytest.raises(Problem) as caught: + sources._paperless_get("https://paper.example.test", "token-alpha", "/api/documents/", {}, "Hinweis.") + assert caught.value.detail.endswith("(HTTP 500 auf /api/documents/). Hinweis.") + monkeypatch.setattr(sources, "urlopen", refuse(400, b"{}")) + with pytest.raises(Problem) as caught: + sources._paperless_get("https://paper.example.test", "token-alpha", "/api/documents/", {}, "Hinweis.") + assert "Hinweis" not in caught.value.detail monkeypatch.setattr(sources, "urlopen", refuse(403, b"{}")) with pytest.raises(Problem) as caught: sources._paperless_get("https://paper.example.test", "token-alpha", "/api/documents/", {})