use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class SkipSection method parse.
public static SkipSection parse(XContentParser parser) throws IOException {
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new IllegalArgumentException("Expected [" + XContentParser.Token.START_OBJECT + ", found [" + parser.currentToken() + "], the skip section is not properly indented");
}
String currentFieldName = null;
XContentParser.Token token;
String version = null;
String reason = null;
List<String> features = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("version".equals(currentFieldName)) {
version = parser.text();
} else if ("reason".equals(currentFieldName)) {
reason = parser.text();
} else if ("features".equals(currentFieldName)) {
features.add(parser.text());
} else {
throw new ParsingException(parser.getTokenLocation(), "field " + currentFieldName + " not supported within skip section");
}
} else if (token == XContentParser.Token.START_ARRAY) {
if ("features".equals(currentFieldName)) {
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
features.add(parser.text());
}
}
}
}
parser.nextToken();
if (!Strings.hasLength(version) && features.isEmpty()) {
throw new ParsingException(parser.getTokenLocation(), "version or features is mandatory within skip section");
}
if (Strings.hasLength(version) && !Strings.hasLength(reason)) {
throw new ParsingException(parser.getTokenLocation(), "reason is mandatory within skip version section");
}
return new SkipSection(version, features, reason);
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class AbstractQueryTestCase method testUnknownField.
/**
* Test that unknown field trigger ParsingException.
* To find the right position in the root query, we add a marker as `queryName` which
* all query builders support. The added bogus field after that should trigger the exception.
* Queries that allow arbitrary field names at this level need to override this test.
*/
public void testUnknownField() throws IOException {
String marker = "#marker#";
QB testQuery;
do {
testQuery = createTestQueryBuilder();
} while (testQuery.toString().contains(marker));
// to find root query to add additional bogus field there
testQuery.queryName(marker);
String queryAsString = testQuery.toString().replace("\"" + marker + "\"", "\"" + marker + "\", \"bogusField\" : \"someValue\"");
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(queryAsString));
// we'd like to see the offending field name here
assertThat(e.getMessage(), containsString("bogusField"));
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class ConstructingObjectParserTests method testBadParamBeforeObjectBuilt.
public void testBadParamBeforeObjectBuilt() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + " \"a\": \"supercalifragilisticexpialidocious\",\n" + " \"animal\": \"cat\"\n," + " \"vegetable\": 2\n" + "}");
ParsingException e = expectThrows(ParsingException.class, () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null));
assertEquals("[has_required_arguments] failed to parse field [vegetable]", e.getMessage());
assertEquals(4, e.getLineNumber());
e = (ParsingException) e.getCause();
assertEquals("failed to build [has_required_arguments] after last required field arrived", e.getMessage());
assertEquals(2, e.getLineNumber());
e = (ParsingException) e.getCause();
assertEquals("[has_required_arguments] failed to parse field [a]", e.getMessage());
assertEquals(2, e.getLineNumber());
assertEquals("[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", e.getCause().getMessage());
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class ObjectParserTests method testParseNamedObjectInOrderNotSupported.
public void testParseNamedObjectInOrderNotSupported() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + " {\"a\": {}}" + "]}");
// Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
ObjectParser<NamedObjectHolder, Void> objectParser = new ObjectParser<>("named_object_holder", NamedObjectHolder::new);
objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named"));
// Now firing the xml through it fails
ParsingException e = expectThrows(ParsingException.class, () -> objectParser.apply(parser, null));
assertEquals("[named_object_holder] failed to parse field [named]", e.getMessage());
assertEquals("[named] doesn't support arrays. Use a single object with multiple fields.", e.getCause().getMessage());
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class ObjectParserTests method testParseNamedObjectNoFieldsInArray.
public void testParseNamedObjectNoFieldsInArray() throws IOException {
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + " {}" + "]}");
ParsingException e = expectThrows(ParsingException.class, () -> NamedObjectHolder.PARSER.apply(parser, null));
assertEquals("[named_object_holder] failed to parse field [named]", e.getMessage());
assertEquals("[named] can be a single object with any number of fields or an array where each entry is an object with a single field", e.getCause().getMessage());
}
Aggregations