11import { WeakPasswordReasons } from './types'
22import { ErrorCode } from './error-codes'
33
4+ /**
5+ * Base error thrown by Supabase Auth helpers.
6+ *
7+ * @example
8+ * ```ts
9+ * import { AuthError } from '@supabase/auth-js'
10+ *
11+ * throw new AuthError('Unexpected auth error', 500, 'unexpected')
12+ * ```
13+ */
414export class AuthError extends Error {
515 /**
616 * Error code associated with the error. Most errors coming from
@@ -27,6 +37,16 @@ export function isAuthError(error: unknown): error is AuthError {
2737 return typeof error === 'object' && error !== null && '__isAuthError' in error
2838}
2939
40+ /**
41+ * Error returned directly from the GoTrue REST API.
42+ *
43+ * @example
44+ * ```ts
45+ * import { AuthApiError } from '@supabase/auth-js'
46+ *
47+ * throw new AuthApiError('Invalid credentials', 400, 'invalid_credentials')
48+ * ```
49+ */
3050export class AuthApiError extends AuthError {
3151 status : number
3252
@@ -42,6 +62,20 @@ export function isAuthApiError(error: unknown): error is AuthApiError {
4262 return isAuthError ( error ) && error . name === 'AuthApiError'
4363}
4464
65+ /**
66+ * Wraps non-standard errors so callers can inspect the root cause.
67+ *
68+ * @example
69+ * ```ts
70+ * import { AuthUnknownError } from '@supabase/auth-js'
71+ *
72+ * try {
73+ * await someAuthCall()
74+ * } catch (err) {
75+ * throw new AuthUnknownError('Auth failed', err)
76+ * }
77+ * ```
78+ */
4579export class AuthUnknownError extends AuthError {
4680 originalError : unknown
4781
@@ -52,6 +86,16 @@ export class AuthUnknownError extends AuthError {
5286 }
5387}
5488
89+ /**
90+ * Flexible error class used to create named auth errors at runtime.
91+ *
92+ * @example
93+ * ```ts
94+ * import { CustomAuthError } from '@supabase/auth-js'
95+ *
96+ * throw new CustomAuthError('My custom auth error', 'MyAuthError', 400, 'custom_code')
97+ * ```
98+ */
5599export class CustomAuthError extends AuthError {
56100 name : string
57101 status : number
@@ -63,6 +107,16 @@ export class CustomAuthError extends AuthError {
63107 }
64108}
65109
110+ /**
111+ * Error thrown when an operation requires a session but none is present.
112+ *
113+ * @example
114+ * ```ts
115+ * import { AuthSessionMissingError } from '@supabase/auth-js'
116+ *
117+ * throw new AuthSessionMissingError()
118+ * ```
119+ */
66120export class AuthSessionMissingError extends CustomAuthError {
67121 constructor ( ) {
68122 super ( 'Auth session missing!' , 'AuthSessionMissingError' , 400 , undefined )
@@ -73,18 +127,51 @@ export function isAuthSessionMissingError(error: any): error is AuthSessionMissi
73127 return isAuthError ( error ) && error . name === 'AuthSessionMissingError'
74128}
75129
130+ /**
131+ * Error thrown when the token response is malformed.
132+ *
133+ * @example
134+ * ```ts
135+ * import { AuthInvalidTokenResponseError } from '@supabase/auth-js'
136+ *
137+ * throw new AuthInvalidTokenResponseError()
138+ * ```
139+ */
76140export class AuthInvalidTokenResponseError extends CustomAuthError {
77141 constructor ( ) {
78142 super ( 'Auth session or user missing' , 'AuthInvalidTokenResponseError' , 500 , undefined )
79143 }
80144}
81145
146+ /**
147+ * Error thrown when email/password credentials are invalid.
148+ *
149+ * @example
150+ * ```ts
151+ * import { AuthInvalidCredentialsError } from '@supabase/auth-js'
152+ *
153+ * throw new AuthInvalidCredentialsError('Email or password is incorrect')
154+ * ```
155+ */
82156export class AuthInvalidCredentialsError extends CustomAuthError {
83157 constructor ( message : string ) {
84158 super ( message , 'AuthInvalidCredentialsError' , 400 , undefined )
85159 }
86160}
87161
162+ /**
163+ * Error thrown when implicit grant redirects contain an error.
164+ *
165+ * @example
166+ * ```ts
167+ * import { AuthImplicitGrantRedirectError } from '@supabase/auth-js'
168+ *
169+ * throw new AuthImplicitGrantRedirectError('OAuth redirect failed', {
170+ * error: 'access_denied',
171+ * code: 'oauth_error',
172+ * })
173+ * ```
174+ */
88175export class AuthImplicitGrantRedirectError extends CustomAuthError {
89176 details : { error : string ; code : string } | null = null
90177 constructor ( message : string , details : { error : string ; code : string } | null = null ) {
@@ -108,6 +195,16 @@ export function isAuthImplicitGrantRedirectError(
108195 return isAuthError ( error ) && error . name === 'AuthImplicitGrantRedirectError'
109196}
110197
198+ /**
199+ * Error thrown during PKCE code exchanges.
200+ *
201+ * @example
202+ * ```ts
203+ * import { AuthPKCEGrantCodeExchangeError } from '@supabase/auth-js'
204+ *
205+ * throw new AuthPKCEGrantCodeExchangeError('PKCE exchange failed')
206+ * ```
207+ */
111208export class AuthPKCEGrantCodeExchangeError extends CustomAuthError {
112209 details : { error : string ; code : string } | null = null
113210
@@ -126,6 +223,16 @@ export class AuthPKCEGrantCodeExchangeError extends CustomAuthError {
126223 }
127224}
128225
226+ /**
227+ * Error thrown when a transient fetch issue occurs.
228+ *
229+ * @example
230+ * ```ts
231+ * import { AuthRetryableFetchError } from '@supabase/auth-js'
232+ *
233+ * throw new AuthRetryableFetchError('Service temporarily unavailable', 503)
234+ * ```
235+ */
129236export class AuthRetryableFetchError extends CustomAuthError {
130237 constructor ( message : string , status : number ) {
131238 super ( message , 'AuthRetryableFetchError' , status , undefined )
@@ -141,6 +248,16 @@ export function isAuthRetryableFetchError(error: unknown): error is AuthRetryabl
141248 * weak. Inspect the reasons to identify what password strength rules are
142249 * inadequate.
143250 */
251+ /**
252+ * Error thrown when a supplied password is considered weak.
253+ *
254+ * @example
255+ * ```ts
256+ * import { AuthWeakPasswordError } from '@supabase/auth-js'
257+ *
258+ * throw new AuthWeakPasswordError('Password too short', 400, ['min_length'])
259+ * ```
260+ */
144261export class AuthWeakPasswordError extends CustomAuthError {
145262 /**
146263 * Reasons why the password is deemed weak.
@@ -158,6 +275,16 @@ export function isAuthWeakPasswordError(error: unknown): error is AuthWeakPasswo
158275 return isAuthError ( error ) && error . name === 'AuthWeakPasswordError'
159276}
160277
278+ /**
279+ * Error thrown when a JWT cannot be verified or parsed.
280+ *
281+ * @example
282+ * ```ts
283+ * import { AuthInvalidJwtError } from '@supabase/auth-js'
284+ *
285+ * throw new AuthInvalidJwtError('Token signature is invalid')
286+ * ```
287+ */
161288export class AuthInvalidJwtError extends CustomAuthError {
162289 constructor ( message : string ) {
163290 super ( message , 'AuthInvalidJwtError' , 400 , 'invalid_jwt' )
0 commit comments