|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.ql; |
| 8 | + |
| 9 | +import org.elasticsearch.rest.RestStatus; |
| 10 | +import org.elasticsearch.xpack.ql.tree.Source; |
| 11 | + |
| 12 | +import static org.elasticsearch.common.logging.LoggerMessageFormat.format; |
| 13 | + |
| 14 | +public class ParsingException extends QlClientException { |
| 15 | + private final int line; |
| 16 | + private final int charPositionInLine; |
| 17 | + |
| 18 | + public ParsingException(String message, Exception cause, int line, int charPositionInLine) { |
| 19 | + super(message, cause); |
| 20 | + this.line = line; |
| 21 | + this.charPositionInLine = charPositionInLine; |
| 22 | + } |
| 23 | + |
| 24 | + public ParsingException(String message, Object... args) { |
| 25 | + this(Source.EMPTY, message, args); |
| 26 | + } |
| 27 | + |
| 28 | + public ParsingException(Source source, String message, Object... args) { |
| 29 | + super(message, args); |
| 30 | + this.line = source.source().getLineNumber(); |
| 31 | + this.charPositionInLine = source.source().getColumnNumber(); |
| 32 | + } |
| 33 | + |
| 34 | + public ParsingException(Exception cause, Source source, String message, Object... args) { |
| 35 | + super(cause, message, args); |
| 36 | + this.line = source.source().getLineNumber(); |
| 37 | + this.charPositionInLine = source.source().getColumnNumber(); |
| 38 | + } |
| 39 | + |
| 40 | + public int getLineNumber() { |
| 41 | + return line; |
| 42 | + } |
| 43 | + |
| 44 | + public int getColumnNumber() { |
| 45 | + return charPositionInLine + 1; |
| 46 | + } |
| 47 | + |
| 48 | + public String getErrorMessage() { |
| 49 | + return super.getMessage(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public RestStatus status() { |
| 54 | + return RestStatus.BAD_REQUEST; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getMessage() { |
| 59 | + return format("line {}:{}: {}", getLineNumber(), getColumnNumber(), getErrorMessage()); |
| 60 | + } |
| 61 | +} |
0 commit comments