fix: Fehler-Feedback Kreditzuordnung, robustes PDF-Fehlerhandling

Kreditzuordnung im Szenario prueft jetzt response.ok, zeigt bei Fehlschlag
eine deutsche Meldung und macht die Checkbox-Aenderung rueckgaengig statt
stillschweigend neu zu laden. process_pdf faengt zusaetzlich zu ParserError
auch unerwartete Parser-Exceptions (z.B. pdfminer bei strukturell kaputten
PDFs) ab und legt dafuer ein sauberes error-Statement an statt eines
unbehandelten 500; die Datei bleibt dabei im Posteingang.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-17 19:14:43 +02:00
parent afbbcbe9a5
commit df6615c8f7
3 changed files with 51 additions and 9 deletions

View File

@@ -23,6 +23,15 @@ def _find_or_create_account(session: Session, bank: str, iban: str | None, filen
return acc
def _error_statement(session: Session, filename: str, message: str) -> Statement:
stmt = Statement(filename=filename, bank="unbekannt", account_id=None,
status="error", error_message=message)
session.add(stmt)
session.commit()
session.refresh(stmt)
return stmt
def process_pdf(session: Session, path: Path) -> Statement:
settings = get_settings()
filename = path.name
@@ -30,12 +39,15 @@ def process_pdf(session: Session, path: Path) -> Statement:
try:
parsed = parse_pdf(path)
except ParserError as exc:
stmt = Statement(filename=filename, bank="unbekannt", account_id=None,
status="error", error_message=str(exc))
session.add(stmt)
session.commit()
session.refresh(stmt)
return stmt
return _error_statement(session, filename, str(exc))
except Exception as exc:
# Fremdbibliotheken (pdfplumber/pdfminer) werfen bei strukturell
# kaputten PDFs eigene Exception-Typen statt ParserError (z.B.
# PdfminerException "No /Root object!"). Ohne diesen Fang wuerde ein
# kaputtes Upload zu einem unbehandelten 500 fuehren statt zu einem
# sauberen Fehler-Import wie bei "Bank nicht erkannt". Die Datei
# bleibt dabei unangetastet im Posteingang.
return _error_statement(session, filename, f"PDF nicht lesbar: {exc}")
account = _find_or_create_account(session, parsed.bank, parsed.iban, filename)

View File

@@ -204,7 +204,7 @@
{% for l in loans %}
<label class="inline-form">
<input type="checkbox" {% if l.id in row.assigned_loan_ids %}checked{% endif %}
onchange="toggleScenarioLoan({{ sc.id }}, {{ l.id }}, this.checked)">
onchange="toggleScenarioLoan({{ sc.id }}, {{ l.id }}, this.checked, this)">
{{ l.name }}
</label>
{% else %}
@@ -354,9 +354,18 @@
recSelect.disabled = isCategory;
}
function toggleScenarioLoan(scenarioId, loanId, checked) {
function toggleScenarioLoan(scenarioId, loanId, checked, checkbox) {
fetch('/api/scenarios/' + scenarioId + '/loans/' + loanId, { method: checked ? 'POST' : 'DELETE' })
.then(function () { window.location.reload(); });
.then(function (resp) {
if (!resp.ok) {
throw new Error('HTTP ' + resp.status);
}
window.location.reload();
})
.catch(function (err) {
checkbox.checked = !checked;
alert('Zuordnung fehlgeschlagen: ' + err.message);
});
}
var loadedSchedules = {};

View File

@@ -71,3 +71,24 @@ def test_upload_rejects_non_pdf(client, tmp_path, monkeypatch):
r = client.post("/api/imports/upload", headers=H,
files={"file": ("x.txt", b"not a pdf", "text/plain")})
assert r.status_code == 400
def test_upload_corrupt_pdf_produces_error_statement_not_500(client, tmp_path, monkeypatch):
# Datei besteht die Endungspruefung (.pdf), ist aber strukturell kein
# gueltiges PDF -> pdfplumber wirft eine eigene Exception (keine
# ParserError). Das darf nicht als unbehandelter 500 durchschlagen,
# sondern muss wie "Bank nicht erkannt" als sauberer Fehler-Import
# sichtbar werden.
monkeypatch.setenv("FB_INBOX_DIR", str(tmp_path / "inbox"))
monkeypatch.setenv("FB_UPLOADS_DIR", str(tmp_path / "uploads"))
from app.config import get_settings
get_settings.cache_clear()
r = client.post("/api/imports/upload", headers=H,
files={"file": ("kaputt.pdf", b"not a pdf", "application/pdf")})
assert r.status_code == 201
body = r.json()
assert body["status"] == "error"
assert "PDF nicht lesbar" in body["error_message"]
# Datei bleibt im Posteingang liegen, wird nicht ins Uploads-Verzeichnis verschoben.
assert (tmp_path / "inbox" / "kaputt.pdf").exists()
assert not (tmp_path / "uploads" / "kaputt.pdf").exists()