Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmann committed Nov 18, 2024
1 parent 16ecaa0 commit 4d057a7
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.ImmutableSet;
import jakarta.inject.Inject;
import jakarta.ws.rs.InternalServerErrorException;
import org.apache.commons.lang3.StringUtils;
import org.graylog.plugins.views.search.Query;
import org.graylog.plugins.views.search.QueryResult;
Expand Down Expand Up @@ -51,6 +52,7 @@

import static org.graylog2.plugin.Message.FIELD_GL2_SOURCE_INPUT;
import static org.graylog2.rest.models.system.inputs.responses.InputDiagnostics.EMPTY_DIAGNOSTICS;
import static org.graylog2.shared.utilities.StringUtils.f;

public class InputDiagnosticService {
private static final Logger LOG = LoggerFactory.getLogger(InputDiagnosticService.class);
Expand All @@ -77,9 +79,10 @@ public InputDiagnostics getInputDiagnostics(

final Set<SearchError> errors = queryResult.errors();
if (errors != null && !errors.isEmpty()) {
LOG.error("An error occurred while executing aggregation: {}",
String errorMsg = f("An error occurred while executing aggregation: %s",
errors.stream().map(SearchError::description).collect(Collectors.joining(", ")));
return EMPTY_DIAGNOSTICS;
LOG.error(errorMsg);
throw new InternalServerErrorException(errorMsg);
}

final SearchType.Result aggregationResult = queryResult.searchTypes().get(PIVOT_ID);
Expand Down Expand Up @@ -126,17 +129,18 @@ private Search buildSearch(Input input) {

private static AbstractMap.SimpleEntry<String, Long> extractValues(PivotResult.Row r) {
if (r.values().size() != 1) {
LOG.warn("Expected 1 value in aggregation result, but received [{}].", r.values().size());
return null;
String errorMsg = f("Expected 1 value in aggregation result, but received [%d].", r.values().size());
LOG.warn(errorMsg);
throw new InternalServerErrorException(errorMsg);
}
final String streamId = r.key().get(0);
if (StringUtils.isEmpty(streamId)) {
LOG.warn("Expected a stream ID to be returned.");
return null;
String errorMsg = "Unable to retrieve stream ID from query result";
LOG.warn(errorMsg);
throw new InternalServerErrorException(errorMsg);
}

final Long count = (Long) r.values().get(0).value();
return new AbstractMap.SimpleEntry<>(streamId, count);
}

}

0 comments on commit 4d057a7

Please sign in to comment.