Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Improve BadJpqlGrammarException by including the underlying grammar.
  • Loading branch information
mp911de committed Jan 9, 2025
1 parent 90f7ed9 commit a3cf499
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ class BadJpqlGrammarErrorListener extends BaseErrorListener {

private final String query;

private final String grammar;

BadJpqlGrammarErrorListener(String query) {
this(query, "JPQL");
}

BadJpqlGrammarErrorListener(String query, String grammar) {
this.query = query;
this.grammar = grammar;
}

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
String msg, RecognitionException e) {
throw new BadJpqlGrammarException("Line " + line + ":" + charPositionInLine + " " + msg, query, null);
throw new BadJpqlGrammarException("Line " + line + ":" + charPositionInLine + " " + msg, grammar, query, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public class BadJpqlGrammarException extends InvalidDataAccessResourceUsageExcep
private final String jpql;

public BadJpqlGrammarException(String message, String jpql, @Nullable Throwable cause) {
super(message + "; Bad JPQL grammar [" + jpql + "]", cause);
this(message, jpql, "JPQL", cause);
}

BadJpqlGrammarException(String message, String grammar, String jpql, @Nullable Throwable cause) {
super(message + "; Bad " + grammar + " grammar [" + jpql + "]", cause);
this.jpql = jpql;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ static <P extends Parser> ParserRuleContext parse(String query, Function<CharStr
Lexer lexer = lexerFactoryFunction.apply(CharStreams.fromString(query));
P parser = parserFactoryFunction.apply(new CommonTokenStream(lexer));

configureParser(query, lexer, parser);
String grammar = lexer.getGrammarFileName();
int dot = grammar.lastIndexOf('.');
if (dot != -1) {
grammar = grammar.substring(0, dot);
}

configureParser(query, grammar.toUpperCase(), lexer, parser);

return parseFunction.apply(parser);
}
Expand All @@ -84,9 +90,9 @@ static <P extends Parser> ParserRuleContext parse(String query, Function<CharStr
* @param lexer
* @param parser
*/
static void configureParser(String query, Lexer lexer, Parser parser) {
static void configureParser(String query, String grammar, Lexer lexer, Parser parser) {

BadJpqlGrammarErrorListener errorListener = new BadJpqlGrammarErrorListener(query);
BadJpqlGrammarErrorListener errorListener = new BadJpqlGrammarErrorListener(query, grammar);

lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
Expand Down

0 comments on commit a3cf499

Please sign in to comment.