-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error report feature to cynic-parser (#833)
It's not perfect but it is a start
- Loading branch information
Showing
6 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use core::fmt; | ||
|
||
use ariadne::{Config, Label, ReportKind, Source}; | ||
|
||
use crate::Error; | ||
|
||
pub struct Report<'doc> { | ||
inner: ariadne::Report<'static>, | ||
document: &'doc str, | ||
} | ||
|
||
impl Error { | ||
pub fn to_report<'a>(&self, document: &'a str) -> Report<'a> { | ||
let (message, label, note) = self.components(); | ||
|
||
let mut builder = ariadne::Report::build(ReportKind::Error, (), 0) | ||
.with_code(3) | ||
.with_message(message) | ||
.with_label(label) | ||
.with_config(Config::default().with_color(false)); | ||
|
||
if let Some(note) = note { | ||
builder.set_note(note) | ||
} | ||
|
||
let inner = builder.finish(); | ||
|
||
Report { inner, document } | ||
} | ||
|
||
fn components(&self) -> (String, Label, Option<String>) { | ||
match self { | ||
Error::InvalidToken { location } => ( | ||
"invalid token".into(), | ||
Label::new(*location..*location).with_message("could not understand this token"), | ||
None, | ||
), | ||
Error::UnrecognizedEof { location, expected } => ( | ||
"unexpected eof".into(), | ||
Label::new(*location..*location).with_message("expected another token here"), | ||
Some(format!("expected one of {}", expected.join(", "))), | ||
), | ||
Error::UnrecognizedToken { | ||
token: (start, token, end), | ||
expected, | ||
} => ( | ||
format!("unexpected {}", token), | ||
Label::new(*start..*end).with_message("didn't expect to see this"), | ||
Some(format!("expected one of {}", expected.join(", "))), | ||
), | ||
Error::ExtraToken { | ||
token: (start, token, end), | ||
} => ( | ||
format!("extra {}", token), | ||
Label::new(*start..*end).with_message("we expected the document to end here"), | ||
None, | ||
), | ||
Error::User { error } => { | ||
let span = error.span(); | ||
( | ||
"invalid token".into(), | ||
Label::new(span.start..span.end).with_message("could not parse a token here"), | ||
None, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Report<'_> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let mut output = Vec::<u8>::new(); | ||
self.inner | ||
.write(Source::from(self.document), &mut output) | ||
.unwrap(); | ||
let s = String::from_utf8_lossy(&output); | ||
|
||
write!(f, "{s}") | ||
} | ||
} | ||
|
||
impl fmt::Debug for Report<'_> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{self}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use cynic_parser::Error; | ||
|
||
#[test] | ||
fn test_report() { | ||
let document = "type Blah {}"; | ||
let report = cynic_parser::parse_type_system_document(document) | ||
.map(|_| ()) | ||
.unwrap_err() | ||
.to_report(document); | ||
|
||
insta::assert_display_snapshot!(report, @r###" | ||
[03] Error: unexpected closing brace ('}') | ||
╭─[<unknown>:1:1] | ||
│ | ||
1 │ type Blah {} | ||
│ ┬ | ||
│ ╰── didn't expect to see this | ||
│ | ||
│ Note: expected one of "enum", BlockStringLiteral, RawIdent, StringLiteral, directive, extend, false, implements, input, interface, mutation, null, on, query, repeatable, scalar, schema, subscription, true, ty, union | ||
───╯ | ||
"###); | ||
} |