Skip to content

Commit

Permalink
feat: use ariadne for better error display (#163)
Browse files Browse the repository at this point in the history
* feat: use `ariadne` for better error display

* Lint
  • Loading branch information
hyf0 committed Nov 4, 2023
1 parent 58dff4e commit 2cd7d49
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 4 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ futures = "0.3.25"
itertools = "0.10.5"
thiserror = "1.0.50"
smallvec = "1.11.1"
ariadne = "0.3.0"
2 changes: 1 addition & 1 deletion crates/rolldown/tests/common/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Case {
[
Cow::Owned(format!("## {}\n", error.code())),
"```text".into(),
Cow::Owned(error.to_string()),
Cow::Owned(format!("{}", error.to_diagnostic().print_to_string())),
"```".into(),
]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ input_file: crates/rolldown/tests/fixtures/errors/unresolved_entry
## UNRESOLVED_ENTRY

```text
Could not resolve entry module "tests/fixtures/errors/unresolved_entry/main.js"
[UNRESOLVED_ENTRY] Error: Cannot resolve entry module "tests/fixtures/errors/unresolved_entry/main.js"
```
1 change: 1 addition & 0 deletions crates/rolldown_error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ repository.workspace = true
sugar_path = { workspace = true }
scoped-tls = { workspace = true }
thiserror = { workspace = true }
ariadne = { workspace = true }
49 changes: 49 additions & 0 deletions crates/rolldown_error/src/diagnostic.rs
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)
}
}
24 changes: 24 additions & 0 deletions crates/rolldown_error/src/error/impl_to_diagnostic.rs
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()
},
}
}
}
1 change: 1 addition & 0 deletions crates/rolldown_error/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
};

pub mod external_entry;
pub mod impl_to_diagnostic;
pub mod unresolved_entry;
pub mod unresolved_import;

Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown_error/src/error/unresolved_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::PathExt;
use std::path::PathBuf;
use thiserror::Error;
#[derive(Error, Debug)]
#[error("Could not resolve entry module {:?}", unresolved_id.relative_display())]
#[error("Cannot resolve entry module {:?}", unresolved_id.relative_display())]
pub struct UnresolvedEntry {
pub(crate) unresolved_id: PathBuf,
}
4 changes: 3 additions & 1 deletion crates/rolldown_error/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod diagnostic;
mod error;
mod error_code;
mod utils;

use std::path::Path;

use sugar_path::SugarPath;

pub use crate::error::BuildError;
pub use crate::{diagnostic::Diagnostic, error::BuildError};

trait PathExt {
fn relative_display(&self) -> String;
Expand Down

0 comments on commit 2cd7d49

Please sign in to comment.