Skip to content

Commit 50a74f9

Browse files
authored
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 9e8d8ee commit 50a74f9

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,37 @@ public Builder(String name, Resolution resolution, DateFormatter dateFormatter,
212212
}
213213
}
214214

215-
protected DateFieldType setupFieldType(BuilderContext context) {
216-
DateFormatter dateTimeFormatter = DateFormatter.forPattern(format.getValue()).withLocale(locale.getValue());
217-
return new DateFieldType(buildFullName(context), index.getValue(), docValues.getValue(),
218-
dateTimeFormatter, resolution, meta.getValue());
215+
private DateFormatter buildFormatter() {
216+
try {
217+
return DateFormatter.forPattern(format.getValue()).withLocale(locale.getValue());
218+
} catch (IllegalArgumentException e) {
219+
throw new IllegalArgumentException("Error parsing [format] on field [" + name() + "]: " + e.getMessage(), e);
220+
}
219221
}
220222

221223
@Override
222224
protected List<Parameter<?>> getParameters() {
223225
return List.of(index, docValues, store, format, locale, nullValue, ignoreMalformed, boost, meta);
224226
}
225227

228+
private Long parseNullValue(DateFormatter formatter) {
229+
if (nullValue.getValue() == null) {
230+
return null;
231+
}
232+
try {
233+
return formatter.parseMillis(nullValue.getValue());
234+
}
235+
catch (Exception e) {
236+
throw new MapperParsingException("Error parsing [null_value] on field [" + name() + "]: " + e.getMessage(), e);
237+
}
238+
}
239+
226240
@Override
227241
public DateFieldMapper build(BuilderContext context) {
228-
DateFieldType ft = setupFieldType(context);
242+
DateFieldType ft = new DateFieldType(buildFullName(context), index.getValue(), docValues.getValue(),
243+
buildFormatter(), resolution, meta.getValue());
229244
ft.setBoost(boost.getValue());
230-
Long nullTimestamp = nullValue.getValue() == null ? null : ft.dateTimeFormatter.parseMillis(nullValue.getValue());
245+
Long nullTimestamp = parseNullValue(ft.dateTimeFormatter);
231246
return new DateFieldMapper(name, ft, multiFieldsBuilder.build(this, context),
232247
copyTo.build(), nullTimestamp, resolution, this);
233248
}

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 {
@@ -191,6 +192,15 @@ public void testNullValue() throws IOException {
191192
assertFalse(dvField.fieldType().stored());
192193
}
193194

195+
public void testBadNullValue() {
196+
197+
MapperParsingException e = expectThrows(MapperParsingException.class,
198+
() -> createDocumentMapper(fieldMapping(b -> b.field("type", "date").field("null_value", ""))));
199+
200+
assertThat(e.getMessage(),
201+
equalTo("Failed to parse mapping: Error parsing [null_value] on field [field]: cannot parse empty date"));
202+
}
203+
194204
public void testNullConfigValuesFail() {
195205
Exception e = expectThrows(MapperParsingException.class,
196206
() -> 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)