Skip to content

Commit 6fa3f7a

Browse files
lipsillChristoph Büscher
authored and
Christoph Büscher
committed
Add expected mapping type to MapperException (#31564)
Currently if a document cannot be indexed because it violates the defined mapping for the index, a MapperException is thrown. In some cases it is useful to expose the expected field type in the exception itself, so that the user can react based on the error message. This change adds the expected data type to the MapperException. Closes #31502
1 parent 6e0e0bd commit 6fa3f7a

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ public Mapper parse(ParseContext context) throws IOException {
299299
context.doc().add(field);
300300
}
301301
} catch (Exception e) {
302-
throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
302+
throw new MapperParsingException("failed to parse field [{}] of type [{}]", e, fieldType().name(),
303+
fieldType().typeName());
303304
}
304305
multiFields.parse(this, context);
305306
return null;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ public Mapper parse(ParseContext context) throws IOException {
519519
indexShape(context, shape);
520520
} catch (Exception e) {
521521
if (ignoreMalformed.value() == false) {
522-
throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
522+
throw new MapperParsingException("failed to parse field [{}] of type [{}]", e, fieldType().name(),
523+
fieldType().typeName());
523524
}
524525
context.addIgnoredField(fieldType.name());
525526
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void testParsesEs6BooleansStrict() throws IOException {
196196
.endObject());
197197
MapperParsingException ex = expectThrows(MapperParsingException.class,
198198
() -> defaultMapper.parse(SourceToParse.source("test", "type", "1", source, XContentType.JSON)));
199-
assertEquals("failed to parse [field]", ex.getMessage());
199+
assertEquals("failed to parse field [field] of type [boolean]", ex.getMessage());
200200
}
201201

202202
public void testMultiFields() throws IOException {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,35 @@ public void testDotsWithExistingNestedMapper() throws Exception {
125125
e.getMessage());
126126
}
127127

128+
public void testUnexpectedFieldMappingType() throws Exception {
129+
DocumentMapperParser mapperParser = createIndex("test").mapperService().documentMapperParser();
130+
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
131+
.startObject("foo").field("type", "long").endObject()
132+
.startObject("bar").field("type", "boolean").endObject()
133+
.startObject("geo").field("type", "geo_shape").endObject()
134+
.endObject().endObject().endObject());
135+
DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
136+
{
137+
BytesReference bytes = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("foo", true).endObject());
138+
MapperException exception = expectThrows(MapperException.class,
139+
() -> mapper.parse(SourceToParse.source("test", "type", "1", bytes, XContentType.JSON)));
140+
assertThat(exception.getMessage(), containsString("failed to parse field [foo] of type [long]"));
141+
}
142+
{
143+
BytesReference bytes = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("bar", "bar").endObject());
144+
MapperException exception = expectThrows(MapperException.class,
145+
() -> mapper.parse(SourceToParse.source("test", "type", "2", bytes, XContentType.JSON)));
146+
assertThat(exception.getMessage(), containsString("failed to parse field [bar] of type [boolean]"));
147+
}
148+
{
149+
BytesReference bytes = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("geo", 123).endObject());
150+
MapperException exception = expectThrows(MapperException.class,
151+
() -> mapper.parse(SourceToParse.source("test", "type", "2", bytes, XContentType.JSON)));
152+
assertThat(exception.getMessage(), containsString("failed to parse field [geo] of type [geo_shape]"));
153+
}
154+
155+
}
156+
128157
public void testDotsWithDynamicNestedMapper() throws Exception {
129158
DocumentMapperParser mapperParser = createIndex("test").mapperService().documentMapperParser();
130159
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")

0 commit comments

Comments
 (0)