private List<Comment> toComment(String description) { if (description == null) return Collections.emptyList(); Comment comment = new Comment(description, new SourceLocation(1, 1)); return Collections.singletonList(comment); }
.map(l -> baseLocation .map(base -> new SourceLocation( base.getLine() + l.getLine(), base.getColumn() + l.getColumn())) .orElse(null)) .collect(Collectors.toList()))
.map(l -> baseLocation .map(base -> new SourceLocation( base.getLine() + l.getLine(), base.getColumn() + l.getColumn())) .orElse(null)) .collect(Collectors.toList()))
private String mkMessage(String msg, String offendingToken, SourceLocation location) { StringBuilder sb = new StringBuilder(); sb.append("Invalid Syntax :"); if (msg != null) { sb.append(" ").append(msg); } if (offendingToken != null) { sb.append(String.format(" offending token '%s'", offendingToken)); } if (location != null) { sb.append(String.format(" at line %d column %d", location.getLine(), location.getColumn())); } return sb.toString(); }
private SourceLocation mkLocation(int line, int column) { return new SourceLocation(line, column); }
protected List<Comment> getCommentOnChannel(List<Token> refChannel) { List<Comment> comments = new ArrayList<>(); for (Token refTok : refChannel) { String text = refTok.getText(); // we strip the leading hash # character but we don't trim because we don't // know the "comment markup". Maybe its space sensitive, maybe its not. So // consumers can decide that if (text == null) { continue; } text = text.replaceFirst("^#", ""); comments.add(new Comment(text, new SourceLocation(refTok.getLine(), refTok.getCharPositionInLine()))); } return comments; }
protected SourceLocation getSourceLocation(Token token) { String sourceName = token.getTokenSource().getSourceName(); if (IntStream.UNKNOWN_SOURCE_NAME.equals(sourceName)) { // UNKNOWN_SOURCE_NAME is Antrl's way of indicating that no source name was given during parsing -- // which is the case when queries and other operations are parsed. We don't want this hardcoded // '<unknown>' sourceName to leak to clients when the response is serialized as JSON, so we null it. sourceName = null; } return new SourceLocation(token.getLine(), token.getCharPositionInLine() + 1, sourceName); }
private String mkMessage(String msg, String offendingToken, SourceLocation location) { StringBuilder sb = new StringBuilder(); sb.append("Invalid Syntax :"); if (msg != null) { sb.append(" ").append(msg); } if (offendingToken != null) { sb.append(String.format(" offending token '%s'", offendingToken)); } if (location != null) { sb.append(String.format(" at line %d column %d", location.getLine(), location.getColumn())); } return sb.toString(); }
InvalidSyntaxException mkMoreTokensException(Token token) { SourceLocation sourceLocation = new SourceLocation(token.getLine(), token.getCharPositionInLine()); String sourcePreview = mkPreview(token.getLine()); return new InvalidSyntaxException(sourceLocation, "There are more tokens in the query that have not been consumed", sourcePreview, token.getText(), null); }
private InvalidSyntaxException mkException(Parser recognizer, RecognitionException cause) { String sourcePreview = null; String offendingToken = null; SourceLocation sourceLocation = null; Token currentToken = recognizer.getCurrentToken(); if (currentToken != null) { int line = currentToken.getLine(); int column = currentToken.getCharPositionInLine(); offendingToken = currentToken.getText(); sourcePreview = mkPreview(line); sourceLocation = new SourceLocation(line, column, sourceName); } return new InvalidSyntaxException(sourceLocation, null, sourcePreview, offendingToken, cause); }
@Override public SourceLocation getSourceLocation() { return new SourceLocation(0, 0); }
@Override public String getMessage() { String result = ""; for (GraphQLError error : errors) { result += error.getErrorType() + ": " + error.getMessage() + "\n"; for (SourceLocation sourceLocation : error.getLocations()) { result += " at line: " + sourceLocation.getLine() + " column: " + sourceLocation.getColumn(); } } return result; } }
private List<Comment> toComment(String description) { if (description == null) return Collections.emptyList(); Comment comment = new Comment(description, new SourceLocation(1, 1)); return Collections.singletonList(comment); }
/** * Creates a new {@link QueryError} data object * based on the provided {@link GraphQLError}. * * @param graphQLError the graphql error to convert * @return the converted query error data object */ public static QueryError convertToQueryError(GraphQLError graphQLError) { return new QueryError(graphQLError.getErrorType().name(), graphQLError.getMessage(), graphQLError.getLocations().stream().map(location -> new ErrorLocation(location.getLine(), location.getColumn())).collect(Collectors.toList())); } }
public ValidatedQuery validateQuery(String query) { ValidatedQuery result = new ValidatedQuery(); result.errors = validationErrors; Document document; try { document = parser.parseDocument(query); result.parsedQuery = document; } catch (Exception e) { ValidationError err = new ValidationError(ValidationErrorType.InvalidSyntax, new SourceLocation(0, 0), "Invalid query syntax."); validationErrors.add(err); result.valid = false; return result; } validationErrors.addAll(validator.validateDocument(schema, document)); if (validationErrors.size() > 0) { result.valid = false; return result; } result.valid = true; return result; }