Search in sources :

Example 1 with QueryParsingException

use of org.graylog2.indexer.QueryParsingException in project graylog2-server by Graylog2.

the class JestUtils method buildQueryParsingException.

private static QueryParsingException buildQueryParsingException(Supplier<String> errorMessage, JsonNode rootCause, List<String> reasons) {
    final JsonNode lineJson = rootCause.path("line");
    final Integer line = lineJson.isInt() ? lineJson.asInt() : null;
    final JsonNode columnJson = rootCause.path("col");
    final Integer column = columnJson.isInt() ? columnJson.asInt() : null;
    final String index = rootCause.path("index").asText(null);
    return new QueryParsingException(errorMessage.get(), line, column, index, reasons);
}
Also used : QueryParsingException(org.graylog2.indexer.QueryParsingException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with QueryParsingException

use of org.graylog2.indexer.QueryParsingException in project graylog2-server by Graylog2.

the class SearchResource method createRequestExceptionForParseFailure.

protected WebApplicationException createRequestExceptionForParseFailure(String query, SearchPhaseExecutionException e) {
    LOG.warn("Unable to execute search: {}", e.getMessage());
    QueryParseError errorMessage = QueryParseError.create(query, "Unable to execute search", e.getClass().getCanonicalName());
    // We're so going to hell for this…
    if (e.toString().contains("nested: QueryParsingException")) {
        final QueryParser queryParser = new QueryParser("", new StandardAnalyzer());
        try {
            queryParser.parse(query);
        } catch (ParseException parseException) {
            Token currentToken = null;
            try {
                // FIXME I have no idea why this is necessary but without that call currentToken will be null.
                final ParseException exception = queryParser.generateParseException();
                currentToken = exception.currentToken;
            } catch (NullPointerException npe) {
                // "Normal" exception and no need to spam the logs with it.
                LOG.debug("Exception thrown while generating parse exception.", npe);
            }
            if (currentToken == null) {
                LOG.warn("No position/token available for ParseException.", parseException);
                errorMessage = QueryParseError.create(query, parseException.getMessage(), parseException.getClass().getCanonicalName());
            } else {
                // scan for first usable token with position information
                int beginColumn = 0;
                int beginLine = 0;
                int endColumn = 0;
                int endLine = 0;
                while (currentToken != null && beginLine == 0) {
                    beginColumn = currentToken.beginColumn;
                    beginLine = currentToken.beginLine;
                    endColumn = currentToken.endColumn;
                    endLine = currentToken.endLine;
                    currentToken = currentToken.next;
                }
                errorMessage = QueryParseError.create(query, beginColumn, beginLine, endColumn, endLine, parseException.getMessage(), parseException.getClass().getCanonicalName());
            }
        }
        return new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build());
    } else {
        return new InternalServerErrorException("Unable to fulfill search request", e);
    }
}
Also used : QueryParser(org.apache.lucene.queryparser.classic.QueryParser) QueryParseError(org.graylog2.rest.resources.search.responses.QueryParseError) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Token(org.apache.lucene.queryparser.classic.Token) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 3 with QueryParsingException

use of org.graylog2.indexer.QueryParsingException in project graylog2-server by Graylog2.

the class JestUtilsTest method executeWithQueryParsingException.

@Test
public void executeWithQueryParsingException() throws Exception {
    final Ping request = new Ping.Builder().build();
    final JestResult resultMock = mock(JestResult.class);
    when(resultMock.isSucceeded()).thenReturn(false);
    final ObjectNode rootCauseStub = objectMapper.createObjectNode();
    rootCauseStub.set("type", new TextNode("query_parsing_exception"));
    rootCauseStub.set("reason", new TextNode("foobar"));
    rootCauseStub.set("line", new IntNode(23));
    rootCauseStub.set("col", new IntNode(42));
    rootCauseStub.set("index", new TextNode("my_index"));
    final ArrayNode rootCausesStub = objectMapper.createArrayNode();
    rootCausesStub.add(rootCauseStub);
    final ObjectNode errorStub = objectMapper.createObjectNode();
    errorStub.set("root_cause", rootCausesStub);
    final ObjectNode responseStub = objectMapper.createObjectNode();
    responseStub.set("error", errorStub);
    when(resultMock.getJsonObject()).thenReturn(responseStub);
    when(clientMock.execute(request)).thenReturn(resultMock);
    try {
        JestUtils.execute(clientMock, request, () -> "BOOM");
        fail("Expected QueryParsingException to be thrown");
    } catch (QueryParsingException e) {
        assertThat(e).hasMessageStartingWith("BOOM").hasMessageEndingWith("foobar").hasNoSuppressedExceptions();
        assertThat(e.getErrorDetails()).containsExactly("foobar");
        assertThat(e.getLine()).contains(23);
        assertThat(e.getColumn()).contains(42);
        assertThat(e.getIndex()).contains("my_index");
    }
}
Also used : IntNode(com.fasterxml.jackson.databind.node.IntNode) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Ping(io.searchbox.core.Ping) QueryParsingException(org.graylog2.indexer.QueryParsingException) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Aggregations

QueryParsingException (org.graylog2.indexer.QueryParsingException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 IntNode (com.fasterxml.jackson.databind.node.IntNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 JestResult (io.searchbox.client.JestResult)1 Ping (io.searchbox.core.Ping)1 BadRequestException (javax.ws.rs.BadRequestException)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)1 Token (org.apache.lucene.queryparser.classic.Token)1 QueryParseError (org.graylog2.rest.resources.search.responses.QueryParseError)1 Test (org.junit.Test)1