Skip to content

Commit be54ba3

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 294ab7e commit be54ba3

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
@@ -276,7 +276,8 @@ public Mapper parse(ParseContext context) throws IOException {
276276
context.doc().add(field);
277277
}
278278
} catch (Exception e) {
279-
throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
279+
throw new MapperParsingException("failed to parse field [{}] of type [{}]", e, fieldType().name(),
280+
fieldType().typeName());
280281
}
281282
multiFields.parse(this, context);
282283
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
@@ -510,7 +510,8 @@ public Mapper parse(ParseContext context) throws IOException {
510510
indexShape(context, shape);
511511
} catch (Exception e) {
512512
if (ignoreMalformed.value() == false) {
513-
throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
513+
throw new MapperParsingException("failed to parse field [{}] of type [{}]", e, fieldType().name(),
514+
fieldType().typeName());
514515
}
515516
context.addIgnoredField(fieldType.name());
516517
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void testParsesBooleansStrict() throws IOException {
142142
.endObject());
143143
MapperParsingException ex = expectThrows(MapperParsingException.class,
144144
() -> defaultMapper.parse(SourceToParse.source("test", "type", "1", source, XContentType.JSON)));
145-
assertEquals("failed to parse [field]", ex.getMessage());
145+
assertEquals("failed to parse field [field] of type [boolean]", ex.getMessage());
146146
}
147147

148148
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)