Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion backend/app/printer_backends/brother_ql_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ async def preflight_check(
timeout_s=timeout_s,
)
except SnmpQueryError as exc:
raise PrinterOfflineError(f"preflight SNMP failed: {exc}") from exc
# Issue #105: query_preflight() kann bereits "preflight SNMP failed: …"
# im SnmpQueryError enthalten — doppeltes Prefixen vermeiden.
msg = str(exc)
if not msg.startswith("preflight SNMP failed:"):
msg = f"preflight SNMP failed: {msg}"
raise PrinterOfflineError(msg) from exc
Comment on lines +178 to +181

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Wenn query_preflight() einen SnmpQueryError mit einer Nachricht wie "preflight SNMP returned error: ..." wirft, startet diese nicht mit "preflight SNMP failed:". Dadurch wird die Fehlermeldung zu "preflight SNMP failed: preflight SNMP returned error: ..." formatiert, was ein doppeltes "preflight SNMP"-Präfix erzeugt.

Eine robustere Prüfung wäre es, auf "preflight SNMP" zu prüfen, um jegliche doppelte Präfixe für alle von query_preflight() geworfenen Fehler zu vermeiden.

Suggested change
msg = str(exc)
if not msg.startswith("preflight SNMP failed:"):
msg = f"preflight SNMP failed: {msg}"
raise PrinterOfflineError(msg) from exc
msg = str(exc)
if not msg.startswith("preflight SNMP"):
msg = f"preflight SNMP failed: {msg}"
raise PrinterOfflineError(msg) from exc


if "noPaper" in preflight.error_flags:
raise TapeEmptyError()
Expand Down
34 changes: 34 additions & 0 deletions backend/tests/unit/printer_backends/test_brother_ql_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,37 @@ async def fake_preflight(
backend = BrotherQLBackend(host="unreachable", model_id="QL-820NWB")
with pytest.raises(PrinterOfflineError, match="preflight SNMP failed"):
await backend.preflight_check()


@pytest.mark.anyio
async def test_preflight_check_no_duplicate_prefix_when_snmp_error_already_has_prefix(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Issue #105: PrinterOfflineError darf 'preflight SNMP failed' nicht doppelt enthalten.

query_preflight() erzeugt SnmpQueryError-Messages die bereits mit
'preflight SNMP failed: ' beginnen (snmp_helper.py Zeile 191).
Das BrotherQLBackend darf dann den Prefix NICHT nochmal voranstellen.
"""
from app.printer_backends.exceptions import PrinterOfflineError
from app.printer_backends.snmp_helper import PreflightStatus, SnmpQueryError

# Simuliert was snmp_helper.query_preflight() bei SNMP-Fehler wirft —
# die Message beginnt bereits mit "preflight SNMP failed: ".
already_prefixed_msg = "preflight SNMP failed: UDP transport error"

async def fake_preflight(
host: str, *, community: str = "public", timeout_s: float = 3.0
) -> PreflightStatus:
raise SnmpQueryError(already_prefixed_msg)

monkeypatch.setattr("app.printer_backends.brother_ql_backend.query_preflight", fake_preflight)
backend = BrotherQLBackend(host="unreachable", model_id="QL-820NWB")
with pytest.raises(PrinterOfflineError) as exc_info:
await backend.preflight_check()

error_msg = str(exc_info.value)
# Darf nur EINMAL "preflight SNMP failed" enthalten
assert error_msg.count("preflight SNMP failed") == 1, (
f"Duplicate prefix in error message: {error_msg!r}"
)
Loading