Raise AuthenticationException on runtime auth failures - #43
Open
nameetpai wants to merge 2 commits into
Open
Conversation
send_request now raises AuthenticationException when a request fails because authentication is no longer valid, including auth-error payloads returned with an HTTP 200. Adds AuthenticationException.detect/from_response helpers so downstream consumers (e.g. Home Assistant) can trigger reauthentication.
bafulton
added a commit
to bafulton/homelab
that referenced
this pull request
Jun 3, 2026
…cket reconnect Wraps _on_socket_started in a try-except so failed session cookie refreshes are logged at DEBUG instead of ERROR. Repeated 403s from this callback trigger Cloudflare rate limiting upstream, eventually causing full integration failure (HA issues #152205, #172809). Remove once jaraco/jaraco.abode#43 is released and picked up by HA. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Today
Client._send_requestcollapses every non-success outcome into a genericjaraco.abode.Exception(ERROR.REQUEST). Callers therefore can't tell a transientnetwork/server error apart from an authentication failure (expired token whose
re-login no longer works, changed password, revoked session, etc.).
This change makes the library surface authentication failures as the dedicated
AuthenticationExceptionso downstream consumers can react appropriately — mostnotably Home Assistant, which wants to start a reauthentication flow rather than
silently retry forever. This was requested during review of
home-assistant/core#166429, where the maintainer asked that the auth-detection
logic live in this library and raise a dedicated authentication error.
What changed
Client._send_requestnow raisesAuthenticationExceptionwhen the responseindicates an auth failure, via the new
AuthenticationException.detect()/AuthenticationException.from_response()helpers.400,401,403.(application error codes
11002/13027, or messages such as"unauthorized" / "invalid credentials" / "password … match"). The content
type is checked before the body is parsed, and parsing is guarded.
AuthenticationExceptionsubclassesjaraco.abode.Exception, so it is stilltrapped by
send_request's single retry (cleanup=self.login). Transienttoken expiry keeps recovering exactly as before; only a persistent auth
failure now propagates as
AuthenticationException.best_messageis made more robust (case-insensitive content-type match,guarded JSON parsing, no
KeyErrorwhenmessageis absent).Backwards compatibility
AuthenticationExceptionis a subclass ofjaraco.abode.Exception, so existingexcept jaraco.abode.Exception:handlers continue to catch these failures. Nopublic signatures change.
Tests
tests/test_auth_handling.pyadds:detect()/from_response()across status codes, auth-like200 payloads, non-dict/invalid JSON bodies, and plain-text bodies.
m/responsesfixtures) asserting that apersistent
403, and an auth-error payload returned with200, both raiseAuthenticationException, while a single403followed by success stillrecovers through re-login.
A newsfragment is included (
newsfragments/+runtime-auth.feature.rst).