-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use
ariadne
for better error display (#163)
* feat: use `ariadne` for better error display * Lint
- Loading branch information
Showing
10 changed files
with
100 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ futures = "0.3.25" | |
itertools = "0.10.5" | ||
thiserror = "1.0.50" | ||
smallvec = "1.11.1" | ||
ariadne = "0.3.0" |
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,49 @@ | ||
use ariadne::{sources, Config, Label, Report, ReportBuilder, ReportKind}; | ||
use std::{fmt::Display, ops::Range}; | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct Diagnostic { | ||
pub(crate) code: &'static str, | ||
pub(crate) summary: String, | ||
pub(crate) files: Vec<(String, String)>, | ||
pub(crate) labels: Vec<Label<(String, Range<usize>)>>, | ||
} | ||
|
||
impl Diagnostic { | ||
fn init_builder( | ||
code: &'static str, | ||
message: String, | ||
labels: Vec<Label<(String, Range<usize>)>>, | ||
) -> ReportBuilder<'static, (String, Range<usize>)> { | ||
let mut builder = Report::<(String, Range<usize>)>::build(ReportKind::Error, "", 0) | ||
.with_code(code) | ||
.with_message(message); | ||
|
||
for label in labels { | ||
builder = builder.with_label(label); | ||
} | ||
|
||
builder | ||
} | ||
pub fn print(self) { | ||
let builder = Self::init_builder(self.code, self.summary, self.labels); | ||
builder.finish().print(sources(self.files)).unwrap(); | ||
} | ||
|
||
pub fn print_to_string(self) -> String { | ||
let builder = Self::init_builder(self.code, self.summary, self.labels); | ||
let mut output = Vec::new(); | ||
builder | ||
.with_config(Config::default().with_color(false)) | ||
.finish() | ||
.write_for_stdout(sources(self.files.clone()), &mut output) | ||
.unwrap(); | ||
String::from_utf8(output).unwrap() | ||
} | ||
} | ||
|
||
impl Display for Diagnostic { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}: {}", self.code, self.summary) | ||
} | ||
} |
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,24 @@ | ||
use crate::{BuildError, Diagnostic}; | ||
|
||
impl BuildError { | ||
pub fn to_diagnostic(&self) -> Diagnostic { | ||
let code = self.code(); | ||
|
||
match self { | ||
Self::UnresolvedEntry(err) => { | ||
Diagnostic { code, summary: err.to_string(), ..Default::default() } | ||
} | ||
Self::ExternalEntry(err) => { | ||
Diagnostic { code, summary: err.to_string(), ..Default::default() } | ||
} | ||
Self::UnresolvedImport(err) => { | ||
Diagnostic { code, summary: err.to_string(), ..Default::default() } | ||
} | ||
Self::Napi { status, reason } => Diagnostic { | ||
code, | ||
summary: format!("Napi error: {status}: {reason}"), | ||
..Default::default() | ||
}, | ||
} | ||
} | ||
} |
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