Skip to content

Commit

Permalink
fix: Print only failed line on parsing exception (#8282)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrrr authored Nov 2, 2021
1 parent a7c78b5 commit 701db5f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ksqldb-cli/src/test/java/io/confluent/ksql/cli/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,27 @@ public void shouldRunScriptOnRunScript() throws Exception {
containsString("Created query with ID CSAS_SHOULDRUNSCRIPT"));
}

@Test
public void shouldPrintOnlyFailedStatementFromScriptFile() throws Exception {
// Given:
final File scriptFile = TMP.newFile("script.sql");
Files.write(scriptFile.toPath(), (""
+ "drop stream if exists s1;\n"
+ "create stream if not exist s1(id int) with (kafka_topic='s1', value_format='json', "
+ "partitions=1);\n").getBytes(StandardCharsets.UTF_8));

// When:
localCli.runScript(scriptFile.getPath());

// Then:
final String out = terminal.getOutputString();
final String expected = "Statement: create stream if not exist s1(id int) "
+ "with (kafka_topic='s1', value_format='json', partitions=1);\n"
+ "Caused by: line 2:22: no viable alternative at input 'create stream if not";
assertThat(out, containsString(expected));
assertThat(out, not(containsString("drop stream if exists")));
}

@Test
public void shouldUpdateCommandSequenceNumber() throws Exception {
// Given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public List<ParsedStatement> parse(final String sql) {
.map(DefaultKsqlParser::parsedStatement)
.collect(Collectors.toList());

} catch (final ParsingException e) {
// ParsingException counts lines starting from 1
final String failedLine = sql.split(System.lineSeparator())[e.getLineNumber() - 1];
throw new ParseFailedException(e.getMessage(), failedLine, e);
} catch (final Exception e) {
throw new ParseFailedException(e.getMessage(), sql, e);
}
Expand Down

0 comments on commit 701db5f

Please sign in to comment.