fix(login): narrow exception types and cover cleanup path

Replaces broad `except Exception` with specific types that reflect the
actual failure modes:
- _save_credentials: `except OSError` (IO-only path; re-raises for cleanup)
- _post error handler: `except (OSError, ValueError)` (network + bad JSON)
- poll-loop: `except (OSError, ValueError)` (network + bad JSON)

Also:
- Simplify _fake_get to use next(..., default) — removes uncovered
  StopIteration branch
- Add encoding="utf-8" to open() in test_approved_flow_returns_0
- Add test_cleanup_on_write_failure to cover the except OSError block
  in _save_credentials

All files score 10.00/10 on pylint (fail-under=10) and 0 errors on
pyright strict.
This commit is contained in:
2026-07-21 04:27:30 +00:00
committed by didericis
parent 21b253c7eb
commit adc033a902
2 changed files with 17 additions and 9 deletions
+3 -3
View File
@@ -92,7 +92,7 @@ def _save_credentials(
with os.fdopen(fd, "w") as f:
f.write(content)
os.replace(tmp, path)
except Exception:
except OSError:
try:
tmp.unlink()
except OSError:
@@ -118,7 +118,7 @@ def cmd_login(argv: list[str]) -> int:
try:
resp = _post(f"{console_url}/api/v1/hosts/authorize", {"label": label})
except Exception as exc:
except (OSError, ValueError) as exc:
sys.stderr.write(f"bb login: failed to start authorization: {exc}\n")
return 1
@@ -143,7 +143,7 @@ def cmd_login(argv: list[str]) -> int:
code, result = _get(
f"{console_url}/api/v1/hosts/authorize/{device_code}"
)
except Exception:
except (OSError, ValueError):
continue
if code == 410: