Skip to content

Commit 774a97f

Browse files
authored
Improve DateTime error handling and add some bad date tests (elastic#112723) (elastic#113569)
* Improve DateTime error handling and add some bad date tests
1 parent 5791538 commit 774a97f

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

docs/changelog/112723.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 112723
2+
summary: Improve DateTime error handling and add some bad date tests
3+
area: Search
4+
type: bug
5+
issues:
6+
- 112190

server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import org.elasticsearch.ElasticsearchParseException;
1313
import org.elasticsearch.common.Strings;
1414

15+
import java.time.DateTimeException;
1516
import java.time.DayOfWeek;
1617
import java.time.Instant;
1718
import java.time.LocalTime;
1819
import java.time.ZoneId;
1920
import java.time.ZoneOffset;
2021
import java.time.ZonedDateTime;
21-
import java.time.format.DateTimeParseException;
2222
import java.time.temporal.ChronoField;
2323
import java.time.temporal.TemporalAccessor;
2424
import java.time.temporal.TemporalAdjusters;
@@ -220,7 +220,7 @@ private Instant parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNo
220220

221221
return DateFormatters.from(accessor).withZoneSameLocal(timeZone).toInstant();
222222
}
223-
} catch (IllegalArgumentException | DateTimeParseException e) {
223+
} catch (IllegalArgumentException | DateTimeException e) {
224224
throw new ElasticsearchParseException(
225225
"failed to parse date field [{}] with format [{}]: [{}]",
226226
e,

server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery;
2323
import org.apache.lucene.search.Query;
2424
import org.apache.lucene.store.Directory;
25+
import org.elasticsearch.ElasticsearchParseException;
2526
import org.elasticsearch.cluster.metadata.IndexMetadata;
2627
import org.elasticsearch.common.settings.Settings;
2728
import org.elasticsearch.common.time.DateFormatter;
@@ -143,6 +144,46 @@ private void doTestIsFieldWithinQuery(DateFieldType ft, DirectoryReader reader,
143144
assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", false, false, zone, null, context));
144145
assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", false, true, zone, null, context));
145146
assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", true, false, zone, null, context));
147+
// Bad dates
148+
assertThrows(
149+
ElasticsearchParseException.class,
150+
() -> ft.isFieldWithinQuery(reader, "2015-00-01", "2016-04-03", randomBoolean(), randomBoolean(), zone, null, context)
151+
);
152+
assertThrows(
153+
ElasticsearchParseException.class,
154+
() -> ft.isFieldWithinQuery(reader, "2015-01-01", "2016-04-00", randomBoolean(), randomBoolean(), zone, null, context)
155+
);
156+
assertThrows(
157+
ElasticsearchParseException.class,
158+
() -> ft.isFieldWithinQuery(reader, "2015-22-01", "2016-04-00", randomBoolean(), randomBoolean(), zone, null, context)
159+
);
160+
assertThrows(
161+
ElasticsearchParseException.class,
162+
() -> ft.isFieldWithinQuery(reader, "2015-01-01", "2016-04-45", randomBoolean(), randomBoolean(), zone, null, context)
163+
);
164+
assertThrows(
165+
ElasticsearchParseException.class,
166+
() -> ft.isFieldWithinQuery(reader, "2015-01-01", "2016-04-01T25:00:00", randomBoolean(), randomBoolean(), zone, null, context)
167+
);
168+
if (ft.resolution().equals(Resolution.NANOSECONDS)) {
169+
assertThrows(
170+
IllegalArgumentException.class,
171+
() -> ft.isFieldWithinQuery(reader, "-2016-04-01", "2016-04-01", randomBoolean(), randomBoolean(), zone, null, context)
172+
);
173+
assertThrows(
174+
IllegalArgumentException.class,
175+
() -> ft.isFieldWithinQuery(
176+
reader,
177+
"9223372036854775807",
178+
"2016-04-01",
179+
randomBoolean(),
180+
randomBoolean(),
181+
zone,
182+
null,
183+
context
184+
)
185+
);
186+
}
146187
}
147188

148189
public void testValueFormat() {

0 commit comments

Comments
 (0)