Extract line and column from ParseError

This commit is contained in:
sunli 2020-04-02 12:37:04 +08:00
parent 10105b8a09
commit b3525356c9
2 changed files with 26 additions and 3 deletions

View File

@ -228,8 +228,22 @@ pub struct RuleError {
impl From<ParseError> for Error {
fn from(err: ParseError) -> Self {
let msg = err.to_string();
let mut s = msg.splitn(2, "\n");
let first = s.next().unwrap();
let ln = &first[first.rfind(" ").unwrap() + 1..];
let (line, column) = {
let mut s = ln.splitn(2, ":");
(
s.next().unwrap().parse().unwrap(),
s.next().unwrap().parse().unwrap(),
)
};
let tail = s.next().unwrap();
Error::Parse {
message: err.to_string(),
line,
column,
message: tail.to_string(),
}
}
}
@ -238,7 +252,11 @@ impl From<ParseError> for Error {
#[derive(Debug, Error)]
pub enum Error {
#[error("Parse error: {message}")]
Parse { message: String },
Parse {
line: usize,
column: usize,
message: String,
},
#[error("Query error: {err}")]
Query {

View File

@ -87,10 +87,15 @@ impl<'a> Serialize for GQLError<'a> {
S: Serializer,
{
match self.0 {
Error::Parse { message } => {
Error::Parse {
line,
column,
message,
} => {
let mut seq = serializer.serialize_seq(Some(1))?;
seq.serialize_element(&serde_json::json! ({
"message": message,
"locations": [{"line": line, "column": column}]
}))?;
seq.end()
}