♻️ refactor: single _error_class mapping for status→exception #200

Merged
mat merged 1 commit from refactor/error-class-helper into main 2026-10-27 14:36:41 +00:00
Owner

Summary

The status→exception mapping, previously encoded twice with different rules, collapses into one helper (#187):

_error_class(status_code, *, auth_context=False)
├── 401                          ──► ComwattAuthError  (always)
├── 403 with auth_context=True   ──► ComwattAuthError  (login endpoint only)
└── anything else                ──► ComwattAPIError

_api_error(response, *, auth_context=False)  ──► uses _error_class
_post_authent                                ──► now just: raise _api_error(response, auth_context=True)

Future error sites (e.g. the WS handshake path) branch on the helper instead of guessing.

Evidence

  • Before: _api_error mapped 401-only, _post_authent mapped (401, 403) inline — the asymmetry undocumented and duplicative.
    After: behavior pinned by tests — existing test_authenticate_error_403 (login 403 → AuthError) plus a new pin test_resource_403_raises_api_error_not_auth_error (resource 403 → APIError, previously uncovered). pytest -q → 243 passed, mypy → clean.

Merge Danger

Door: two-way

Blast Radius: none — pure refactor, no behavior change; _post_authent still accepts only 200 on login (unchanged by #180 on purpose).

Closes #187.

## Summary The status→exception mapping, previously encoded twice with different rules, collapses into one helper (#187): ```text _error_class(status_code, *, auth_context=False) ├── 401 ──► ComwattAuthError (always) ├── 403 with auth_context=True ──► ComwattAuthError (login endpoint only) └── anything else ──► ComwattAPIError _api_error(response, *, auth_context=False) ──► uses _error_class _post_authent ──► now just: raise _api_error(response, auth_context=True) ``` Future error sites (e.g. the WS handshake path) branch on the helper instead of guessing. ## Evidence - **Before:** `_api_error` mapped 401-only, `_post_authent` mapped (401, 403) inline — the asymmetry undocumented and duplicative. **After:** behavior pinned by tests — existing `test_authenticate_error_403` (login 403 → AuthError) plus a new pin `test_resource_403_raises_api_error_not_auth_error` (resource 403 → APIError, previously uncovered). `pytest -q` → 243 passed, `mypy` → clean. ## Merge Danger **Door:** two-way **Blast Radius:** none — pure refactor, no behavior change; `_post_authent` still accepts only 200 on login (unchanged by #180 on purpose). Closes #187.
♻️ refactor: single _error_class mapping for status→exception
All checks were successful
Tests / forgejo-pytest (push) Successful in 44s
Tests / forgejo-pytest (pull_request) Successful in 43s
79ccfd0fae
mat changed title from WIP: ♻️ refactor: single _error_class mapping for status→exception to ♻️ refactor: single _error_class mapping for status→exception 2026-10-27 14:36:40 +00:00
mat merged commit d21c1e3aa5 into main 2026-10-27 14:36:41 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
mat/python-comwatt-client!200
No description provided.