@@ -169,11 +169,13 @@ export class Flagsmith {
169169 }
170170 try {
171171 if ( this . enableLocalEvaluation || this . offlineMode ) {
172- const environment = await this . getEnvironment ( ) ;
173- return this . getEnvironmentFlagsFromDocument ( environment ) ;
172+ return await this . getEnvironmentFlagsFromDocument ( ) ;
174173 }
175- return this . getEnvironmentFlagsFromApi ( ) ;
174+ return await this . getEnvironmentFlagsFromApi ( ) ;
176175 } catch ( error ) {
176+ if ( ! this . defaultFlagHandler ) {
177+ throw new Error ( 'getEnvironmentFlags failed and no default flag handler was provided' , { cause : error } ) ;
178+ }
177179 this . logger . error ( error , 'getEnvironmentFlags failed' ) ;
178180 return new Flags ( {
179181 flags : { } ,
@@ -209,8 +211,7 @@ export class Flagsmith {
209211 traits = traits || { } ;
210212 try {
211213 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 || { } ) ;
214215 }
215216 return await this . getIdentityFlagsFromApi ( identifier , traits , transient ) ;
216217 } catch ( error ) {
@@ -376,7 +377,8 @@ export class Flagsmith {
376377 return buildEnvironmentModel ( environment_data ) ;
377378 }
378379
379- private async getEnvironmentFlagsFromDocument ( environment : EnvironmentModel ) : Promise < Flags > {
380+ private async getEnvironmentFlagsFromDocument ( ) : Promise < Flags > {
381+ const environment = await this . getEnvironment ( ) ;
380382 const flags = Flags . fromFeatureStateModels ( {
381383 featureStates : getEnvironmentFeatureStates ( environment ) ,
382384 analyticsProcessor : this . analyticsProcessor ,
@@ -389,10 +391,10 @@ export class Flagsmith {
389391 }
390392
391393 private async getIdentityFlagsFromDocument (
392- environment : EnvironmentModel ,
393394 identifier : string ,
394395 traits : { [ key : string ] : any }
395396 ) : Promise < Flags > {
397+ const environment = await this . getEnvironment ( ) ;
396398 const identityModel = this . getIdentityModel (
397399 environment ,
398400 identifier ,
@@ -422,31 +424,16 @@ export class Flagsmith {
422424 if ( ! this . environmentFlagsUrl ) {
423425 throw new Error ( '`apiUrl` argument is missing or invalid.' ) ;
424426 }
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 ) ;
449435 }
436+ return flags ;
450437 }
451438
452439 private async getIdentityFlagsFromApi (
0 commit comments