Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.
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
2 changes: 1 addition & 1 deletion common/exception/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public class ErrorMessage extends com.vaticle.typedb.common.exception.ErrorMessa
new ErrorMessage(40, "'%s' is not a valid Type label. Type labels must start with a letter, and may contain only letters, numbers, '-' and '_'.");


private static final String codePrefix = "GQL";
private static final String codePrefix = "TQL";
private static final String messagePrefix = "TypeQL Error";

public ErrorMessage(int codeNumber, String messageBody) {
Expand Down
4 changes: 4 additions & 0 deletions parser/ErrorListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public boolean hasErrors() {
return !errors.isEmpty();
}

public void clearErrors() {
errors.clear();
}

@Override
public String toString() {
return errors.stream().map(SyntaxError::toString).collect(Collectors.joining("\n"));
Expand Down
1 change: 1 addition & 0 deletions parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private <CONTEXT extends ParserRuleContext, RETURN> RETURN parse(
// DefaultErrorStrategy + LL_EXACT_AMBIG_DETECTION
// This was not set to default parsing strategy, but it is useful
// to produce detailed/useful error message
errorListener.clearErrors();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling logic is as follows:

  1. Attach error listener to parser - whenever a parser error is thrown, it gets stored in the Listener
  2. Parse query with optimised parser (returns less useful error messages)
  3. If parsing fails, re-parse query with strict parser (returns more detailed error messages)
  4. If parsing fails (which it will), dump all errors in the Listener to an exception string.

Trouble is, this flow causes 2 errors (which may not necessarily be identical, but often are) to be piped into the Listener, and they both make it into the output.

To fix this issue, we clean the first (less precise) error from the Listener before storing the second, more precise one.

parser.setErrorHandler(new DefaultErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
queryContext = parserMethod.apply(parser);
Expand Down