use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.
the class DefaultCommandQueueSync method waitFor.
@Override
public void waitFor(final KsqlEntityList previousCommands, final Class<? extends Statement> statementClass) {
if (mustSync.test(statementClass)) {
final ArrayList<KsqlEntity> reversed = new ArrayList<>(previousCommands);
Collections.reverse(reversed);
reversed.stream().filter(e -> e instanceof CommandStatusEntity).map(CommandStatusEntity.class::cast).map(CommandStatusEntity::getCommandSequenceNumber).findFirst().ifPresent(seqNum -> {
try {
commandQueue.ensureConsumedPast(seqNum, timeout);
} catch (final InterruptedException e) {
throw new KsqlRestException(Errors.serverShuttingDown());
} catch (final TimeoutException e) {
throw new KsqlRestException(Errors.commandQueueCatchUpTimeout(seqNum));
}
});
}
}
use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.
the class StreamedQueryResourceTest method shouldReturn400OnBadStatement.
@Test
public void shouldReturn400OnBadStatement() {
// Given:
when(mockStatementParser.parseSingleStatement(any())).thenThrow(new IllegalArgumentException("some error message"));
// When:
final KsqlRestException e = assertThrows(KsqlRestException.class, () -> testResource.streamQuery(securityContext, new KsqlRequest("query", Collections.emptyMap(), Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context));
// Then:
assertThat(e, exceptionStatusCode(is(BAD_REQUEST.code())));
assertThat(e, exceptionErrorMessage(errorMessage(is("some error message"))));
assertThat(e, exceptionErrorMessage(errorCode(is(ERROR_CODE_BAD_STATEMENT))));
}
use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.
the class StreamedQueryResourceTest method shouldSuggestAlternativesIfPrintTopicDoesNotExist.
@Test
public void shouldSuggestAlternativesIfPrintTopicDoesNotExist() {
// Given:
final PrintTopic cmd = mock(PrintTopic.class);
when(cmd.getTopic()).thenReturn("TEST_TOPIC");
print = PreparedStatement.of("print", cmd);
when(mockStatementParser.<PrintTopic>parseSingleStatement(any())).thenReturn(print);
when(mockKafkaTopicClient.isTopicExists(any())).thenReturn(false);
when(mockKafkaTopicClient.listTopicNames()).thenReturn(ImmutableSet.of("aTopic", "test_topic", "Test_Topic"));
// When:
final KsqlRestException e = assertThrows(KsqlRestException.class, () -> testResource.streamQuery(securityContext, new KsqlRequest(PRINT_TOPIC, Collections.emptyMap(), Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context));
// Then:
assertThat(e, exceptionStatusCode(is(BAD_REQUEST.code())));
assertThat(e, exceptionErrorMessage(errorMessage(containsString("Could not find topic 'TEST_TOPIC', " + "or the KSQL user does not have permissions to list the topic. " + "Topic names are case-sensitive." + System.lineSeparator() + "Did you mean:"))));
assertThat(e, exceptionErrorMessage(errorMessage(containsString("\tprint test_topic;"))));
assertThat(e, exceptionErrorMessage(errorMessage(containsString("\tprint Test_Topic;"))));
}
use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.
the class StreamedQueryResourceTest method shouldThrowOnHandleStatementIfNotConfigured.
@Test
public void shouldThrowOnHandleStatementIfNotConfigured() {
// Given:
testResource = new StreamedQueryResource(mockKsqlEngine, ksqlRestConfig, mockStatementParser, commandQueue, DISCONNECT_CHECK_INTERVAL, COMMAND_QUEUE_CATCHUP_TIMOEUT, activenessRegistrar, Optional.of(authorizationValidator), errorsHandler, denyListPropertyValidator, queryExecutor);
// When:
final KsqlRestException e = assertThrows(KsqlRestException.class, () -> testResource.streamQuery(securityContext, new KsqlRequest("query", Collections.emptyMap(), Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context));
// Then:
assertThat(e, exceptionStatusCode(is(SERVICE_UNAVAILABLE.code())));
assertThat(e, exceptionErrorMessage(errorMessage(Matchers.is("Server initializing"))));
}
use of io.confluent.ksql.rest.server.resources.KsqlRestException in project ksql by confluentinc.
the class StreamedQueryResource method handlePrintTopic.
private EndpointResponse handlePrintTopic(final ServiceContext serviceContext, final Map<String, Object> streamProperties, final PreparedStatement<PrintTopic> statement, final CompletableFuture<Void> connectionClosedFuture) {
final PrintTopic printTopic = statement.getStatement();
final String topicName = printTopic.getTopic();
if (!serviceContext.getTopicClient().isTopicExists(topicName)) {
final Collection<String> possibleAlternatives = findPossibleTopicMatches(topicName, serviceContext);
final String reverseSuggestion = possibleAlternatives.isEmpty() ? "" : possibleAlternatives.stream().map(name -> "\tprint " + name + ";").collect(Collectors.joining(System.lineSeparator(), System.lineSeparator() + "Did you mean:" + System.lineSeparator(), ""));
throw new KsqlRestException(Errors.badRequest("Could not find topic '" + topicName + "', " + "or the KSQL user does not have permissions to list the topic. " + "Topic names are case-sensitive." + reverseSuggestion));
}
final Map<String, Object> propertiesWithOverrides = new HashMap<>(ksqlConfig.getKsqlStreamConfigProps());
propertiesWithOverrides.putAll(streamProperties);
final TopicStreamWriter topicStreamWriter = TopicStreamWriter.create(serviceContext, propertiesWithOverrides, printTopic, disconnectCheckInterval, connectionClosedFuture);
log.info("Printing topic '{}'", topicName);
return EndpointResponse.ok(topicStreamWriter);
}
Aggregations