From 4d057a790fac981c5b9142251bf3e696b79e04de Mon Sep 17 00:00:00 2001 From: patrickmann Date: Mon, 18 Nov 2024 12:50:16 +0100 Subject: [PATCH] improve error handling --- .../inputs/InputDiagnosticService.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/graylog2-server/src/main/java/org/graylog2/inputs/InputDiagnosticService.java b/graylog2-server/src/main/java/org/graylog2/inputs/InputDiagnosticService.java index 11547cc53712..09e99fe9351b 100644 --- a/graylog2-server/src/main/java/org/graylog2/inputs/InputDiagnosticService.java +++ b/graylog2-server/src/main/java/org/graylog2/inputs/InputDiagnosticService.java @@ -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; @@ -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); @@ -77,9 +79,10 @@ public InputDiagnostics getInputDiagnostics( final Set 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); @@ -126,17 +129,18 @@ private Search buildSearch(Input input) { private static AbstractMap.SimpleEntry 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); } - }