Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve print topic error message when topic does not exist #3464

Merged
merged 3 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class KsStateStoreTest {
.build();

@Rule
public final Timeout timeout = Timeout.seconds(1);
public final Timeout timeout = Timeout.seconds(10);

@Rule
public final ExpectedException expectedException = ExpectedException.none();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
import io.confluent.ksql.util.TransientQueryMetadata;
import io.confluent.ksql.version.metrics.ActivenessRegistrar;
import java.time.Duration;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -232,24 +234,29 @@ private Response handlePrintTopic(
final PreparedStatement<PrintTopic> statement
) {
final PrintTopic printTopic = statement.getStatement();
final String topicName = printTopic.getTopic().toString();
final String topicName = printTopic.getTopic();

if (!serviceContext.getTopicClient().isTopicExists(topicName)) {
String reverseSuggestion = "";
final String nameReversedCase = reverseCase(topicName);
if (serviceContext.getTopicClient().isTopicExists(nameReversedCase)) {
reverseSuggestion = "Did you mean '" + nameReversedCase + "'?" + System.lineSeparator();
}
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(String.format(
"Could not find topic '%s', "
+ "or the KSQL user does not have permissions to list the topic."
+ System.lineSeparator()
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
+ "KSQL will treat unquoted topic names as uppercase."
+ System.lineSeparator()
+ "To print a case-sensitive topic use quotes, for example: print \'Topic\';",
topicName)));
));
}

final Map<String, Object> propertiesWithOverrides =
Expand All @@ -267,17 +274,13 @@ private Response handlePrintTopic(
return Response.ok().entity(topicStreamWriter).build();
}

private String reverseCase(final String str) {
final char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
final char c = chars[i];
if (Character.isUpperCase(c)) {
chars[i] = Character.toLowerCase(c);
} else {
chars[i] = Character.toUpperCase(c);
}
}
return new String(chars);
private static Collection<String> findPossibleTopicMatches(
final String topicName,
final ServiceContext serviceContext
) {
return serviceContext.getTopicClient().listTopicNames().stream()
.filter(name -> name.equalsIgnoreCase(topicName))
.collect(Collectors.toSet());
}
}

Expand Down
Loading