Skip to content

Added OAuthException and Tests, fixes #287 #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

zac-workos
Copy link

Description

Addresses an issue where authentication attempts with an invalid or expired authorization code resulted in a meaningless exception. The root cause was that the API’s OAuth error payload did not match the expected error response model, leading to null fields and unhelpful error messages.

Changes

  • Added a new OAuthErrorResponse model to match the actual OAuth error payload (error and error_description).
  • Introduced a new OAuthException to provide meaningful error messages for OAuth-specific errors.
  • Updated WorkOS.handleResponseError to detect and map OAuth error payloads (e.g., invalid_grant) to OAuthException.
  • Added a test (OAuthErrorHandlingTest) to verify that an invalid code triggers the correct exception and message.

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Summary

This PR addresses issue #287 by implementing proper OAuth error handling for authentication failures in the WorkOS Kotlin SDK. The problem was that OAuth errors from the WorkOS API have a different JSON structure ({"error": "code", "error_description": "message"}) compared to regular API errors, causing authentication failures with invalid or expired authorization codes to throw generic exceptions with null fields and unhelpful messages.

The implementation adds three key components:

  1. OAuthErrorResponse data class: A new model that matches the OAuth error payload structure with error and error_description fields, both nullable to handle partial error responses.

  2. OAuthException class: A specialized exception that extends the base Exception class, taking OAuth-specific parameters (error, errorDescription, requestId) and using the error description as the primary exception message for better developer experience.

  3. Enhanced error handling in WorkOS.handleResponseError: The method now detects OAuth errors by checking for the presence of "error" and "error_description" fields in 400 status responses, then parses them using the new OAuthErrorResponse model and throws the appropriate OAuthException.

The changes integrate seamlessly with the existing error handling architecture while providing OAuth-specific error detection that falls back to the standard BadRequestException for non-OAuth errors. A comprehensive test validates that invalid authorization codes trigger the correct exception type, ensuring the fix works as intended and preventing regression.

Confidence score: 4/5

  • This PR is safe to merge with low risk of production issues
  • Score reflects well-structured OAuth error handling with proper fallback mechanisms and comprehensive testing
  • Pay attention to the error detection logic in WorkOS.kt to ensure it correctly distinguishes OAuth vs standard errors

4 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile


@Test
fun authenticateWithCodeShouldThrowOAuthExceptionOnInvalidGrant() {
stubResponse(
Copy link

Choose a reason for hiding this comment

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

style: Consider adding indentation consistency - the stubResponse call has unusual indentation that doesn't match the rest of the codebase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant