Skip to content

Commit

Permalink
Make ConfigJsonIsNotPresent error handling consistent (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 authored Apr 25, 2023
1 parent b918d6d commit dc8c493
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
19 changes: 15 additions & 4 deletions lib/configcat/configcatclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def get_value_details(key, default_value, user = nil)
def get_all_keys
settings, _ = _get_settings()
if settings === nil
@log.error(1000, "Config JSON is not present. Returning empty list.")
return []
end
return settings.keys
Expand Down Expand Up @@ -174,9 +175,14 @@ def get_all_variation_ids(user = nil)
ConfigCat.logger.warn("get_all_variation_ids is deprecated and will be removed in a future major version. " \
"Please use [get_value_details] instead.")

keys = get_all_keys()
settings, _ = _get_settings()
if settings === nil
@log.error(1000, "Config JSON is not present. Returning empty list.")
return []
end

variation_ids = []
for key in keys
for key in settings.keys
variation_id = get_variation_id(key, nil, user)
if !variation_id.equal?(nil)
variation_ids.push(variation_id)
Expand Down Expand Up @@ -224,9 +230,14 @@ def get_key_and_value(variation_id)
# :param user [User] the user object to identify the caller.
# :return dictionary of values
def get_all_values(user = nil)
keys = get_all_keys()
settings, _ = _get_settings()
if settings === nil
@log.error(1000, "Config JSON is not present. Returning empty dictionary.")
return {}
end

all_values = {}
for key in keys
for key in settings.keys
value = get_value(key, nil, user)
if !value.equal?(nil)
all_values[key] = value
Expand Down
13 changes: 10 additions & 3 deletions lib/configcat/configservice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def initialize(sdk_key, polling_mode, hooks, config_fetcher, log, config_cache,
def get_settings
if @polling_mode.is_a?(LazyLoadingMode)
entry, _ = fetch_if_older(Utils.get_utc_now_seconds_since_epoch - @polling_mode.cache_refresh_interval_seconds)
return entry.config[FEATURE_FLAGS], entry.fetch_time
return !entry.empty? ?
[entry.config.fetch(FEATURE_FLAGS, {}), entry.fetch_time] :
[nil, Utils::DISTANT_PAST]

elsif @polling_mode.is_a?(AutoPollingMode) && !@initialized.set?
elapsed_time = Utils.get_utc_now_seconds_since_epoch - @start_time # Elapsed time in seconds
if elapsed_time < @polling_mode.max_init_wait_time_seconds
Expand All @@ -43,13 +46,17 @@ def get_settings
# Max wait time expired without result, notify subscribers with the cached config.
if !@initialized.set?
set_initialized
return @cached_entry.config[FEATURE_FLAGS], @cached_entry.fetch_time
return !@cached_entry.empty? ?
[@cached_entry.config.fetch(FEATURE_FLAGS, {}), @cached_entry.fetch_time] :
[nil, Utils::DISTANT_PAST]
end
end
end

entry, _ = fetch_if_older(Utils::DISTANT_PAST, prefer_cache: true)
return entry.config[FEATURE_FLAGS], entry.fetch_time
return !entry.empty? ?
[entry.config.fetch(FEATURE_FLAGS, {}), entry.fetch_time] :
[nil, Utils::DISTANT_PAST]
end

# :return [RefreshResult]
Expand Down

0 comments on commit dc8c493

Please sign in to comment.