Skip to content

Commit

Permalink
feat: store source in BuildError (#328)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->

### Test Plan

<!-- e.g. is there anything you'd like reviewers to focus on? -->

---
  • Loading branch information
hyf0 committed Nov 19, 2023
1 parent 158b11d commit c34d3cd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
21 changes: 20 additions & 1 deletion crates/rolldown_error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type StaticStr = Cow<'static, str>;
#[derive(Debug)]
pub struct BuildError {
inner: Box<dyn BuildErrorLike>,
source: Option<Box<dyn std::error::Error + 'static + Send + Sync>>,
}

fn _assert_build_error_send_sync() {
Expand All @@ -27,15 +28,33 @@ impl Display for BuildError {
}
}

impl std::error::Error for BuildError {
// clippy::option_map_or_none: Cool. Finally, catch a error of clippy. Clippy suggest using `self.source.as_ref().map(|source| source.as_ref())`
// which will cause type mismatch error.
#[allow(clippy::option_map_or_none)]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source.as_ref().map_or(None, |source| Some(source.as_ref()))
}
}

impl BuildError {
pub fn code(&self) -> &'static str {
self.inner.code()
}

#[must_use]
pub fn with_source(
mut self,
source: impl Into<Box<dyn std::error::Error + 'static + Send + Sync>>,
) -> Self {
self.source = Some(source.into());
self
}

// --- private

fn new_inner(inner: impl Into<Box<dyn BuildErrorLike>>) -> Self {
Self { inner: inner.into() }
Self { inner: inner.into(), source: None }
}

// --- Aligned with rollup
Expand Down
14 changes: 10 additions & 4 deletions crates/rolldown_resolver/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ impl<F: FileSystem + Default> Resolver<F> {
resolved: info.path().to_string_lossy().to_string().into(),
module_type: calc_module_type(&info),
}),
Err(_err) => importer.map_or_else(
|| Err(BuildError::unresolved_entry(specifier)),
|importer| Err(BuildError::unresolved_import(specifier.to_string(), importer.as_path())),
),
Err(err) => {
if let Some(importer) = importer {
Err(
BuildError::unresolved_import(specifier.to_string(), importer.as_path())
.with_source(err),
)
} else {
Err(BuildError::unresolved_entry(specifier).with_source(err))
}
}
}
}
}
Expand Down

0 comments on commit c34d3cd

Please sign in to comment.