@@ -169,11 +169,13 @@ export class Flagsmith {
169
169
}
170
170
try {
171
171
if ( this . enableLocalEvaluation || this . offlineMode ) {
172
- const environment = await this . getEnvironment ( ) ;
173
- return this . getEnvironmentFlagsFromDocument ( environment ) ;
172
+ return await this . getEnvironmentFlagsFromDocument ( ) ;
174
173
}
175
- return this . getEnvironmentFlagsFromApi ( ) ;
174
+ return await this . getEnvironmentFlagsFromApi ( ) ;
176
175
} catch ( error ) {
176
+ if ( ! this . defaultFlagHandler ) {
177
+ throw new Error ( 'getEnvironmentFlags failed and no default flag handler was provided' , { cause : error } ) ;
178
+ }
177
179
this . logger . error ( error , 'getEnvironmentFlags failed' ) ;
178
180
return new Flags ( {
179
181
flags : { } ,
@@ -209,8 +211,7 @@ export class Flagsmith {
209
211
traits = traits || { } ;
210
212
try {
211
213
if ( this . enableLocalEvaluation || this . offlineMode ) {
212
- const environment = await this . getEnvironment ( ) ;
213
- return this . getIdentityFlagsFromDocument ( environment , identifier , traits || { } ) ;
214
+ return await this . getIdentityFlagsFromDocument ( identifier , traits || { } ) ;
214
215
}
215
216
return await this . getIdentityFlagsFromApi ( identifier , traits , transient ) ;
216
217
} catch ( error ) {
@@ -376,7 +377,8 @@ export class Flagsmith {
376
377
return buildEnvironmentModel ( environment_data ) ;
377
378
}
378
379
379
- private async getEnvironmentFlagsFromDocument ( environment : EnvironmentModel ) : Promise < Flags > {
380
+ private async getEnvironmentFlagsFromDocument ( ) : Promise < Flags > {
381
+ const environment = await this . getEnvironment ( ) ;
380
382
const flags = Flags . fromFeatureStateModels ( {
381
383
featureStates : getEnvironmentFeatureStates ( environment ) ,
382
384
analyticsProcessor : this . analyticsProcessor ,
@@ -389,10 +391,10 @@ export class Flagsmith {
389
391
}
390
392
391
393
private async getIdentityFlagsFromDocument (
392
- environment : EnvironmentModel ,
393
394
identifier : string ,
394
395
traits : { [ key : string ] : any }
395
396
) : Promise < Flags > {
397
+ const environment = await this . getEnvironment ( ) ;
396
398
const identityModel = this . getIdentityModel (
397
399
environment ,
398
400
identifier ,
@@ -422,31 +424,16 @@ export class Flagsmith {
422
424
if ( ! this . environmentFlagsUrl ) {
423
425
throw new Error ( '`apiUrl` argument is missing or invalid.' ) ;
424
426
}
425
- try {
426
- const apiFlags = await this . getJSONResponse ( this . environmentFlagsUrl , 'GET' ) ;
427
- const flags = Flags . fromAPIFlags ( {
428
- apiFlags : apiFlags ,
429
- analyticsProcessor : this . analyticsProcessor ,
430
- defaultFlagHandler : this . defaultFlagHandler
431
- } ) ;
432
- if ( ! ! this . cache ) {
433
- await this . cache . set ( 'flags' , flags ) ;
434
- }
435
- return flags ;
436
- } catch ( e ) {
437
- if ( this . offlineHandler ) {
438
- const environment = this . offlineHandler . getEnvironment ( ) ;
439
- return this . getEnvironmentFlagsFromDocument ( environment ) ;
440
- }
441
- if ( this . defaultFlagHandler ) {
442
- return new Flags ( {
443
- flags : { } ,
444
- defaultFlagHandler : this . defaultFlagHandler
445
- } ) ;
446
- }
447
-
448
- throw e ;
427
+ const apiFlags = await this . getJSONResponse ( this . environmentFlagsUrl , 'GET' ) ;
428
+ const flags = Flags . fromAPIFlags ( {
429
+ apiFlags : apiFlags ,
430
+ analyticsProcessor : this . analyticsProcessor ,
431
+ defaultFlagHandler : this . defaultFlagHandler
432
+ } ) ;
433
+ if ( ! ! this . cache ) {
434
+ await this . cache . set ( 'flags' , flags ) ;
449
435
}
436
+ return flags ;
450
437
}
451
438
452
439
private async getIdentityFlagsFromApi (
0 commit comments