Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@nicknisi
Copy link
Member

@nicknisi nicknisi commented Dec 23, 2025

Problem

When token refresh fails, the error doesn't include enough context to debug which user/session was affected. This makes it harder to correlate errors with specific users in logs or the WorkOS dashboard.

Solution

Add TokenRefreshError (matching the pattern in authkit-session) that includes userId and sessionId on the error object. These are extracted from the session before throwing.

Example Output

⨯ Error [TokenRefreshError]: Failed to refresh session: Error: invalid_grant
Error Description: Session has already ended.
    at async testRefresh (src/app/test-error/actions.ts:7:5)
   5 | export async function testRefresh() {
   6 |   try {
>  7 |     await refreshSession();
     |     ^
   8 |     return { success: true };
   9 |   } catch (error) {
  10 |     if (error instanceof TokenRefreshError) { {
  data: undefined,
  userId: 'user_01ABC123...',
  sessionId: 'session_01XYZ789...',
  digest: '1995551028',
  [cause]: OauthException: Error: invalid_grant
  Error Description: Session has already ended.
      at Generator.throw (<anonymous>) {
    status: 400,
    requestID: '',
    error: 'invalid_grant',
    errorDescription: 'Session has already ended.',
    rawData: {
      error: 'invalid_grant',
      error_description: 'Session has already ended.'
    }
  }
}

Consumers can catch and inspect these properties:

import { TokenRefreshError } from '@workos-inc/authkit-nextjs';

try {
  await refreshSession();
} catch (error) {
  if (error instanceof TokenRefreshError) {
    console.error('Refresh failed for user:', error.userId, 'session:', error.sessionId);
  }
}

@nicknisi nicknisi requested a review from a team as a code owner December 23, 2025 21:11
@nicknisi nicknisi requested a review from stacurry December 23, 2025 21:11
@nicknisi nicknisi force-pushed the nicknisi/expanded-error-info branch from 8245f38 to 5c06ede Compare December 23, 2025 21:15
@nicknisi nicknisi changed the title feat: add SessionError with userId and sessionId for debugging feat: add TokenRefreshError with userId and sessionId for debugging Dec 23, 2025
@nicknisi nicknisi force-pushed the nicknisi/expanded-error-info branch 2 times, most recently from ca2bd15 to f5479ba Compare December 23, 2025 21:18
src/errors.ts Outdated Show resolved Hide resolved
Copy link

@faroceann faroceann left a comment

Choose a reason for hiding this comment

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

lgtm

Add custom error classes matching authkit-session pattern:
- AuthKitError base class with optional data field
- TokenRefreshError with userId/sessionId context for debugging
- getSessionErrorContext helper to extract context from sessions

Update session refresh failure to include user/session context.
@nicknisi nicknisi force-pushed the nicknisi/expanded-error-info branch from f5479ba to b920ab5 Compare December 23, 2025 23:21
@nicknisi nicknisi merged commit ca91ca0 into main Dec 23, 2025
5 checks passed
@nicknisi nicknisi deleted the nicknisi/expanded-error-info branch December 23, 2025 23:23
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.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.