Skip to content

Commit

Permalink
fix: improve error message on query or print statement on /ksql (#3337)
Browse files Browse the repository at this point in the history
If someone issues a `PRINT` or `SELECT` statement to the normal HTTP `/ksql` endpoint it returns an error. The previous error talked about `RUN SCRIPT`, which is misleading. We don't even have `RUN SCRIPT` anymore.

New error message just tells users to target such statements to the websocket endpoint `/query`
  • Loading branch information
big-andy-coates committed Sep 17, 2019
1 parent 68a1cc3 commit dae28eb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,11 @@ public void shouldFailBareContinuousQuery() {
expectedException.expect(KsqlRestException.class);
expectedException.expect(exceptionStatusCode(is(Code.BAD_REQUEST)));
expectedException.expect(exceptionStatementErrorMessage(errorMessage(is(
"RUN SCRIPT cannot be used with the following statements: \n"
+ "* PRINT\n"
+ "* SELECT"))));
"The following statement types should be issued to the websocket endpoint '/query':"
+ System.lineSeparator()
+ "\t* PRINT"
+ System.lineSeparator()
+ "\t* SELECT"))));
expectedException.expect(exceptionStatementErrorMessage(statement(is(
"SELECT * FROM test_table EMIT CHANGES;"))));

Expand Down Expand Up @@ -661,9 +663,11 @@ public void shouldFailPrintTopic() {
expectedException.expect(KsqlRestException.class);
expectedException.expect(exceptionStatusCode(is(Code.BAD_REQUEST)));
expectedException.expect(exceptionStatementErrorMessage(errorMessage(is(
"RUN SCRIPT cannot be used with the following statements: \n"
+ "* PRINT\n"
+ "* SELECT"))));
"The following statement types should be issued to the websocket endpoint '/query':"
+ System.lineSeparator()
+ "\t* PRINT"
+ System.lineSeparator()
+ "\t* SELECT"))));
expectedException.expect(exceptionStatementErrorMessage(statement(is(
"PRINT 'orders-topic';"))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,58 @@
import static org.mockito.Mockito.mock;

import com.google.common.collect.ImmutableMap;
import io.confluent.ksql.KsqlExecutionContext;
import io.confluent.ksql.parser.KsqlParser.PreparedStatement;
import io.confluent.ksql.parser.tree.PrintTopic;
import io.confluent.ksql.rest.server.TemporaryEngine;
import io.confluent.ksql.rest.server.resources.KsqlRestException;
import io.confluent.ksql.services.ServiceContext;
import io.confluent.ksql.statement.ConfiguredStatement;
import io.confluent.ksql.util.KsqlConfig;
import org.eclipse.jetty.http.HttpStatus.Code;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class PrintTopicValidatorTest {

@Rule public final TemporaryEngine engine = new TemporaryEngine();
@Rule public final ExpectedException expectedException = ExpectedException.none();
private static final KsqlConfig CONFIG = new KsqlConfig(ImmutableMap.of());

@Rule
public final ExpectedException expectedException = ExpectedException.none();

@Mock
private KsqlExecutionContext ksqlEngine;
@Mock
private ServiceContext serviceContext;


@Test
public void shouldThrowExceptionOnPrintTopic() {
// Expect:
// Given:
final ConfiguredStatement<PrintTopic> query = ConfiguredStatement.of(
PreparedStatement.of("PRINT 'topic';", mock(PrintTopic.class)),
ImmutableMap.of(),
CONFIG
);

// When::
expectedException.expect(KsqlRestException.class);
expectedException.expect(exceptionStatusCode(is(Code.BAD_REQUEST)));
expectedException.expect(exceptionStatementErrorMessage(errorMessage(containsString(
"RUN SCRIPT cannot be used with the following statements: \n"
+ "* PRINT\n"
+ "* SELECT"))));
"The following statement types should be issued to the websocket endpoint '/query'"
))));
expectedException.expect(exceptionStatementErrorMessage(statement(containsString(
"PRINT 'topic';"))));

// When:
CustomValidators.PRINT_TOPIC.validate(
ConfiguredStatement.of(
PreparedStatement.of("PRINT 'topic';", mock(PrintTopic.class)),
ImmutableMap.of(),
engine.getKsqlConfig()),
engine.getEngine(),
engine.getServiceContext()
query,
ksqlEngine,
serviceContext
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,27 @@ public class QueryValidatorTest {

@Test
public void shouldThrowExceptionOnQueryEndpoint() {
// Expect:
// Expect:
// Given:
final ConfiguredStatement<Query> query = ConfiguredStatement.of(
PreparedStatement.of("SELECT * FROM test_table;", mock(Query.class)),
ImmutableMap.of(),
engine.getKsqlConfig()
);

// Then:
expectedException.expect(KsqlRestException.class);
expectedException.expect(exceptionStatusCode(is(Code.BAD_REQUEST)));
expectedException.expect(exceptionStatementErrorMessage(errorMessage(containsString(
"RUN SCRIPT cannot be used with the following statements: \n"
+ "* PRINT\n"
+ "* SELECT"))));
"The following statement types should be issued to the websocket endpoint '/query'"
))));
expectedException.expect(exceptionStatementErrorMessage(statement(containsString(
"SELECT * FROM test_table"))));

// When:
CustomValidators.QUERY_ENDPOINT.validate(
ConfiguredStatement.of(
PreparedStatement.of("SELECT * FROM test_table;", mock(Query.class)),
ImmutableMap.of(),
engine.getKsqlConfig()
),
query,
engine.getEngine(),
engine.getServiceContext()
);
}


}
10 changes: 6 additions & 4 deletions ksql-rest-model/src/main/java/io/confluent/ksql/rest/Errors.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,12 @@ public static Response queryEndpoint(final String statementText) {
return Response
.status(BAD_REQUEST)
.entity(new KsqlStatementErrorMessage(
ERROR_CODE_QUERY_ENDPOINT,
"RUN SCRIPT cannot be used with the following statements: \n"
+ "* PRINT\n"
+ "* SELECT",
ERROR_CODE_QUERY_ENDPOINT,
"The following statement types should be issued to the websocket endpoint '/query':"
+ System.lineSeparator()
+ "\t* PRINT"
+ System.lineSeparator()
+ "\t* SELECT",
statementText, new KsqlEntityList()))
.build();
}
Expand Down

0 comments on commit dae28eb

Please sign in to comment.