From 0f2b7a8fab5ad8fed6db6dc0c2ff00da99c90aa1 Mon Sep 17 00:00:00 2001 From: Stommer Date: Sat, 24 Oct 2026 22:53:46 +0200 Subject: [PATCH] =?UTF-8?q?Nenne=20in=20der=20Paperless-Fehlermeldung=20St?= =?UTF-8?q?atuscode,=20Pfad=20und=20Grund,=20damit=20sich=20eine=20abgeleh?= =?UTF-8?q?nte=20Abfrage=20zuordnen=20l=C3=A4sst.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- backend/app/sources.py | 13 +++++++++- backend/tests/test_connector_followup.py | 31 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/backend/app/sources.py b/backend/app/sources.py index 4667dd4..169526a 100644 --- a/backend/app/sources.py +++ b/backend/app/sources.py @@ -1014,11 +1014,22 @@ 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("Paperless hat die Anfrage nicht beantwortet.") from exc + raise unprocessable(f"Paperless hat die Anfrage nicht beantwortet ({_refusal(exc, path)}).") from exc except (URLError, TimeoutError, json.JSONDecodeError) as exc: raise unprocessable("Paperless ist nicht erreichbar.") from exc +def _refusal(exc: HTTPError, path: str) -> str: + """Status and, when Paperless names one, its reason. The body is the owner's own instance, cut short.""" + reason = "" + try: + body = json.loads(exc.read(2000).decode("utf-8", "replace")) + reason = json.dumps(body, ensure_ascii=False)[:240] if isinstance(body, (dict, list)) else "" + except (ValueError, OSError): + reason = "" + return f"HTTP {exc.code} auf {path}" + (f": {reason}" if reason else "") + + def _correspondent(doc: dict, names: dict | None = None) -> str | None: value = doc.get("correspondent_name") or doc.get("correspondent") if isinstance(value, str) and value.strip(): diff --git a/backend/tests/test_connector_followup.py b/backend/tests/test_connector_followup.py index f5c9520..78f5d72 100644 --- a/backend/tests/test_connector_followup.py +++ b/backend/tests/test_connector_followup.py @@ -10,6 +10,8 @@ import io import zipfile from email.parser import BytesParser +import pytest + from app.media.port import MemoryAdapter from app.sourceaccess import borrow_original, complete_borrow from app.sourceregistry import SourceModule, unregister_source, register_source @@ -652,6 +654,35 @@ def test_paperless_custom_field_filters_use_the_instance_fields(client, db, monk ) +def test_paperless_refusal_names_the_status_and_reason(monkeypatch): + import io + from urllib.error import HTTPError + + import app.sources as sources + from app.core.errors import Problem + + def refuse(code, body): + def fake(request, timeout=0): + raise HTTPError(request.full_url, code, "x", {}, io.BytesIO(body)) + + return fake + + monkeypatch.setattr(sources, "urlopen", refuse(400, '{"custom_field_query": ["Ungültig"]}'.encode())) + with pytest.raises(Problem) as caught: + sources._paperless_get("https://paper.example.test", "token-alpha", "/api/documents/", {"page_size": "20"}) + assert "HTTP 400 auf /api/documents/" in caught.value.detail + assert "custom_field_query" in caught.value.detail + assert "token-alpha" not in caught.value.detail + monkeypatch.setattr(sources, "urlopen", refuse(500, b"Fehler")) + 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/).") + monkeypatch.setattr(sources, "urlopen", refuse(403, b"{}")) + with pytest.raises(Problem) as caught: + sources._paperless_get("https://paper.example.test", "token-alpha", "/api/documents/", {}) + assert "Schlüssel abgelehnt" in caught.value.detail + + def test_paperless_thumbnail_is_bound_to_the_owners_instance(client, db, monkeypatch): import app.sources as sources