Skip to content

Commit

Permalink
fix: Don’t swallow exception and don’t overwrite previous valid trans…
Browse files Browse the repository at this point in the history
…lation when a later translator fails.

Signed-off-by: Michael Simons <[email protected]>
  • Loading branch information
michael-simons committed Jan 10, 2025
1 parent e98cab4 commit 46ef2c5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions neo4j-jdbc/src/main/java/org/neo4j/jdbc/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -862,26 +862,36 @@ public String apply(String statement) {
if (this.translators.size() == 1) {
return this.translators.get(0).translate(statement, this.metaData);
}

Throwable lastException = null;
String result = null;
String in = statement;
for (var translator : this.translators) {
var in = (result != null) ? result : statement;
// Break out early if any of the translators indicates a final Cypher
// statement
if (StatementImpl.forceCypher(in)) {
return in;
}
try {
result = translator.translate(in, this.metaData);
// Don't overwrite previous results if the intermediate is null
if (result != null) {
in = result;
}
}
catch (IllegalArgumentException ex) {
this.warningSink.accept(new SQLWarning(
"Translator %s failed to translate `%s`".formatted(translator.getClass().getName(), in),
ex));
if (ex.getCause() != null) {
lastException = ex.getCause();
}
}
}

if (result == null) {
throw new IllegalStateException("No suitable translator for input `%s`".formatted(statement));
throw new IllegalStateException("No suitable translator for input `%s`".formatted(statement),
lastException);
}

return result;
Expand Down

0 comments on commit 46ef2c5

Please sign in to comment.