Skip to content

Fix: Improve error handling during OAuth token refresh (#284)#322

Merged
mesilov merged 3 commits intodevfrom
claude/fix-issue-284-token-refresh-errors-01Kf3SaMBYEyTB8nPuYokyK2
Dec 5, 2025
Merged

Fix: Improve error handling during OAuth token refresh (#284)#322
mesilov merged 3 commits intodevfrom
claude/fix-issue-284-token-refresh-errors-01Kf3SaMBYEyTB8nPuYokyK2

Conversation

@mesilov
Copy link
Copy Markdown
Collaborator

@mesilov mesilov commented Dec 5, 2025

Added specialized exceptions for different OAuth token refresh failure scenarios:

  • InvalidGrantException - for invalid/expired refresh tokens (requires user re-authorization)
  • PortalDomainNotFoundException - for non-existent or inaccessible portals

Improved ApiClient::getNewAuthToken() method:

  • Replaced generic error messages with specific exception types based on HTTP status codes
  • Added detailed error handling for different OAuth error codes (invalid_grant, invalid_client, etc.)
  • Enhanced error messages with both OAuth error code and description
  • Added support for HTTP 400, 401, 404, and 5xx status codes
  • Developers can now distinguish between different failure causes and implement specific recovery logic

Added comprehensive unit tests:

  • Tests for all new exception types
  • Tests for different HTTP status codes and OAuth error scenarios
  • Success case test for token refresh

Updated CHANGELOG.md with details about the improvements.

Resolves: #284

Q A
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #284
License MIT

Added specialized exceptions for different OAuth token refresh failure scenarios:
- InvalidGrantException - for invalid/expired refresh tokens (requires user re-authorization)
- PortalDomainNotFoundException - for non-existent or inaccessible portals

Improved ApiClient::getNewAuthToken() method:
- Replaced generic error messages with specific exception types based on HTTP status codes
- Added detailed error handling for different OAuth error codes (invalid_grant, invalid_client, etc.)
- Enhanced error messages with both OAuth error code and description
- Added support for HTTP 400, 401, 404, and 5xx status codes
- Developers can now distinguish between different failure causes and implement specific recovery logic

Added comprehensive unit tests:
- Tests for all new exception types
- Tests for different HTTP status codes and OAuth error scenarios
- Success case test for token refresh

Updated CHANGELOG.md with details about the improvements.

Resolves: #284
@mesilov mesilov self-assigned this Dec 5, 2025
@mesilov mesilov added the bug in SDK Something isn't working in SDK label Dec 5, 2025
@mesilov mesilov added this to the 1.9.0 milestone Dec 5, 2025
- Apply Rector code quality improvements (UnwrapSprintfOneArgumentRector, RenameVariableToMatchMethodCallReturnTypeRector)
- Fix testGetNewAuthTokenSuccess: add required 'expires' field to mock response
- Rename test parameter from $expectedException to $throwable for consistency
- All unit tests pass (522 tests, 921 assertions)
Centralized OAuth token refresh error handling by moving logic from
ApiClient::getNewAuthToken() to a new ApiLevelErrorHandler::handleOAuthError() method.

Changes:
- Added ApiLevelErrorHandler::handleOAuthError() method for centralized OAuth error handling
- Simplified ApiClient::getNewAuthToken() from ~120 lines to ~20 lines
- Method now delegates all error handling to ApiLevelErrorHandler
- Improved code maintainability and reduced duplication
- Added 'never' return type to handleOAuthError() for PHPStan compatibility

Benefits:
- Single source of truth for OAuth error handling logic
- Easier to maintain and test error handling
- Consistent error handling patterns across the SDK
- Better separation of concerns

All tests pass (522 tests, 921 assertions)
All linters pass (PHP CS Fixer, PHPStan, Rector)

Related: #284
@mesilov mesilov merged commit 1a4061f into dev Dec 5, 2025
24 checks passed
@mesilov mesilov deleted the claude/fix-issue-284-token-refresh-errors-01Kf3SaMBYEyTB8nPuYokyK2 branch December 13, 2025 05:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug in SDK Something isn't working in SDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants