Search in sources :

Example 1 with UnknownNamedObjectException

use of org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException in project elasticsearch by elastic.

the class QueryParseContext method parseInnerQueryBuilder.

/**
     * Parses a query excluding the query element that wraps it
     */
public QueryBuilder parseInnerQueryBuilder() throws IOException {
    if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
        if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, must start with start_object");
        }
    }
    if (parser.nextToken() == XContentParser.Token.END_OBJECT) {
        // we encountered '{}' for a query clause, it used to be supported, deprecated in 5.0 and removed in 6.0
        throw new IllegalArgumentException("query malformed, empty clause found at [" + parser.getTokenLocation() + "]");
    }
    if (parser.currentToken() != XContentParser.Token.FIELD_NAME) {
        throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, no field after start_object");
    }
    String queryName = parser.currentName();
    // move to the next START_OBJECT
    if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] query malformed, no start_object after query name");
    }
    QueryBuilder result;
    try {
        result = parser.namedObject(QueryBuilder.class, queryName, this);
    } catch (UnknownNamedObjectException e) {
        // This intentionally doesn't include the causing exception because that'd change the "root_cause" of any unknown query errors
        throw new ParsingException(new XContentLocation(e.getLineNumber(), e.getColumnNumber()), "no [query] registered for [" + e.getName() + "]");
    }
    //end_object of the specific query (e.g. match, multi_match etc.) element
    if (parser.currentToken() != XContentParser.Token.END_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]");
    }
    //end_object of the query object
    if (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]");
    }
    return result;
}
Also used : UnknownNamedObjectException(org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException) ParsingException(org.elasticsearch.common.ParsingException) XContentLocation(org.elasticsearch.common.xcontent.XContentLocation)

Example 2 with UnknownNamedObjectException

use of org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException in project elasticsearch by elastic.

the class UnknownNamedObjectExceptionTests method testRoundTrip.

public void testRoundTrip() throws IOException {
    XContentLocation location = new XContentLocation(between(1, 1000), between(1, 1000));
    UnknownNamedObjectException created = new UnknownNamedObjectException(location, UnknownNamedObjectExceptionTests.class, randomAsciiOfLength(5));
    UnknownNamedObjectException roundTripped;
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        created.writeTo(out);
        try (StreamInput in = out.bytes().streamInput()) {
            roundTripped = new UnknownNamedObjectException(in);
        }
    }
    assertEquals(created.getMessage(), roundTripped.getMessage());
    assertEquals(created.getLineNumber(), roundTripped.getLineNumber());
    assertEquals(created.getColumnNumber(), roundTripped.getColumnNumber());
    assertEquals(created.getCategoryClass(), roundTripped.getCategoryClass());
    assertEquals(created.getName(), roundTripped.getName());
}
Also used : UnknownNamedObjectException(org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 3 with UnknownNamedObjectException

use of org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException in project elasticsearch by elastic.

the class UnknownNamedObjectExceptionTests method testStatusCode.

public void testStatusCode() {
    XContentLocation location = new XContentLocation(between(1, 1000), between(1, 1000));
    UnknownNamedObjectException e = new UnknownNamedObjectException(location, UnknownNamedObjectExceptionTests.class, randomAsciiOfLength(5));
    assertEquals(RestStatus.BAD_REQUEST, e.status());
}
Also used : UnknownNamedObjectException(org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException)

Aggregations

UnknownNamedObjectException (org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException)3 ParsingException (org.elasticsearch.common.ParsingException)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 XContentLocation (org.elasticsearch.common.xcontent.XContentLocation)1