Skip to content

Commit 5664f4f

Browse files
Improved error message when index field type is unknown (#122860)
* Updating error message when index field type is unknown * Fix style issue * Add yaml test for invalid field type error message * Update docs/changelog/122860.yaml * Updating error message for runtime and multi field type parser * add and fix yaml tests * Fix code styles by running spotlessApply * Update changelog * Updatig the test in yml * Updating error message for runtime * Fix failing yaml tests * Update error message to Fix unit tests * fix serverless qa test --------- Co-authored-by: Elastic Machine <[email protected]>
1 parent 4c3ceae commit 5664f4f

File tree

8 files changed

+106
-6
lines changed

8 files changed

+106
-6
lines changed

docs/changelog/122860.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 122860
2+
summary: Improved error message when index field type is invalid
3+
area: Mapping
4+
type: enhancement
5+
issues: []

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,62 @@
214214
index.mode: lookup
215215
index.number_of_shards: 2
216216

217+
---
218+
"Create index with invalid field type":
219+
- requires:
220+
cluster_features: [ "mapper.unknown_field_mapping_update_error_message" ]
221+
reason: "Update error message for unknown field type"
222+
223+
- do:
224+
catch: bad_request
225+
indices.create:
226+
index: test_index
227+
body:
228+
mappings:
229+
properties:
230+
content:
231+
type: invalid
232+
233+
- match: { error.type: "mapper_parsing_exception" }
234+
- match: { error.reason: "Failed to parse mapping: The mapper type [invalid] declared on field [content] does not exist. It might have been created within a future version or requires a plugin to be installed. Check the documentation." }
235+
236+
---
237+
"Create index with invalid runtime field type":
238+
- requires:
239+
cluster_features: [ "mapper.unknown_field_mapping_update_error_message" ]
240+
reason: "Update error message for unknown field type"
241+
242+
- do:
243+
catch: bad_request
244+
indices.create:
245+
index: test_index
246+
body:
247+
mappings:
248+
runtime:
249+
content:
250+
type: invalid
251+
252+
- match: { error.type: "mapper_parsing_exception" }
253+
- match: { error.reason: "Failed to parse mapping: The mapper type [invalid] declared on runtime field [content] does not exist. It might have been created within a future version or requires a plugin to be installed. Check the documentation." }
254+
255+
---
256+
"Create index with invalid multi-field type":
257+
- requires:
258+
cluster_features: [ "mapper.unknown_field_mapping_update_error_message" ]
259+
reason: "Update error message for unknown field type"
260+
261+
- do:
262+
catch: bad_request
263+
indices.create:
264+
index: test_index
265+
body:
266+
mappings:
267+
properties:
268+
city:
269+
type: text
270+
fields:
271+
raw:
272+
type: invalid
273+
274+
- match: { error.type: "mapper_parsing_exception" }
275+
- match: { error.reason: "Failed to parse mapping: The mapper type [invalid] declared on field [raw] does not exist. It might have been created within a future version or requires a plugin to be installed. Check the documentation." }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class MapperFeatures implements FeatureSpecification {
3333
public static final NodeFeature SORT_FIELDS_CHECK_FOR_NESTED_OBJECT_FIX = new NodeFeature("mapper.nested.sorting_fields_check_fix");
3434
public static final NodeFeature DYNAMIC_HANDLING_IN_COPY_TO = new NodeFeature("mapper.copy_to.dynamic_handling");
3535
public static final NodeFeature DOC_VALUES_SKIPPER = new NodeFeature("mapper.doc_values_skipper");
36+
static final NodeFeature UKNOWN_FIELD_MAPPING_UPDATE_ERROR_MESSAGE = new NodeFeature(
37+
"mapper.unknown_field_mapping_update_error_message"
38+
);
3639

3740
@Override
3841
public Set<NodeFeature> getTestFeatures() {
@@ -54,6 +57,7 @@ public Set<NodeFeature> getTestFeatures() {
5457
TSDB_NESTED_FIELD_SUPPORT,
5558
SourceFieldMapper.SYNTHETIC_RECOVERY_SOURCE,
5659
ObjectMapper.SUBOBJECTS_FALSE_MAPPING_UPDATE_FIX,
60+
UKNOWN_FIELD_MAPPING_UPDATE_ERROR_MESSAGE,
5761
DOC_VALUES_SKIPPER
5862
);
5963
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,15 @@ protected static void parseProperties(Builder objBuilder, Map<String, Object> pr
396396
}
397397
Mapper.TypeParser typeParser = parserContext.typeParser(type);
398398
if (typeParser == null) {
399-
throw new MapperParsingException("No handler for type [" + type + "] declared on field [" + fieldName + "]");
399+
throw new MapperParsingException(
400+
"The mapper type ["
401+
+ type
402+
+ "] declared on field ["
403+
+ fieldName
404+
+ "] does not exist."
405+
+ " It might have been created within a future version or requires a plugin to be installed."
406+
+ " Check the documentation."
407+
);
400408
}
401409
Mapper.Builder fieldBuilder;
402410
if (objBuilder.subobjects.isPresent() && objBuilder.subobjects.get() != Subobjects.ENABLED) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,15 @@ static Map<String, RuntimeField> parseRuntimeFields(
179179
}
180180
Parser typeParser = parserContext.runtimeFieldParser(type);
181181
if (typeParser == null) {
182-
throw new MapperParsingException("No handler for type [" + type + "] declared on runtime field [" + fieldName + "]");
182+
throw new MapperParsingException(
183+
"The mapper type ["
184+
+ type
185+
+ "] declared on runtime field ["
186+
+ fieldName
187+
+ "] does not exist."
188+
+ " It might have been created within a future version or requires a plugin to be installed."
189+
+ " Check the documentation."
190+
);
183191
}
184192
runtimeFields.put(fieldName, builder.apply(typeParser.parse(fieldName, propNode, parserContext)));
185193
propNode.remove("type");

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,15 @@ public static boolean parseMultiField(
161161

162162
Mapper.TypeParser typeParser = parserContext.typeParser(type);
163163
if (typeParser == null) {
164-
throw new MapperParsingException("no handler for type [" + type + "] declared on field [" + multiFieldName + "]");
164+
throw new MapperParsingException(
165+
"The mapper type ["
166+
+ type
167+
+ "] declared on field ["
168+
+ multiFieldName
169+
+ "] does not exist."
170+
+ " It might have been created within a future version or requires a plugin to be installed."
171+
+ " Check the documentation."
172+
);
165173
}
166174
if (typeParser instanceof FieldMapper.TypeParser == false) {
167175
throw new MapperParsingException("Type [" + type + "] cannot be used in multi field");

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,21 @@ public void testRuntimeSectionMerge() throws IOException {
313313
public void testRuntimeSectionNonRuntimeType() throws IOException {
314314
XContentBuilder mapping = runtimeFieldMapping(builder -> builder.field("type", "unknown"));
315315
MapperParsingException e = expectThrows(MapperParsingException.class, () -> createMapperService(mapping));
316-
assertEquals("Failed to parse mapping: No handler for type [unknown] declared on runtime field [field]", e.getMessage());
316+
assertEquals(
317+
"Failed to parse mapping: The mapper type [unknown] declared on runtime field [field] does not exist."
318+
+ " It might have been created within a future version or requires a plugin to be installed. Check the documentation.",
319+
e.getMessage()
320+
);
317321
}
318322

319323
public void testRuntimeSectionHandlerNotFound() throws IOException {
320324
XContentBuilder mapping = runtimeFieldMapping(builder -> builder.field("type", "unknown"));
321325
MapperParsingException e = expectThrows(MapperParsingException.class, () -> createMapperService(mapping));
322-
assertEquals("Failed to parse mapping: No handler for type [unknown] declared on runtime field [field]", e.getMessage());
326+
assertEquals(
327+
"Failed to parse mapping: The mapper type [unknown] declared on runtime field [field] does not exist."
328+
+ " It might have been created within a future version or requires a plugin to be installed. Check the documentation.",
329+
e.getMessage()
330+
);
323331
}
324332

325333
public void testRuntimeSectionMissingType() throws IOException {

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/transform/transforms_crud.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ setup:
407407
---
408408
"Test transform where source query is invalid":
409409
- do:
410-
catch: /No handler for type \[bad-type\] declared on runtime field \[rt-field\]/
410+
catch: /The mapper type \[bad-type\] declared on runtime field \[rt-field\]/
411411
transform.put_transform:
412412
transform_id: "airline-transform"
413413
body: >

0 commit comments

Comments
 (0)