use of io.trino.client.QueryError in project trino by trinodb.
the class AbstractTrinoResultSet method resultsException.
static SQLException resultsException(QueryStatusInfo results) {
QueryError error = requireNonNull(results.getError());
String message = format("Query failed (#%s): %s", results.getId(), error.getMessage());
Throwable cause = (error.getFailureInfo() == null) ? null : error.getFailureInfo().toException();
return new SQLException(message, error.getSqlState(), error.getErrorCode(), cause);
}
use of io.trino.client.QueryError in project trino by trinodb.
the class Query method renderFailure.
public void renderFailure(PrintStream out) {
QueryStatusInfo results = client.finalStatusInfo();
QueryError error = results.getError();
checkState(error != null);
out.printf("Query %s failed: %s%n", results.getId(), error.getMessage());
if (debug && (error.getFailureInfo() != null)) {
error.getFailureInfo().toException().printStackTrace(out);
}
if (error.getErrorLocation() != null) {
renderErrorLocation(client.getQuery(), error.getErrorLocation(), out);
}
out.println();
}
use of io.trino.client.QueryError in project trino by trinodb.
the class AbstractTestingTrinoClient method execute.
public ResultWithQueryId<T> execute(Session session, @Language("SQL") String sql) {
ResultsSession<T> resultsSession = getResultSession(session);
ClientSession clientSession = toClientSession(session, trinoServer.getBaseUrl(), new Duration(2, TimeUnit.MINUTES));
try (StatementClient client = newStatementClient(httpClient, clientSession, sql)) {
while (client.isRunning()) {
resultsSession.addResults(client.currentStatusInfo(), client.currentData());
client.advance();
}
checkState(client.isFinished());
QueryError error = client.finalStatusInfo().getError();
if (error == null) {
QueryStatusInfo results = client.finalStatusInfo();
if (results.getUpdateType() != null) {
resultsSession.setUpdateType(results.getUpdateType());
}
if (results.getUpdateCount() != null) {
resultsSession.setUpdateCount(results.getUpdateCount());
}
resultsSession.setWarnings(results.getWarnings());
resultsSession.setStatementStats(results.getStats());
T result = resultsSession.build(client.getSetSessionProperties(), client.getResetSessionProperties());
return new ResultWithQueryId<>(new QueryId(results.getId()), result);
}
if (error.getFailureInfo() != null) {
RuntimeException remoteException = error.getFailureInfo().toException();
throw new RuntimeException(Optional.ofNullable(remoteException.getMessage()).orElseGet(remoteException::toString), remoteException);
}
throw new RuntimeException("Query failed: " + error.getMessage());
// dump query info to console for debugging (NOTE: not pretty printed)
// JsonCodec<QueryInfo> queryInfoJsonCodec = createCodecFactory().prettyPrint().jsonCodec(QueryInfo.class);
// log.info("\n" + queryInfoJsonCodec.toJson(queryInfo));
}
}
use of io.trino.client.QueryError in project trino by trinodb.
the class TestServer method testInvalidSessionError.
@Test
public void testInvalidSessionError() {
String invalidTimeZone = "this_is_an_invalid_time_zone";
QueryResults queryResults = postQuery(request -> request.setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)).setHeader(TRINO_HEADERS.requestCatalog(), "catalog").setHeader(TRINO_HEADERS.requestSchema(), "schema").setHeader(TRINO_HEADERS.requestPath(), "path").setHeader(TRINO_HEADERS.requestTimeZone(), invalidTimeZone)).map(JsonResponse::getValue).peek(result -> checkState((result.getError() == null) != (result.getNextUri() == null))).collect(last());
QueryError queryError = queryResults.getError();
assertNotNull(queryError);
TimeZoneNotSupportedException expected = new TimeZoneNotSupportedException(invalidTimeZone);
assertEquals(queryError.getErrorCode(), expected.getErrorCode().getCode());
assertEquals(queryError.getErrorName(), expected.getErrorCode().getName());
assertEquals(queryError.getErrorType(), expected.getErrorCode().getType().name());
assertEquals(queryError.getMessage(), expected.getMessage());
}
use of io.trino.client.QueryError in project trino by trinodb.
the class Query method toQueryError.
private static QueryError toQueryError(QueryInfo queryInfo, Optional<Throwable> exception) {
QueryState state = queryInfo.getState();
if (state != FAILED && exception.isEmpty()) {
return null;
}
ExecutionFailureInfo executionFailure;
if (queryInfo.getFailureInfo() != null) {
executionFailure = queryInfo.getFailureInfo();
} else if (exception.isPresent()) {
executionFailure = toFailure(exception.get());
} else {
log.warn("Query %s in state %s has no failure info", queryInfo.getQueryId(), state);
executionFailure = toFailure(new RuntimeException(format("Query is %s (reason unknown)", state)));
}
FailureInfo failure = executionFailure.toFailureInfo();
ErrorCode errorCode;
if (queryInfo.getErrorCode() != null) {
errorCode = queryInfo.getErrorCode();
} else if (exception.isPresent()) {
errorCode = SERIALIZATION_ERROR.toErrorCode();
} else {
errorCode = GENERIC_INTERNAL_ERROR.toErrorCode();
log.warn("Failed query %s has no error code", queryInfo.getQueryId());
}
return new QueryError(firstNonNull(failure.getMessage(), "Internal error"), null, errorCode.getCode(), errorCode.getName(), errorCode.getType().toString(), failure.getErrorLocation(), failure);
}
Aggregations