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);
}
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);
}
}
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");
}
}
Aggregations