fix(auth): add toJSON to AuthError for correct JSON serialization#2238
fix(auth): add toJSON to AuthError for correct JSON serialization#2238mandarini merged 2 commits intosupabase:masterfrom
Conversation
@supabase/auth-js
@supabase/functions-js
@supabase/postgrest-js
@supabase/realtime-js
@supabase/storage-js
@supabase/supabase-js
commit: |
mandarini
left a comment
There was a problem hiding this comment.
Hi @oniani1, thank you so much for contributing to Supabase! 💚
This is a really nice fix. The base-class toJSON() approach is exactly the right call, and it lines up perfectly with what we did for FunctionsError (#2226) and PostgrestError (#2212). The tests are thorough and the super.toJSON() spread in the existing overrides is a clean de-duplication.
Two small things I wanted to flag before merging:
-
AuthUnknownError.originalError: with the basetoJSON(), this property won't appear in serialized output. I suspect that's fine (the original error could be circular or otherwise non-serializable), but wanted to confirm that's intentional on your end. If it is, could you add a quick test assertion thatoriginalErroris not present in the serialized output? That way nobody tries to "fix" it later without understanding the reasoning. -
codenow appears inAuthImplicitGrantRedirectErrorandAuthPKCEGrantCodeExchangeErrortoJSON output: since both passundefinedforcode, this is invisible throughJSON.stringify(undefined gets dropped). But if someone calls.toJSON()directly, they'll now seecode: undefinedin the object. Not a blocker at all, just wanted to make sure you'd considered it.
Once you've confirmed the originalError point, this is good to go.
Thank you so much for contributing to Supabase!
Adds a test assertion confirming that AuthUnknownError.originalError does not appear in JSON.stringify output, since the underlying error could be circular or otherwise non-serializable.
|
Yeah, the Added a test for that in 7af2c87. On the |
This PR updates @supabase/*-js libraries to version 2.103.2. **Source**: supabase-js-stable-release **Changes**: - Updated @supabase/supabase-js to 2.103.2 - Updated @supabase/auth-js to 2.103.2 - Updated @supabase/realtime-js to 2.103.2 - Updated @supabase/postgest-js to 2.103.2 - Refreshed pnpm-lock.yaml --- ## Release Notes ## v2.103.2 ## 2.103.2 (2026-04-15) ### 🩹 Fixes - **auth:** include Cloudflare error codes in NETWORK_ERROR_CODES ([#2239](supabase/supabase-js#2239)) - **auth:** remove Prettify wrapper from exported types for TypeDoc expansion ([#2250](supabase/supabase-js#2250)) - **misc:** add explicit return types to toJSON methods for JSR compat ([#2252](supabase/supabase-js#2252)) - **storage:** remove client-side signed URL render endpoint normalization ([#2249](supabase/supabase-js#2249)) ### ❤️ Thank You - Katerina Skroumpelou @mandarini - Vansh Sharma @Vansh1811 ## v2.103.1 ## 2.103.1 (2026-04-15) ### 🩹 Fixes - **auth:** add toJSON to AuthError for correct JSON serialization ([#2238](supabase/supabase-js#2238)) - **postgrest:** handle bigint rpc ([#2245](supabase/supabase-js#2245)) - **storage:** add toJSON to StorageError for correct JSON serialization ([#2246](supabase/supabase-js#2246)) - **storage:** apply empty transform check to download and getPublicUrl ([#2219](supabase/supabase-js#2219)) ### ❤️ Thank You - oniani1 - Vaibhav @7ttp This PR was created automatically. Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
Summary
JSON.stringify()silently drops themessagefield on most auth error classes becauseError.prototype.messageis non-enumerable.Only 2 out of 13 auth error classes (
AuthImplicitGrantRedirectErrorandAuthPKCEGrantCodeExchangeError) hadtoJSON()overrides. The rest -- includingAuthApiError, which is by far the most commonly encountered -- would serialize withoutmessage:This adds
toJSON()to theAuthErrorbase class (returningname,message,status,code), so all subclasses inherit it automatically. The two existing overrides now spreadsuper.toJSON()instead of duplicating fields.AuthWeakPasswordErrorgets an override to includereasons.Same pattern as the recent fixes for
FunctionsError(#2226) andPostgrestError(#2212).Test plan
errors.test.tscovering all 12 concrete error classesJSON.stringify()round-trip preservesmessageand all relevant fields