Commit f48d6c40 authored by Matija Obreza's avatar Matija Obreza

Fix: Reduce log clutter by using appropriate exceptions in AppliedFilters parser

parent 62e0194c
...@@ -22,6 +22,7 @@ import java.util.Map; ...@@ -22,6 +22,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.MaxPageLimitException; import org.genesys2.server.exception.MaxPageLimitException;
import org.genesys2.server.exception.NotFoundElement; import org.genesys2.server.exception.NotFoundElement;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -79,7 +80,7 @@ public class UserControllerAdvice extends BaseController { ...@@ -79,7 +80,7 @@ public class UserControllerAdvice extends BaseController {
} }
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = { MaxPageLimitException.class, RequestRejectedException.class, IllegalArgumentException.class }) @ExceptionHandler(value = { MaxPageLimitException.class, InvalidApiUsageException.class, RequestRejectedException.class, IllegalArgumentException.class })
public ModelAndView handleMaxPageLimitException(final Throwable e, final HttpServletRequest request) { public ModelAndView handleMaxPageLimitException(final Throwable e, final HttpServletRequest request) {
LOG.warn("Bad request {} {}: {}", request.getMethod(), request.getRequestURL(), e.getMessage()); LOG.warn("Bad request {} {}: {}", request.getMethod(), request.getRequestURL(), e.getMessage());
final ModelAndView mav = new ModelAndView("/errors/error"); final ModelAndView mav = new ModelAndView("/errors/error");
......
...@@ -272,7 +272,7 @@ public class FilterHandler { ...@@ -272,7 +272,7 @@ public class FilterHandler {
@Override @Override
public AppliedFilters deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { public AppliedFilters deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
if (jp.getCurrentToken() != JsonToken.START_OBJECT) { if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
throw new IOException("invalid start marker"); throw new JsonParseException(jp, "invalid start marker");
} }
final AppliedFilters appliedFilters = new AppliedFilters(); final AppliedFilters appliedFilters = new AppliedFilters();
...@@ -317,7 +317,7 @@ public class FilterHandler { ...@@ -317,7 +317,7 @@ public class FilterHandler {
if ("like".equals(op)) { if ("like".equals(op)) {
String startsWith = jp.nextTextValue(); String startsWith = jp.nextTextValue();
if (startsWith == null || StringUtils.isBlank(startsWith)) if (startsWith == null || StringUtils.isBlank(startsWith))
throw new JsonParseException("StartsWithFilter expects a non-blank string", jp.getCurrentLocation(), new InvalidApiUsageException("StartsWithFilter expects a non-blank string")); throw new JsonParseException(jp, "StartsWithFilter expects a non-blank string", jp.getCurrentLocation(), new InvalidApiUsageException("StartsWithFilter expects a non-blank string"));
af.addFilterValue(new StartsWithFilter(startsWith)); af.addFilterValue(new StartsWithFilter(startsWith));
} else if ("min".equals(op)) { } else if ("min".equals(op)) {
...@@ -330,7 +330,7 @@ public class FilterHandler { ...@@ -330,7 +330,7 @@ public class FilterHandler {
} }
if (number1 == null) if (number1 == null)
throw new JsonParseException("MinValueFilter expects a single numeric value", jp.getCurrentLocation(), new InvalidApiUsageException("MinValueFilter expects a single numeric value")); throw new JsonParseException(jp, "MinValueFilter expects a single numeric value", jp.getCurrentLocation(), new InvalidApiUsageException("MinValueFilter expects a single numeric value"));
af.addFilterValue(new MinValueFilter(number1)); af.addFilterValue(new MinValueFilter(number1));
} else if ("dateMin".equals(op)) { } else if ("dateMin".equals(op)) {
...@@ -341,7 +341,7 @@ public class FilterHandler { ...@@ -341,7 +341,7 @@ public class FilterHandler {
} }
if (value == null) if (value == null)
throw new JsonParseException("MinValueFilter for date expects a single string value", jp.getCurrentLocation(), new InvalidApiUsageException("MinValueFilter for date expects a single string value")); throw new JsonParseException(jp, "MinValueFilter for date expects a single string value", jp.getCurrentLocation(), new InvalidApiUsageException("MinValueFilter for date expects a single string value"));
af.addFilterValue(new MinValueFilter(value)); af.addFilterValue(new MinValueFilter(value));
} else if ("max".equals(op)) { } else if ("max".equals(op)) {
...@@ -354,7 +354,7 @@ public class FilterHandler { ...@@ -354,7 +354,7 @@ public class FilterHandler {
} }
if (number1 == null) if (number1 == null)
throw new JsonParseException("MaxValueFilter expects a single numeric value", jp.getCurrentLocation()); throw new JsonParseException(jp, "MaxValueFilter expects a single numeric value", jp.getCurrentLocation());
af.addFilterValue(new MaxValueFilter(number1)); af.addFilterValue(new MaxValueFilter(number1));
} else if ("dateMax".equals(op)) { } else if ("dateMax".equals(op)) {
...@@ -364,7 +364,7 @@ public class FilterHandler { ...@@ -364,7 +364,7 @@ public class FilterHandler {
value = jp.getValueAsString(); value = jp.getValueAsString();
} }
if (value == null) if (value == null)
throw new JsonParseException("MaxValueFilter for date expects a single string value", jp.getCurrentLocation()); throw new JsonParseException(jp, "MaxValueFilter for date expects a single string value", jp.getCurrentLocation());
af.addFilterValue(new MaxValueFilter(value)); af.addFilterValue(new MaxValueFilter(value));
} else if ("range".equals(op)) { } else if ("range".equals(op)) {
...@@ -383,11 +383,11 @@ public class FilterHandler { ...@@ -383,11 +383,11 @@ public class FilterHandler {
number2 = jp.getLongValue(); number2 = jp.getLongValue();
} }
if (number1 == null || number2 == null || jp.nextToken() != JsonToken.END_ARRAY) if (number1 == null || number2 == null || jp.nextToken() != JsonToken.END_ARRAY)
throw new JsonParseException("ValueRangeFilter expects two numeric values in an array", jp.getCurrentLocation()); throw new JsonParseException(jp, "ValueRangeFilter expects two numeric values in an array", jp.getCurrentLocation());
af.addFilterValue(new ValueRangeFilter(number1, number2)); af.addFilterValue(new ValueRangeFilter(number1, number2));
} else { } else {
throw new JsonParseException("ValueRangeFilter expects an array of values", jp.getCurrentLocation()); throw new JsonParseException(jp, "ValueRangeFilter expects an array of values", jp.getCurrentLocation());
} }
} else if ("dateRange".equals(op)) { } else if ("dateRange".equals(op)) {
if (jp.nextToken() == JsonToken.START_ARRAY) { if (jp.nextToken() == JsonToken.START_ARRAY) {
...@@ -401,21 +401,21 @@ public class FilterHandler { ...@@ -401,21 +401,21 @@ public class FilterHandler {
value2 = jp.getValueAsString(); value2 = jp.getValueAsString();
} }
if (value1 == null || value2 == null || jp.nextToken() != JsonToken.END_ARRAY) if (value1 == null || value2 == null || jp.nextToken() != JsonToken.END_ARRAY)
throw new JsonParseException("ValueRangeFilter for date expects two string values in an array", jp.getCurrentLocation()); throw new JsonParseException(jp, "ValueRangeFilter for date expects two string values in an array", jp.getCurrentLocation());
af.addFilterValue(new ValueRangeFilter(value1, value2)); af.addFilterValue(new ValueRangeFilter(value1, value2));
} else { } else {
throw new JsonParseException("ValueRangeFilter expects an array of values", jp.getCurrentLocation()); throw new JsonParseException(jp, "ValueRangeFilter expects an array of values", jp.getCurrentLocation());
} }
} }
if (jp.nextToken() != JsonToken.END_OBJECT) if (jp.nextToken() != JsonToken.END_OBJECT)
throw new JsonParseException("Expecting }, got " + jp.getCurrentToken(), jp.getCurrentLocation()); throw new JsonParseException(jp, "Expecting }, got " + jp.getCurrentToken(), jp.getCurrentLocation());
} else if (jp.getCurrentToken() == JsonToken.END_ARRAY) { } else if (jp.getCurrentToken() == JsonToken.END_ARRAY) {
// Blank array // Blank array
break; break;
} else { } else {
throw new JsonParseException("I don't know where I am.", jp.getCurrentLocation()); throw new JsonParseException(jp, "I don't know where I am.", jp.getCurrentLocation());
} }
jp.nextToken(); jp.nextToken();
...@@ -423,7 +423,7 @@ public class FilterHandler { ...@@ -423,7 +423,7 @@ public class FilterHandler {
} while (jp.getCurrentToken() != JsonToken.END_ARRAY); } while (jp.getCurrentToken() != JsonToken.END_ARRAY);
} else { } else {
System.err.println(jp.getCurrentToken()); System.err.println(jp.getCurrentToken());
throw new JsonParseException("Filter values must be provided in an array", jp.getCurrentLocation()); throw new JsonParseException(jp, "Filter values must be provided in an array", jp.getCurrentLocation());
} }
if (jp.nextToken() == JsonToken.END_OBJECT) if (jp.nextToken() == JsonToken.END_OBJECT)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment