Skip to content

Commit

Permalink
Improve error handling for epoch format parser with time zone (#23689)
Browse files Browse the repository at this point in the history
Change the error response when using a non UTF timezone for range queries with epoch_millis
or epoch_second formats to an illegal argument exception. The goal is to provide a better 
explanation of why the query has failed. The current behavior is to respond with a parse exception.

Closes #22621
  • Loading branch information
sneivandt authored and cbuescher committed Mar 28, 2017
1 parent b505acd commit 4820c3a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 4 additions & 3 deletions core/src/main/java/org/elasticsearch/common/joda/Joda.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,10 @@ public int parseInto(DateTimeParserBucket bucket, String text, int position) {
boolean isPositive = text.startsWith("-") == false;
boolean isTooLong = text.length() > estimateParsedLength();

if ((isPositive && isTooLong) ||
// timestamps have to have UTC timezone
bucket.getZone() != DateTimeZone.UTC) {
if (bucket.getZone() != DateTimeZone.UTC) {
String format = hasMilliSecondPrecision ? "epoch_millis" : "epoch_second";
throw new IllegalArgumentException("time_zone must be UTC for format [" + format + "]");
} else if (isPositive && isTooLong) {
return -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,36 @@ public void testForInvalidDatesInEpochMillis() {
}
}

public void testForInvalidTimeZoneWithEpochSeconds() {
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.append(new Joda.EpochTimeParser(false))
.toFormatter()
.withZone(DateTimeZone.forOffsetHours(1));
FormatDateTimeFormatter formatter =
new FormatDateTimeFormatter("epoch_seconds", dateTimeFormatter, Locale.ROOT);
try {
formatter.parser().parseDateTime("1433144433655");
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("time_zone must be UTC"));
}
}

public void testForInvalidTimeZoneWithEpochMillis() {
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.append(new Joda.EpochTimeParser(true))
.toFormatter()
.withZone(DateTimeZone.forOffsetHours(1));
FormatDateTimeFormatter formatter =
new FormatDateTimeFormatter("epoch_millis", dateTimeFormatter, Locale.ROOT);
try {
formatter.parser().parseDateTime("1433144433");
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("time_zone must be UTC"));
}
}

public void testThatEpochParserIsPrinter() {
FormatDateTimeFormatter formatter = Joda.forPattern("epoch_millis");
assertThat(formatter.parser().isPrinter(), is(true));
Expand Down

0 comments on commit 4820c3a

Please sign in to comment.