Skip to content

Conversation

@CasualDeveloper
Copy link

@CasualDeveloper CasualDeveloper commented Dec 25, 2025

Summary

  • Token refresh failures now throw errors instead of returning Response objects
  • API error responses returned as-is for OpenCode/AI SDK to handle properly
  • Matches the pattern used by opencode-copilot-auth and opencode-anthropic-auth

Problem

The plugin's error handling didn't follow OpenCode plugin conventions, causing errors to display incorrectly in the UI (persisting errors, raw JSON output).

Screenshot 2025-12-25 at 2 23 21 PM 5 45 34 PM

Changes

lib/request/fetch-helpers.ts

  • refreshAndUpdateToken(): Now throws Error on failure instead of returning { success: false, response }
  • handleErrorResponse(): Returns original response as-is (clones for logging) instead of consuming and enriching it

index.ts

  • Simplified token refresh call - errors propagate naturally via throw

test/fetch-helpers.test.ts

  • Updated test to match new behavior

Result

Errors now display correctly in the UI:

Screenshot 2025-12-25 at 2 29 56 PM

References

Copilot AI review requested due to automatic review settings December 25, 2025 09:35
- Token refresh failures now throw errors instead of returning Response
- API error responses returned as-is for OpenCode/AI SDK to handle
- Matches pattern used by opencode-copilot-auth and opencode-anthropic-auth

Fixes numman-ali#80
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors error handling to align with OpenCode plugin conventions, ensuring errors are properly displayed in the UI. The key changes involve making token refresh failures throw errors instead of returning Response objects, and returning API error responses as-is for the OpenCode/AI SDK to handle.

Key Changes

  • Token refresh failures now throw Error instead of returning { success: false, response } objects
  • API error responses are returned unchanged (using response.clone() for logging) instead of enriching them with additional fields
  • Simplified error handling in the main fetch function by letting errors propagate naturally

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
lib/request/fetch-helpers.ts Updated refreshAndUpdateToken() to throw on failure; modified handleErrorResponse() to return original response using clone; removed unused HTTP_STATUS import and toNumber() helper
index.ts Simplified token refresh call by removing error check logic (errors propagate via throw)
test/fetch-helpers.test.ts Updated test to verify original response is preserved instead of checking for enriched error fields

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@CasualDeveloper CasualDeveloper force-pushed the fix/error-handling-pattern branch from c70213f to 80e1c0d Compare December 25, 2025 09:38
@CasualDeveloper
Copy link
Author

Added tests for refreshAndUpdateToken() in the follow-up commit (e80e7b2) to address the Copilot review feedback.

The tests cover:

  • Error path: verifies the function throws when refreshAccessToken() returns { type: 'failed' }
  • Success path: verifies auth state is updated and client.auth.set() is called with correct parameters

@CasualDeveloper CasualDeveloper deleted the fix/error-handling-pattern branch December 26, 2025 07:51
@CasualDeveloper CasualDeveloper restored the fix/error-handling-pattern branch December 26, 2025 07:53
@CasualDeveloper
Copy link
Author

Accidentally closed it when I just wanted to rename it. Reopened.

@CasualDeveloper CasualDeveloper changed the title fix: align error handling with OpenCode plugin conventions fix: align error handling with OpenCode plugin conventions (#80) Dec 26, 2025
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.

[BUG] Error handling pattern doesn't match OpenCode plugin conventions (#81)

1 participant