[client] Unify status→exception mapping: _api_error (401 only) vs _post_authent (401+403) #187

Closed
opened 2026-10-27 11:31:57 +00:00 by mat · 0 comments
Owner

The status→exception mapping is encoded in two places with different rules:

  • _api_error (_core.py:46-47): 401 → ComwattAuthError, everything else → ComwattAPIError;
  • _post_authent (_core.py:70-71): (401, 403) → ComwattAuthError.

The asymmetry is arguably intentional (403 means "bad credentials" only on the auth endpoint; a 403 on a resource is an authorization failure, not an auth failure), but nothing states that, and any future third site (e.g. the WS handshake error path) will have to guess.

Fix: extract one helper (e.g. _error_class(status_code, *, auth_context)) used by both sites, with a self-explanatory signature encoding the 403 nuance — or at minimum document the asymmetry at both sites.

Done when: single mapping site (or documented asymmetry), pytest -q green, tests/test_exceptions.py still pins both behaviors.

The status→exception mapping is encoded in two places with different rules: - `_api_error` (`_core.py:46-47`): 401 → `ComwattAuthError`, everything else → `ComwattAPIError`; - `_post_authent` (`_core.py:70-71`): (401, 403) → `ComwattAuthError`. The asymmetry is arguably intentional (403 means "bad credentials" only on the auth endpoint; a 403 on a resource is an authorization failure, not an auth failure), but nothing states that, and any future third site (e.g. the WS handshake error path) will have to guess. **Fix:** extract one helper (e.g. `_error_class(status_code, *, auth_context)`) used by both sites, with a self-explanatory signature encoding the 403 nuance — or at minimum document the asymmetry at both sites. **Done when:** single mapping site (or documented asymmetry), `pytest -q` green, `tests/test_exceptions.py` still pins both behaviors.
mat closed this issue 2026-10-27 14:36:41 +00:00
Sign in to join this conversation.
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#187
No description provided.