11import { inBrowser } from '@clerk/shared/browser' ;
2- import { type ClerkError , ClerkWebAuthnError } from '@clerk/shared/error' ;
2+ import { type ClerkError , ClerkRuntimeError , ClerkWebAuthnError } from '@clerk/shared/error' ;
33import { Poller } from '@clerk/shared/poller' ;
44import type {
55 AttemptFirstFactorParams ,
@@ -687,17 +687,18 @@ class SignInFuture implements SignInFutureResource {
687687 }
688688
689689 async sendResetPasswordEmailCode ( ) : Promise < { error : ClerkError | null } > {
690+ if ( ! this . resource . id ) {
691+ throw new Error ( 'Cannot reset password without a sign in.' ) ;
692+ }
690693 return runAsyncResourceTask ( this . resource , async ( ) => {
691- if ( ! this . resource . id ) {
692- throw new Error ( 'Cannot reset password without a sign in.' ) ;
693- }
694-
695694 const resetPasswordEmailCodeFactor = this . resource . supportedFirstFactors ?. find (
696695 f => f . strategy === 'reset_password_email_code' ,
697696 ) ;
698697
699698 if ( ! resetPasswordEmailCodeFactor ) {
700- throw new Error ( 'Reset password email code factor not found' ) ;
699+ throw new ClerkRuntimeError ( 'Reset password email code factor not found' , {
700+ code : 'factor_not_found' ,
701+ } ) ;
701702 }
702703
703704 const { emailAddressId } = resetPasswordEmailCodeFactor ;
@@ -784,7 +785,7 @@ class SignInFuture implements SignInFutureResource {
784785
785786 const emailCodeFactor = this . selectFirstFactor ( { strategy : 'email_code' , emailAddressId } ) ;
786787 if ( ! emailCodeFactor ) {
787- throw new Error ( 'Email code factor not found' ) ;
788+ throw new ClerkRuntimeError ( 'Email code factor not found' , { code : 'factor_not_found' } ) ;
788789 }
789790
790791 await this . resource . __internal_basePost ( {
@@ -825,7 +826,7 @@ class SignInFuture implements SignInFutureResource {
825826
826827 const emailLinkFactor = this . selectFirstFactor ( { strategy : 'email_link' , emailAddressId } ) ;
827828 if ( ! emailLinkFactor ) {
828- throw new Error ( 'Email link factor not found' ) ;
829+ throw new ClerkRuntimeError ( 'Email link factor not found' , { code : 'factor_not_found' } ) ;
829830 }
830831
831832 let absoluteVerificationUrl = verificationUrl ;
@@ -888,7 +889,7 @@ class SignInFuture implements SignInFutureResource {
888889
889890 const phoneCodeFactor = this . selectFirstFactor ( { strategy : 'phone_code' , phoneNumberId } ) ;
890891 if ( ! phoneCodeFactor ) {
891- throw new Error ( 'Phone code factor not found' ) ;
892+ throw new ClerkRuntimeError ( 'Phone code factor not found' , { code : 'factor_not_found' } ) ;
892893 }
893894
894895 await this . resource . __internal_basePost ( {
@@ -993,7 +994,7 @@ class SignInFuture implements SignInFutureResource {
993994 f => f . strategy === strategy ,
994995 ) as Web3SignatureFactor ;
995996 if ( ! web3FirstFactor ) {
996- throw new Error ( 'Web3 first factor not found' ) ;
997+ throw new ClerkRuntimeError ( 'Web3 first factor not found' , { code : 'factor_not_found' } ) ;
997998 }
998999
9991000 await this . resource . __internal_basePost ( {
@@ -1003,7 +1004,7 @@ class SignInFuture implements SignInFutureResource {
10031004
10041005 const { message } = this . firstFactorVerification ;
10051006 if ( ! message ) {
1006- throw new Error ( 'Web3 nonce not found' ) ;
1007+ throw new ClerkRuntimeError ( 'Web3 nonce not found' , { code : 'web3_nonce_not_found' } ) ;
10071008 }
10081009
10091010 let signature : string ;
@@ -1056,7 +1057,7 @@ class SignInFuture implements SignInFutureResource {
10561057 const passKeyFactor = this . supportedFirstFactors . find ( f => f . strategy === 'passkey' ) as PasskeyFactor ;
10571058
10581059 if ( ! passKeyFactor ) {
1059- clerkVerifyPasskeyCalledBeforeCreate ( ) ;
1060+ throw new ClerkRuntimeError ( 'Passkey factor not found' , { code : 'factor_not_found' } ) ;
10601061 }
10611062 await this . resource . __internal_basePost ( {
10621063 body : { strategy : 'passkey' } ,
@@ -1068,7 +1069,7 @@ class SignInFuture implements SignInFutureResource {
10681069 const publicKeyOptions = nonce ? convertJSONToPublicKeyRequestOptions ( JSON . parse ( nonce ) ) : null ;
10691070
10701071 if ( ! publicKeyOptions ) {
1071- clerkMissingWebAuthnPublicKeyOptions ( 'get' ) ;
1072+ throw new ClerkRuntimeError ( 'Missing public key options' , { code : 'missing_public_key_options' } ) ;
10721073 }
10731074
10741075 let canUseConditionalUI = false ;
@@ -1088,7 +1089,7 @@ class SignInFuture implements SignInFutureResource {
10881089 } ) ;
10891090
10901091 if ( ! publicKeyCredential ) {
1091- throw error ;
1092+ throw new ClerkWebAuthnError ( error . message , { code : 'passkey_retrieval_failed' } ) ;
10921093 }
10931094
10941095 await this . resource . __internal_basePost ( {
@@ -1106,7 +1107,7 @@ class SignInFuture implements SignInFutureResource {
11061107 const phoneCodeFactor = this . resource . supportedSecondFactors ?. find ( f => f . strategy === 'phone_code' ) ;
11071108
11081109 if ( ! phoneCodeFactor ) {
1109- throw new Error ( 'Phone code factor not found' ) ;
1110+ throw new ClerkRuntimeError ( 'Phone code factor not found' , { code : 'factor_not_found' } ) ;
11101111 }
11111112
11121113 const { phoneNumberId } = phoneCodeFactor ;
@@ -1154,11 +1155,12 @@ class SignInFuture implements SignInFutureResource {
11541155
11551156 async finalize ( params ?: SignInFutureFinalizeParams ) : Promise < { error : ClerkError | null } > {
11561157 const { navigate } = params || { } ;
1157- return runAsyncResourceTask ( this . resource , async ( ) => {
1158- if ( ! this . resource . createdSessionId ) {
1159- throw new Error ( 'Cannot finalize sign-in without a created session.' ) ;
1160- }
11611158
1159+ if ( ! this . resource . createdSessionId ) {
1160+ throw new Error ( 'Cannot finalize sign-in without a created session.' ) ;
1161+ }
1162+
1163+ return runAsyncResourceTask ( this . resource , async ( ) => {
11621164 // Reload the client to prevent an issue where the created session is not picked up.
11631165 await SignIn . clerk . client ?. reload ( ) ;
11641166
0 commit comments