Skip to content

Commit e459fd7

Browse files
strausmannclaude
andauthored
fix(brother-ql): doppelter 'preflight SNMP failed' Prefix vermieden (Issue #105) (#114)
query_preflight() in snmp_helper.py erzeugt SnmpQueryError-Messages die bereits mit "preflight SNMP failed: " beginnen. BrotherQLBackend hat denselben Prefix nochmal vorangestellt, was zu doppelten Error-Messages führte ("preflight SNMP failed: preflight SNMP failed: …"). Fix: prüft ob der Prefix bereits vorhanden ist, bevor er hinzugefügt wird. Refs #105 Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 3939741 commit e459fd7

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

backend/app/printer_backends/brother_ql_backend.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,12 @@ async def preflight_check(
173173
timeout_s=timeout_s,
174174
)
175175
except SnmpQueryError as exc:
176-
raise PrinterOfflineError(f"preflight SNMP failed: {exc}") from exc
176+
# Issue #105: query_preflight() kann bereits "preflight SNMP failed: …"
177+
# im SnmpQueryError enthalten — doppeltes Prefixen vermeiden.
178+
msg = str(exc)
179+
if not msg.startswith("preflight SNMP failed:"):
180+
msg = f"preflight SNMP failed: {msg}"
181+
raise PrinterOfflineError(msg) from exc
177182

178183
if "noPaper" in preflight.error_flags:
179184
raise TapeEmptyError()

backend/tests/unit/printer_backends/test_brother_ql_backend.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,37 @@ async def fake_preflight(
179179
backend = BrotherQLBackend(host="unreachable", model_id="QL-820NWB")
180180
with pytest.raises(PrinterOfflineError, match="preflight SNMP failed"):
181181
await backend.preflight_check()
182+
183+
184+
@pytest.mark.anyio
185+
async def test_preflight_check_no_duplicate_prefix_when_snmp_error_already_has_prefix(
186+
monkeypatch: pytest.MonkeyPatch,
187+
) -> None:
188+
"""Issue #105: PrinterOfflineError darf 'preflight SNMP failed' nicht doppelt enthalten.
189+
190+
query_preflight() erzeugt SnmpQueryError-Messages die bereits mit
191+
'preflight SNMP failed: ' beginnen (snmp_helper.py Zeile 191).
192+
Das BrotherQLBackend darf dann den Prefix NICHT nochmal voranstellen.
193+
"""
194+
from app.printer_backends.exceptions import PrinterOfflineError
195+
from app.printer_backends.snmp_helper import PreflightStatus, SnmpQueryError
196+
197+
# Simuliert was snmp_helper.query_preflight() bei SNMP-Fehler wirft —
198+
# die Message beginnt bereits mit "preflight SNMP failed: ".
199+
already_prefixed_msg = "preflight SNMP failed: UDP transport error"
200+
201+
async def fake_preflight(
202+
host: str, *, community: str = "public", timeout_s: float = 3.0
203+
) -> PreflightStatus:
204+
raise SnmpQueryError(already_prefixed_msg)
205+
206+
monkeypatch.setattr("app.printer_backends.brother_ql_backend.query_preflight", fake_preflight)
207+
backend = BrotherQLBackend(host="unreachable", model_id="QL-820NWB")
208+
with pytest.raises(PrinterOfflineError) as exc_info:
209+
await backend.preflight_check()
210+
211+
error_msg = str(exc_info.value)
212+
# Darf nur EINMAL "preflight SNMP failed" enthalten
213+
assert error_msg.count("preflight SNMP failed") == 1, (
214+
f"Duplicate prefix in error message: {error_msg!r}"
215+
)

0 commit comments

Comments
 (0)