Skip to content

Commit

Permalink
swallow all exceptions that can occurr when deserializing
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jun 21, 2024
1 parent 88daff2 commit 72cb396
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/
package com.redhat.devtools.intellij.telemetry.core.configuration.limits;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import com.redhat.devtools.intellij.telemetry.core.service.Event;
import org.jetbrains.annotations.NotNull;
Expand All @@ -27,6 +28,8 @@

public class EventLimits implements IEventLimits {

private static final Logger LOGGER = Logger.getInstance(EventLimits.class);

static final Duration DEFAULT_REFRESH_PERIOD = Duration.ofHours(6);
private final String pluginId;
private final PluginLimitsFactory factory;
Expand Down Expand Up @@ -102,7 +105,7 @@ List<PluginLimits> getAllLimits() {
FileTime lastModified = configuration.getLocalLastModified();
if (this.limits == null
|| needsRefresh(refreshAfter, lastModified)) {
this.limits = createLimits(configuration.getRemote(), factory);
this.limits = createLimits(Configurations.REMOTE, configuration.getRemote(), factory);
}
return limits;
}
Expand Down Expand Up @@ -148,14 +151,15 @@ private Duration getRefreshAfter(PluginLimits defaults) {
return Duration.ofHours(defaults.getRefresh());
}

private List<PluginLimits> createLimits(String config, PluginLimitsFactory factory) {
private List<PluginLimits> createLimits(String path, String config, PluginLimitsFactory factory) {
try {
if (StringUtil.isEmptyOrSpaces(config)) {
// null to trigger reload upon next #getAllLimits
return null;
}
return factory.create(config);
} catch (Exception e) {
LOGGER.warn("Could not deserialize config " + path, e);
// null to trigger reload upon next #getAllLimits
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static String getStringValue(String name, JsonNode node) {
private static int getIntValue(String name, JsonNode node) {
int numeric = DEFAULT_NUMERIC_VALUE;
if (node != null
&& node.get(name) == null) {
&& node.get(name) != null) {
numeric = node.get(name).asInt(DEFAULT_NUMERIC_VALUE);
}
return numeric;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,32 @@ public void getIncludes_should_return_event_name_filter_with_negative_daily_limi
assertThat(dailyLimit).isEqualTo(-1);
}

@Test
public void getIncludes_should_return_event_name_filter_with_negative_daily_limit_if_daily_limit_is_not_specified() throws JsonProcessingException {
// given
String config =
"{\n" +
" \"*\": {\n" +
" \"includes\": [\n" +
" {\n" +
" \"name\" : \"yoda\"\n" + // dailyLimit missing
" }\n" +
" ]\n" +
" }" +
"}";
List<PluginLimits> limits = PluginLimitsDeserialization.create(config);
PluginLimits limit = limits.get(0); // *
List<Filter> includes = limit.getIncludes();
assertThat(includes).hasSize(1);
Filter filter = includes.get(0);
assertThat(filter).isExactlyInstanceOf(Filter.EventNameFilter.class);
Filter.EventNameFilter nameFilter = (Filter.EventNameFilter) filter;
// when
int dailyLimit = nameFilter.getDailyLimit();
// then
assertThat(dailyLimit).isEqualTo(-1);
}

@Test
public void getIncludes_should_return_1_event_name_filter_with_ratio_and_daily_limit() throws JsonProcessingException {
// given
Expand Down

0 comments on commit 72cb396

Please sign in to comment.