Skip to content

Commit 66bb1ee

Browse files
committed
Improve error messages on bad [format] and [null_value] params for date mapper (#61932)
Currently, if an incorrectly formatted date is passed as a null_value for a date field mapper configuration, you get a vague error: Failed to parse mapping [_doc]: cannot parse empty date Similarly, if you pass an incorrect format, you get the error: Failed to parse mapping [_doc]: Invalid format [...] This commit improves both these errors by including the mapper name and parameter that are misconfigured. Fixes #61712
1 parent 289b1f4 commit 66bb1ee

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,27 +217,41 @@ public Builder(String name, Version indexCreatedVersion, Resolution resolution,
217217
}
218218
}
219219

220-
protected DateFieldType setupFieldType(BuilderContext context) {
221-
DateFormatter formatter;
222-
if (Joda.isJodaPattern(indexCreatedVersion, format.getValue())) {
223-
formatter = Joda.forPattern(format.getValue()).withLocale(locale.getValue());
224-
} else {
225-
formatter = DateFormatter.forPattern(format.getValue()).withLocale(locale.getValue());
220+
private DateFormatter buildFormatter() {
221+
try {
222+
if (Joda.isJodaPattern(indexCreatedVersion, format.getValue())) {
223+
return Joda.forPattern(format.getValue()).withLocale(locale.getValue());
224+
} else {
225+
return DateFormatter.forPattern(format.getValue()).withLocale(locale.getValue());
226+
}
227+
} catch (IllegalArgumentException e) {
228+
throw new IllegalArgumentException("Error parsing [format] on field [" + name() + "]: " + e.getMessage(), e);
226229
}
227-
return new DateFieldType(buildFullName(context), index.getValue(), docValues.getValue(),
228-
formatter, resolution, meta.getValue());
229230
}
230231

231232
@Override
232233
protected List<Parameter<?>> getParameters() {
233234
return Arrays.asList(index, docValues, store, format, locale, nullValue, ignoreMalformed, boost, meta);
234235
}
235236

237+
private Long parseNullValue(DateFormatter formatter) {
238+
if (nullValue.getValue() == null) {
239+
return null;
240+
}
241+
try {
242+
return formatter.parseMillis(nullValue.getValue());
243+
}
244+
catch (Exception e) {
245+
throw new MapperParsingException("Error parsing [null_value] on field [" + name() + "]: " + e.getMessage(), e);
246+
}
247+
}
248+
236249
@Override
237250
public DateFieldMapper build(BuilderContext context) {
238-
DateFieldType ft = setupFieldType(context);
251+
DateFieldType ft = new DateFieldType(buildFullName(context), index.getValue(), docValues.getValue(),
252+
buildFormatter(), resolution, meta.getValue());
239253
ft.setBoost(boost.getValue());
240-
Long nullTimestamp = nullValue.getValue() == null ? null : ft.dateTimeFormatter.parseMillis(nullValue.getValue());
254+
Long nullTimestamp = parseNullValue(ft.dateTimeFormatter);
241255
return new DateFieldMapper(name, ft, multiFieldsBuilder.build(this, context),
242256
copyTo.build(), nullTimestamp, resolution, indexCreatedVersion, this);
243257
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Map;
4040

4141
import static org.hamcrest.Matchers.containsString;
42+
import static org.hamcrest.Matchers.equalTo;
4243
import static org.hamcrest.Matchers.notNullValue;
4344

4445
public class DateFieldMapperTests extends MapperTestCase {
@@ -192,6 +193,15 @@ public void testNullValue() throws IOException {
192193
assertFalse(dvField.fieldType().stored());
193194
}
194195

196+
public void testBadNullValue() {
197+
198+
MapperParsingException e = expectThrows(MapperParsingException.class,
199+
() -> createDocumentMapper(fieldMapping(b -> b.field("type", "date").field("null_value", ""))));
200+
201+
assertThat(e.getMessage(),
202+
equalTo("Failed to parse mapping [_doc]: Error parsing [null_value] on field [field]: cannot parse empty date"));
203+
}
204+
195205
public void testNullConfigValuesFail() {
196206
Exception e = expectThrows(MapperParsingException.class,
197207
() -> createDocumentMapper(fieldMapping(b -> b.field("type", "date").nullField("format"))));
@@ -244,6 +254,7 @@ public void testIllegalFormatField() {
244254
.field("type", "date")
245255
.field("format", "test_format"))));
246256
assertThat(e.getMessage(), containsString("Invalid format: [test_format]: Unknown pattern letter: t"));
257+
assertThat(e.getMessage(), containsString("Error parsing [format] on field [field]: Invalid"));
247258
}
248259

249260
public void testFetchSourceValue() {

0 commit comments

Comments
 (0)