From 45953a43bdc77c7db79ef66de699c7b0858bcb25 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Thu, 13 Dec 2018 16:20:39 +0100 Subject: [PATCH] Date handling: Unify exception handling between joda/javatime Due to the change in exception handling in #36447 the backport now used different exceptions on parser failures between java and joda time formatting. This uses now IllegalArgumentExceptions for formatting errors all over the place. Closes #36598 --- .../org/elasticsearch/common/time/JavaDateFormatter.java | 4 ++-- .../common/joda/JavaJodaTimeDuellingTests.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java index 68e2cfd4fe317..3bc9515c9c958 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java @@ -72,13 +72,13 @@ class JavaDateFormatter implements DateFormatter { @Override public TemporalAccessor parse(String input) { - DateTimeParseException failure = null; + IllegalArgumentException failure = null; for (int i = 0; i < parsers.length; i++) { try { return parsers[i].parse(input); } catch (DateTimeParseException e) { if (failure == null) { - failure = e; + failure = new IllegalArgumentException(e); } else { failure.addSuppressed(e); } diff --git a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java index b3790b325c66e..8b0bfff5be700 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java @@ -28,7 +28,6 @@ import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; import java.time.temporal.TemporalAccessor; import java.util.Locale; @@ -519,7 +518,8 @@ private void assertJodaParseException(String input, String format, String expect private void assertJavaTimeParseException(String input, String format, String expectedMessage) { DateFormatter javaTimeFormatter = DateFormatters.forPattern(format); - DateTimeParseException dateTimeParseException = expectThrows(DateTimeParseException.class, () -> javaTimeFormatter.parse(input)); - assertThat(dateTimeParseException.getMessage(), startsWith(expectedMessage)); + IllegalArgumentException dateTimeParseException = + expectThrows(IllegalArgumentException.class, () -> javaTimeFormatter.parse(input)); + assertThat(dateTimeParseException.getCause().getMessage(), startsWith(expectedMessage)); } }