Skip to content

Commit

Permalink
Refactor handling of InvalidRangeFormatException
Browse files Browse the repository at this point in the history
This changeset adds InvalidRangeFormatExceptionMapper and removes the individual
handling of InvalidRangeFormatExceptions throughout the code.
  • Loading branch information
Jochen Schalanda committed Jan 13, 2015
1 parent 3860c6e commit c84d610
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 155 deletions.
Expand Up @@ -20,7 +20,7 @@
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject; import com.google.inject.assistedinject.AssistedInject;
import org.graylog2.alerts.AbstractAlertCondition; import org.graylog2.alerts.AbstractAlertCondition;
import org.graylog2.indexer.IndexHelper; import org.graylog2.indexer.InvalidRangeFormatException;
import org.graylog2.indexer.results.FieldStatsResult; import org.graylog2.indexer.results.FieldStatsResult;
import org.graylog2.indexer.results.ResultMessage; import org.graylog2.indexer.results.ResultMessage;
import org.graylog2.indexer.searches.Searches; import org.graylog2.indexer.searches.Searches;
Expand Down Expand Up @@ -158,7 +158,7 @@ protected CheckResult runCheck() {
// cannot happen lol // cannot happen lol
LOG.error("Invalid timerange.", e); LOG.error("Invalid timerange.", e);
return null; return null;
} catch (IndexHelper.InvalidRangeFormatException e) { } catch (InvalidRangeFormatException e) {
// lol same here // lol same here
LOG.error("Invalid timerange format.", e); LOG.error("Invalid timerange format.", e);
return null; return null;
Expand Down
Expand Up @@ -20,7 +20,7 @@
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject; import com.google.inject.assistedinject.AssistedInject;
import org.graylog2.alerts.AbstractAlertCondition; import org.graylog2.alerts.AbstractAlertCondition;
import org.graylog2.indexer.IndexHelper; import org.graylog2.indexer.InvalidRangeFormatException;
import org.graylog2.indexer.results.CountResult; import org.graylog2.indexer.results.CountResult;
import org.graylog2.indexer.results.ResultMessage; import org.graylog2.indexer.results.ResultMessage;
import org.graylog2.indexer.results.SearchResult; import org.graylog2.indexer.results.SearchResult;
Expand Down Expand Up @@ -116,7 +116,7 @@ protected CheckResult runCheck() {
// cannot happen lol // cannot happen lol
LOG.error("Invalid timerange.", e); LOG.error("Invalid timerange.", e);
return null; return null;
} catch (IndexHelper.InvalidRangeFormatException e) { } catch (InvalidRangeFormatException e) {
// lol same here // lol same here
LOG.error("Invalid timerange format.", e); LOG.error("Invalid timerange format.", e);
return null; return null;
Expand Down
Expand Up @@ -18,7 +18,7 @@


import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.graylog2.indexer.IndexHelper; import org.graylog2.indexer.InvalidRangeFormatException;
import org.graylog2.indexer.results.HistogramResult; import org.graylog2.indexer.results.HistogramResult;
import org.graylog2.indexer.searches.Searches; import org.graylog2.indexer.searches.Searches;
import org.graylog2.indexer.searches.timeranges.TimeRange; import org.graylog2.indexer.searches.timeranges.TimeRange;
Expand Down Expand Up @@ -99,10 +99,6 @@ protected ComputationResult compute() {
String msg = "Could not calculate [" + this.getClass().getCanonicalName() + "] widget <" + getId() + ">. Not a numeric field? The field was [" + config.get("field") + "]"; String msg = "Could not calculate [" + this.getClass().getCanonicalName() + "] widget <" + getId() + ">. Not a numeric field? The field was [" + config.get("field") + "]";
LOG.error(msg, e); LOG.error(msg, e);
throw new RuntimeException(msg); throw new RuntimeException(msg);
} catch (IndexHelper.InvalidRangeFormatException e) {
String msg = "Could not calculate [" + this.getClass().getCanonicalName() + "] widget <" + getId() + ">. Invalid time range.";
LOG.error(msg, e);
throw new RuntimeException(msg);
} }
} }


Expand Down
Expand Up @@ -19,7 +19,6 @@
import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.graylog2.indexer.IndexHelper;
import org.graylog2.indexer.results.TermsResult; import org.graylog2.indexer.results.TermsResult;
import org.graylog2.indexer.searches.Searches; import org.graylog2.indexer.searches.Searches;
import org.graylog2.indexer.searches.timeranges.TimeRange; import org.graylog2.indexer.searches.timeranges.TimeRange;
Expand Down Expand Up @@ -87,21 +86,15 @@ protected ComputationResult compute() {
filter = "streams:" + streamId; filter = "streams:" + streamId;
} }


try { final TermsResult terms = searches.terms(field, 50, query, filter, timeRange);
final TermsResult terms = searches.terms(field, 50, query, filter, timeRange);


Map<String, Object> result = Maps.newHashMap(); Map<String, Object> result = Maps.newHashMap();
result.put("terms", terms.getTerms()); result.put("terms", terms.getTerms());
result.put("total", terms.getTotal()); result.put("total", terms.getTotal());
result.put("other", terms.getOther()); result.put("other", terms.getOther());
result.put("missing", terms.getMissing()); result.put("missing", terms.getMissing());


return new ComputationResult(result, terms.took().millis()); return new ComputationResult(result, terms.took().millis());
} catch (IndexHelper.InvalidRangeFormatException e) {
String msg = "Could not calculate [" + this.getClass().getCanonicalName() + "] widget <" + getId() + ">. Invalid time range.";
LOG.error(msg, e);
throw new RuntimeException(msg);
}
} }


private boolean checkConfig(Map<String, Object> config) { private boolean checkConfig(Map<String, Object> config) {
Expand Down
Expand Up @@ -18,7 +18,6 @@


import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.graylog2.indexer.IndexHelper;
import org.graylog2.indexer.results.HistogramResult; import org.graylog2.indexer.results.HistogramResult;
import org.graylog2.indexer.searches.Searches; import org.graylog2.indexer.searches.Searches;
import org.graylog2.indexer.searches.timeranges.TimeRange; import org.graylog2.indexer.searches.timeranges.TimeRange;
Expand Down Expand Up @@ -90,11 +89,7 @@ protected ComputationResult compute() {
filter = "streams:" + streamId; filter = "streams:" + streamId;
} }


try { HistogramResult histogram = searches.histogram(query, interval, filter, timeRange);
HistogramResult histogram = searches.histogram(query, interval, filter, timeRange); return new ComputationResult(histogram.getResults(), histogram.took().millis(), histogram.getHistogramBoundaries());
return new ComputationResult(histogram.getResults(), histogram.took().millis(), histogram.getHistogramBoundaries());
} catch (IndexHelper.InvalidRangeFormatException e) {
throw new RuntimeException("Invalid timerange format.", e);
}
} }
} }
Expand Up @@ -19,7 +19,6 @@
import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.graylog2.indexer.IndexHelper;
import org.graylog2.indexer.results.CountResult; import org.graylog2.indexer.results.CountResult;
import org.graylog2.indexer.searches.Searches; import org.graylog2.indexer.searches.Searches;
import org.graylog2.indexer.searches.timeranges.AbsoluteRange; import org.graylog2.indexer.searches.timeranges.AbsoluteRange;
Expand Down Expand Up @@ -76,25 +75,21 @@ protected ComputationResult compute() {
} }


protected ComputationResult computeInternal(String filter) { protected ComputationResult computeInternal(String filter) {
try { CountResult cr = searches.count(query, timeRange, filter);
CountResult cr = searches.count(query, timeRange, filter); if (trend && timeRange instanceof RelativeRange) {
if (trend && timeRange instanceof RelativeRange) { DateTime toPrevious = timeRange.getFrom();
DateTime toPrevious = timeRange.getFrom(); DateTime fromPrevious = toPrevious.minus(Seconds.seconds(((RelativeRange) timeRange).getRange()));
DateTime fromPrevious = toPrevious.minus(Seconds.seconds(((RelativeRange) timeRange).getRange())); TimeRange previousTimeRange = new AbsoluteRange(fromPrevious, toPrevious);
TimeRange previousTimeRange = new AbsoluteRange(fromPrevious, toPrevious); CountResult previousCr = searches.count(query, previousTimeRange);
CountResult previousCr = searches.count(query, previousTimeRange);


Map<String, Object> results = Maps.newHashMap(); Map<String, Object> results = Maps.newHashMap();
results.put("now", cr.getCount()); results.put("now", cr.getCount());
results.put("previous", previousCr.getCount()); results.put("previous", previousCr.getCount());
long tookMs = cr.getTookMs() + previousCr.getTookMs(); long tookMs = cr.getTookMs() + previousCr.getTookMs();


return new ComputationResult(results, tookMs); return new ComputationResult(results, tookMs);
} else { } else {
return new ComputationResult(cr.getCount(), cr.getTookMs()); return new ComputationResult(cr.getCount(), cr.getTookMs());
}
} catch (IndexHelper.InvalidRangeFormatException e) {
throw new RuntimeException("Invalid timerange format.", e);
} }
} }
} }
Expand Up @@ -19,7 +19,6 @@
import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.graylog2.indexer.IndexHelper;
import org.graylog2.indexer.results.FieldStatsResult; import org.graylog2.indexer.results.FieldStatsResult;
import org.graylog2.indexer.searches.Searches; import org.graylog2.indexer.searches.Searches;
import org.graylog2.indexer.searches.timeranges.AbsoluteRange; import org.graylog2.indexer.searches.timeranges.AbsoluteRange;
Expand Down Expand Up @@ -118,8 +117,6 @@ protected ComputationResult compute() {
} else { } else {
return new ComputationResult(getStatisticalValue(fieldStatsResult), fieldStatsResult.took().millis()); return new ComputationResult(getStatisticalValue(fieldStatsResult), fieldStatsResult.took().millis());
} }
} catch (IndexHelper.InvalidRangeFormatException e) {
throw new RuntimeException("Invalid timerange format.", e);
} catch (Searches.FieldTypeException e) { } catch (Searches.FieldTypeException e) {
throw new RuntimeException("Invalid field provided.", e); throw new RuntimeException("Invalid field provided.", e);
} }
Expand Down
Expand Up @@ -127,6 +127,4 @@ public int compare(IndexRange o1, IndexRange o2) {
return indices; return indices;
} }


public static class InvalidRangeFormatException extends Exception {
}
} }
@@ -0,0 +1,19 @@
package org.graylog2.indexer;

public class InvalidRangeFormatException extends RuntimeException {
public InvalidRangeFormatException() {
super("Invalid timerange parameters provided");
}

public InvalidRangeFormatException(String message) {
super(message);
}

public InvalidRangeFormatException(String message, Throwable cause) {
super(message, cause);
}

public InvalidRangeFormatException(Throwable cause) {
super("Invalid timerange parameters provided", cause);
}
}
@@ -0,0 +1,20 @@
package org.graylog2.indexer;

import org.graylog2.plugin.rest.ApiError;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class InvalidRangeFormatExceptionMapper implements ExceptionMapper<InvalidRangeFormatException> {
@Override
public Response toResponse(InvalidRangeFormatException exception) {
return Response
.status(Response.Status.BAD_REQUEST)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(new ApiError(exception.getMessage()))
.build();
}
}

0 comments on commit c84d610

Please sign in to comment.