Skip to content

Classify wrong-region login failures as invalid_auth, not cannot_connect - #13

Merged
jsight merged 1 commit into
mainfrom
fix-region-login-error-classification
Jul 31, 2026
Merged

Classify wrong-region login failures as invalid_auth, not cannot_connect#13
jsight merged 1 commit into
mainfrom
fix-region-login-error-classification

Conversation

@jsight

@jsight jsight commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Summary

Investigated GH#6 (login fails with cannot_connect even 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 different user_domain_secret. Picking the wrong region produces a login rejection that's indistinguishable from a wrong password at the API level — both raise AuthenticationError. But config_flow.py classified 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 generic cannot_connect bucket — telling users to check their internet connection when the real fix was "try a different region."

Changes:

  • Catch the library's typed AuthenticationError explicitly instead of string-matching.
  • invalid_auth error message now mentions region as a possible cause.
  • Incidental fix: the "no devices found" check was inside the try block, so raising PecronAuthError there got immediately caught and re-wrapped as PecronConnectionError by the blanket except. Moved outside the try so it surfaces correctly as invalid_auth.
  • Added test coverage for _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 --check clean on changed files

🤖 Generated with Claude Code

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>
@jsight
jsight merged commit 6aaae49 into main Jul 31, 2026
5 checks passed
@jsight
jsight deleted the fix-region-login-error-classification branch July 31, 2026 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot login, error "cannot_connect"

1 participant