Skip to content

Conversation

@mandarini
Copy link
Contributor

@mandarini mandarini commented Oct 10, 2025

Moved from: supabase/auth-js#1022
Author: @kiwicopple

Fixes #1688

Add support for throwOnError option in AuthClient to throw errors instead of returning them.

  • GoTrueClient Changes:

    • Add throwOnError option to GoTrueClient constructor.
    • Modify methods to throw errors when throwOnError is true.
    • Update methods like signUp, signInWithPassword, updateUser, and others to throw errors if throwOnError is true.
  • AuthClient Changes:

    • Update AuthClient to pass throwOnError option to GoTrueClient.
  • Tests:

    • Add tests in test/GoTrueClient.test.ts to verify throwOnError functionality.

Other changes

ConstructorParameters

Replace ConstructorParameters type extraction with direct import of GoTrueClientOptions:

// Before type AuthClientOptions = ConstructorParameters<typeof AuthClient>[0] export interface SupabaseAuthClientOptions extends AuthClientOptions {} // After import { GoTrueClientOptions } from '@supabase/auth-js' export interface SupabaseAuthClientOptions extends GoTrueClientOptions {}

This was introduced because ConstructorParameters wasn't resolving properly across package boundaries. Direct type extension is more explicit and ensures all auth-js properties are recognized.

Other

Also fix two test failures in the throwOnError feature:

  1. Fix window is not defined error in Node.js tests

Issue: _initialize() in GoTrueClient.ts accessed window.location.href without checking if running in a browser environment, causing Node.js tests to fail with ReferenceError: window is not defined.

Root Cause: This bug was introduced in commit f9775cf but wasn't caught until this PR added new tests that instantiate SupabaseClient directly in Node.js environment.

Fix: Wrap URL parsing in an isBrowser() check, consistent with how browser APIs are accessed elsewhere in the codebase.

// Before const params = parseParametersFromURL(window.location.href) // After let params: { [parameter: string]: string } = {} if (isBrowser()) { params = parseParametersFromURL(window.location.href) // ... check for callback types }
  1. Fix incorrect test expectations for signInWithSSO()

Issue: Test signInWithSSO() should return error shape; calling code can opt to throw expected the error to be returned instead of thrown, causing test failure.

Root Cause: The test was incorrectly written when added in commit 002886a. The test name suggested alternative behavior, but this contradicts the feature design and all other tests in the suite.

Why this is a test bug, not a code bug:

  1. Feature Design: throwOnError is a client-level setting. When throwOnError: true, ALL methods throw on error consistently via the _returnResult() helper.

  2. Test Suite Consistency: All other tests in the GoTrueClient with throwOnError option suite expect throws:

  • signUp() expects throw
  • signInWithPassword() expects throw
  • updateUser() expects throw
  • signInWithOtp() expects throw
  • signInWithSSO() expected return (incorrect)
  1. Implementation Consistency: signInWithSSO() uses _returnResult() just like all other methods - there's no special case handling.

Fix: Update test to expect throwing behavior and rename to accurately reflect what it tests.

// Before test('signInWithSSO() should return error shape; calling code can opt to throw', async () => { const res = await client.signInWithSSO({ domain: 'nonexistent.example.com' }) expect(res.error).not.toBeNull() }) // After  test('signInWithSSO() should throw on error when throwOnError is true', async () => { await expect(client.signInWithSSO({ domain: 'nonexistent.example.com' })).rejects.toThrow() })
@mandarini mandarini requested review from a team as code owners October 10, 2025 14:51
@mandarini mandarini requested a review from hf October 10, 2025 14:51
@mandarini mandarini added the auth-js Related to the auth-js library. label Oct 13, 2025
@mandarini mandarini added do-not-merge Do not merge this PR. and removed do-not-merge Do not merge this PR. labels Oct 30, 2025
@mandarini mandarini force-pushed the kiwicopple/add-throwOnError branch from 1ea1aa2 to 9c6aecd Compare October 30, 2025 13:14
@coveralls
Copy link

coveralls commented Oct 30, 2025

Coverage Status

coverage: 95.455% (-0.5%) from 95.987%
when pulling 7a7da62 on kiwicopple/add-throwOnError
into ec5a976 on master.

@mandarini mandarini requested a review from a team as a code owner October 30, 2025 14:13
@mandarini mandarini force-pushed the kiwicopple/add-throwOnError branch 2 times, most recently from b24ff82 to 7a7da62 Compare October 30, 2025 14:34
hf
hf previously approved these changes Oct 30, 2025
@mandarini mandarini removed the request for review from a team October 30, 2025 14:49
o-santi
o-santi previously approved these changes Oct 30, 2025
@mandarini mandarini self-assigned this Oct 30, 2025
@mandarini mandarini dismissed stale reviews from o-santi and hf via aa8aeea October 30, 2025 15:28
@mandarini mandarini requested review from hf and o-santi October 30, 2025 15:30
grdsdev
grdsdev previously approved these changes Oct 30, 2025
@mandarini mandarini force-pushed the kiwicopple/add-throwOnError branch from aa8aeea to 2fd8785 Compare October 31, 2025 10:39
@mandarini mandarini requested a review from a team as a code owner October 31, 2025 11:23
@mandarini mandarini requested a review from grdsdev October 31, 2025 11:23
@mandarini mandarini merged commit 7511686 into master Oct 31, 2025
26 checks passed
@mandarini mandarini deleted the kiwicopple/add-throwOnError branch October 31, 2025 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth-js Related to the auth-js library.

7 participants