17
17
18
18
import { FirebaseError , querystring } from '@firebase/util' ;
19
19
20
- import {
21
- AUTH_ERROR_FACTORY ,
22
- AuthErrorCode ,
23
- NamedErrorParams ,
24
- ERRORS
25
- } from '../core/errors' ;
26
- import { fail } from '../core/util/assert' ;
20
+ import { AuthErrorCode , NamedErrorParams } from '../core/errors' ;
21
+ import { _createError , _fail } from '../core/util/assert' ;
27
22
import { Delay } from '../core/util/delay' ;
28
23
import { _emulatorUrl } from '../core/util/emulator' ;
29
24
import { FetchProvider } from '../core/util/fetch_provider' ;
@@ -122,7 +117,7 @@ export async function _performFetchWithErrorHandling<V>(
122
117
( auth as AuthInternal ) . _canInitEmulator = false ;
123
118
const errorMap = { ...SERVER_ERROR_MAP , ...customErrorMap } ;
124
119
try {
125
- const networkTimeout = new NetworkTimeout < Response > ( auth . name ) ;
120
+ const networkTimeout = new NetworkTimeout < Response > ( auth ) ;
126
121
const response : Response = await Promise . race < Promise < Response > > ( [
127
122
fetchFn ( ) ,
128
123
networkTimeout . promise
@@ -154,21 +149,14 @@ export async function _performFetchWithErrorHandling<V>(
154
149
errorMap [ serverErrorCode ] ||
155
150
( ( serverErrorCode
156
151
. toLowerCase ( )
157
- . replace ( / _ / g, '-' ) as unknown ) as AuthErrorCode ) ;
158
- if ( authError && Object . keys ( ERRORS ) . includes ( authError ) ) {
159
- fail ( authError , { appName : auth . name } ) ;
160
- } else {
161
- // TODO probably should handle improperly formatted errors as well
162
- // If you see this, add an entry to SERVER_ERROR_MAP for the corresponding error
163
- console . error ( `Unexpected API error: ${ json . error . message } ` ) ;
164
- fail ( AuthErrorCode . INTERNAL_ERROR , { appName : auth . name } ) ;
165
- }
152
+ . replace ( / [ _ \s ] + / g, '-' ) as unknown ) as AuthErrorCode ) ;
153
+ _fail ( auth , authError ) ;
166
154
}
167
155
} catch ( e ) {
168
156
if ( e instanceof FirebaseError ) {
169
157
throw e ;
170
158
}
171
- fail ( AuthErrorCode . NETWORK_REQUEST_FAILED , { appName : auth . name } ) ;
159
+ _fail ( auth , AuthErrorCode . NETWORK_REQUEST_FAILED ) ;
172
160
}
173
161
}
174
162
@@ -187,8 +175,7 @@ export async function _performSignInRequest<T, V extends IdTokenResponse>(
187
175
customErrorMap
188
176
) ) as V ;
189
177
if ( 'mfaPendingCredential' in serverResponse ) {
190
- throw AUTH_ERROR_FACTORY . create ( AuthErrorCode . MFA_REQUIRED , {
191
- appName : auth . name ,
178
+ _fail ( auth , AuthErrorCode . MFA_REQUIRED , {
192
179
serverResponse
193
180
} ) ;
194
181
}
@@ -218,19 +205,15 @@ class NetworkTimeout<T> {
218
205
private timer : any | null = null ;
219
206
readonly promise = new Promise < T > ( ( _ , reject ) => {
220
207
this . timer = setTimeout ( ( ) => {
221
- return reject (
222
- AUTH_ERROR_FACTORY . create ( AuthErrorCode . TIMEOUT , {
223
- appName : this . appName
224
- } )
225
- ) ;
208
+ return reject ( _createError ( this . auth , AuthErrorCode . TIMEOUT ) ) ;
226
209
} , DEFAULT_API_TIMEOUT_MS . get ( ) ) ;
227
210
} ) ;
228
211
229
212
clearNetworkTimeout ( ) : void {
230
213
clearTimeout ( this . timer ) ;
231
214
}
232
215
233
- constructor ( private readonly appName : string ) { }
216
+ constructor ( private readonly auth : Auth ) { }
234
217
}
235
218
236
219
interface PotentialResponse extends IdTokenResponse {
@@ -239,12 +222,12 @@ interface PotentialResponse extends IdTokenResponse {
239
222
}
240
223
241
224
function makeTaggedError (
242
- { name } : Auth ,
225
+ auth : Auth ,
243
226
code : AuthErrorCode ,
244
227
response : PotentialResponse
245
228
) : FirebaseError {
246
229
const errorParams : NamedErrorParams = {
247
- appName : name
230
+ appName : auth . name
248
231
} ;
249
232
250
233
if ( response . email ) {
@@ -254,7 +237,7 @@ function makeTaggedError(
254
237
errorParams . phoneNumber = response . phoneNumber ;
255
238
}
256
239
257
- const error = AUTH_ERROR_FACTORY . create ( code , errorParams ) ;
240
+ const error = _createError ( auth , code , errorParams ) ;
258
241
259
242
// We know customData is defined on error because errorParams is defined
260
243
( error . customData ! as TaggedWithTokenResponse ) . _tokenResponse = response ;
0 commit comments