Classify wrong-region login failures as invalid_auth, not cannot_connect - #13
Merged
Merged
Conversation
Each Pecron region signs login requests with its own secret, so
picking the wrong region rejects the login exactly like a wrong
password would -- but the config flow only mapped errors to
invalid_auth via fragile string matching ("authentication"/"401" in
the message), so a region-mismatch rejection fell through to the
generic cannot_connect error instead, telling users to check their
internet connection when the real fix was to try another region.
Two users hit this in GH#6: one couldn't log in at all, another
found switching from EU to US fixed it.
Use the API library's typed AuthenticationError instead of string
matching, and mention region as a possible cause in the invalid_auth
message. Also fixes an existing bug where the "no devices found"
check was inside the try block and got silently re-wrapped as
PecronConnectionError by the blanket except clause.
Co-Authored-By: Claude Sonnet 5 <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
Investigated GH#6 (login fails with
cannot_connecteven with correct credentials). The GitHub issue comments have the key clue: @dodyrmoy hit the same error and it went away after switching region from EU to US.Root cause: each region in
unofficial_pecron_api(US/EU/CN) signs login requests with a differentuser_domain_secret. Picking the wrong region produces a login rejection that's indistinguishable from a wrong password at the API level — both raiseAuthenticationError. Butconfig_flow.pyclassified errors with fragile string matching ("authentication" in str(err).lower() or "401" in str(err)), and the actual rejection message from a region mismatch doesn't contain either substring, so it fell through to the genericcannot_connectbucket — telling users to check their internet connection when the real fix was "try a different region."Changes:
AuthenticationErrorexplicitly instead of string-matching.invalid_autherror message now mentions region as a possible cause.tryblock, so raisingPecronAuthErrorthere got immediately caught and re-wrapped asPecronConnectionErrorby the blanketexcept. Moved outside the try so it surfaces correctly asinvalid_auth._validate_pecron_credentials: auth error, network error, no-devices, and success paths (previously untested).Didn't attempt automatic region detection (discussed and decided against for now) — this keeps manual region selection but makes the failure mode legible instead of misleading.
Closes #6 (at least the misdiagnosis part — if a user is on a genuinely unsupported region we still can't detect that, but they'll now get pointed at region as the likely cause instead of chasing a phantom network issue).
Test plan
pytest— 68/68 pass (4 new tests for the classification logic)ruff check/ruff format --checkclean on changed files🤖 Generated with Claude Code