Skip to content

Commit

Permalink
Always wrap date parsing exception into IllegalArgumentException (#71038
Browse files Browse the repository at this point in the history
)

JavaDateFormatter when parsing a date with multiple parsers is using
parseObject method which can throw DateTimeException. This was not later
catched and resulted in propagating this runtime exception.
All exceptions that can occur when parsing shoudl be wrapped in
IllegalArgumentException
  • Loading branch information
pgomulka authored Mar 31, 2021
1 parent 1653f2f commit 4dcab9d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public TemporalAccessor parse(String input) {

try {
return doParse(input);
} catch (DateTimeParseException e) {
} catch (Exception e) {
throw new IllegalArgumentException("failed to parse date field [" + input + "] with format [" + format + "]", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public static void checkJvmProperties(){
: "`-Djava.locale.providers=SPI,COMPAT` needs to be set";
}

public void testIncorrectFormat() {
assertParseException("2021-01-01T23-35-00Z", "strict_date_optional_time||epoch_millis");
assertParseException("2021-01-01T23-35-00Z", "strict_date_optional_time");
}

public void testTimezoneParsing() {
/** this testcase won't work in joda. See comment in {@link #testPartialTimeParsing()}
* assertSameDateAs("2016-11-30T+01", "strict_date_optional_time", "strict_date_optional_time");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ public void testField() throws Exception {
"{\"_doc\":{\"properties\":{\"foo\":{\"type\":\"text\",\"fields\":" +
"{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}",
Strings.toString(doc.dynamicMappingsUpdate()));

}

public void testDynamicFieldOnIncorrectDate() throws Exception {
DocumentMapper mapper = createDocumentMapper(mapping(b -> {}));
ParsedDocument doc = mapper.parse(source(b -> b.field("foo", "2020-01-01T01-01-01Z")));
assertNotNull(doc.dynamicMappingsUpdate());
assertEquals(
"{\"_doc\":{\"properties\":{\"foo\":{\"type\":\"text\",\"fields\":" +
"{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}",
Strings.toString(doc.dynamicMappingsUpdate()));
}

public void testDynamicUpdateWithRuntimeField() throws Exception {
Expand Down

0 comments on commit 4dcab9d

Please sign in to comment.