-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
trait BuildErrorLike
(#327)
<!-- Thank you for contributing! --> ### Description - Each error should defined as self contained. It includes it's error code and message. - I used `enum` for exposing error kind to rust users of rolldown, and now I think it seems meaningless to care what the errors are. - However, I haven't think it through. Let's see when this need is really requires. <!-- 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
Showing
8 changed files
with
108 additions
and
63 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,55 +1,64 @@ | ||
use std::{ | ||
borrow::Cow, | ||
fmt::Display, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use miette::Diagnostic; | ||
use thiserror::Error; | ||
|
||
use crate::error_kind::{ | ||
external_entry::ExternalEntry, unresolved_entry::UnresolvedEntry, | ||
unresolved_import::UnresolvedImport, ErrorKind, | ||
unresolved_import::UnresolvedImport, BuildErrorLike, NapiError, | ||
}; | ||
|
||
type StaticStr = Cow<'static, str>; | ||
|
||
#[derive(Error, Debug, Diagnostic)] | ||
#[error(transparent)] | ||
#[diagnostic(transparent)] | ||
pub struct BuildError(ErrorKind); | ||
#[derive(Debug)] | ||
pub struct BuildError { | ||
inner: Box<dyn BuildErrorLike>, | ||
} | ||
|
||
fn _assert_build_error_send_sync() { | ||
fn _assert_send_sync<T: Send + Sync>() {} | ||
_assert_send_sync::<BuildError>(); | ||
} | ||
|
||
impl Display for BuildError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.inner.message().fmt(f) | ||
} | ||
} | ||
|
||
impl BuildError { | ||
pub fn new_with_kind(kind: ErrorKind) -> Self { | ||
Self(kind) | ||
pub fn code(&self) -> &'static str { | ||
self.inner.code() | ||
} | ||
|
||
// --- private | ||
|
||
fn new_inner(inner: impl Into<Box<dyn BuildErrorLike>>) -> Self { | ||
Self { inner: inner.into() } | ||
} | ||
|
||
// --- Aligned with rollup | ||
pub fn entry_cannot_be_external(unresolved_id: impl AsRef<Path>) -> Self { | ||
Self::new_with_kind(ErrorKind::ExternalEntry( | ||
ExternalEntry { id: unresolved_id.as_ref().to_path_buf() }.into(), | ||
)) | ||
Self::new_inner(ExternalEntry { id: unresolved_id.as_ref().to_path_buf() }) | ||
} | ||
|
||
pub fn unresolved_entry(unresolved_id: impl AsRef<Path>) -> Self { | ||
Self::new_with_kind(ErrorKind::UnresolvedEntry( | ||
UnresolvedEntry { unresolved_id: unresolved_id.as_ref().to_path_buf() }.into(), | ||
)) | ||
Self::new_inner(UnresolvedEntry { unresolved_id: unresolved_id.as_ref().to_path_buf() }) | ||
} | ||
|
||
pub fn unresolved_import(specifier: impl Into<StaticStr>, importer: impl Into<PathBuf>) -> Self { | ||
Self::new_with_kind(ErrorKind::UnresolvedImport( | ||
UnresolvedImport { specifier: specifier.into(), importer: importer.into() }.into(), | ||
)) | ||
Self::new_inner(UnresolvedImport { specifier: specifier.into(), importer: importer.into() }) | ||
} | ||
|
||
// --- rolldown specific | ||
pub fn napi_error(status: String, reason: String) -> Self { | ||
Self::new_with_kind(ErrorKind::Napi { status, reason }) | ||
Self::new_inner(NapiError { status, reason }) | ||
} | ||
} | ||
|
||
impl From<std::io::Error> for BuildError { | ||
fn from(e: std::io::Error) -> Self { | ||
Self::new_with_kind(ErrorKind::Io(Box::new(e))) | ||
Self::new_inner(e) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,40 +1,48 @@ | ||
use std::borrow::Cow; | ||
use std::fmt::Debug; | ||
|
||
pub mod external_entry; | ||
// pub mod impl_to_diagnostic; | ||
pub mod unresolved_entry; | ||
pub mod unresolved_import; | ||
|
||
use miette::Diagnostic; | ||
use thiserror::Error; | ||
// TODO(hyf0): Not a good name, probably should rename to `BuildError` | ||
pub trait BuildErrorLike: Debug + Sync + Send { | ||
fn code(&self) -> &'static str; | ||
|
||
use self::{ | ||
external_entry::ExternalEntry, unresolved_entry::UnresolvedEntry, | ||
unresolved_import::UnresolvedImport, | ||
}; | ||
fn message(&self) -> String; | ||
} | ||
|
||
impl<T: BuildErrorLike + 'static> From<T> for Box<dyn BuildErrorLike> { | ||
fn from(e: T) -> Self { | ||
Box::new(e) | ||
} | ||
} | ||
|
||
type StaticStr = Cow<'static, str>; | ||
// --- TODO(hyf0): These errors are only for compatibility with legacy code. They should be replaced with more specific errors. | ||
|
||
#[derive(Error, Debug, Diagnostic)] | ||
pub enum ErrorKind { | ||
#[diagnostic(transparent)] | ||
#[error(transparent)] | ||
UnresolvedEntry(Box<UnresolvedEntry>), | ||
#[derive(Debug)] | ||
pub struct NapiError { | ||
pub status: String, | ||
pub reason: String, | ||
} | ||
|
||
#[diagnostic(transparent)] | ||
#[error(transparent)] | ||
ExternalEntry(Box<ExternalEntry>), | ||
impl BuildErrorLike for NapiError { | ||
fn code(&self) -> &'static str { | ||
"NAPI_ERROR" | ||
} | ||
|
||
#[diagnostic(transparent)] | ||
#[error(transparent)] | ||
UnresolvedImport(Box<UnresolvedImport>), | ||
fn message(&self) -> String { | ||
format!("Napi error: {status}: {reason}", status = self.status, reason = self.reason) | ||
} | ||
} | ||
|
||
#[diagnostic(code = "IO_ERROR")] | ||
#[error(transparent)] | ||
Io(Box<std::io::Error>), | ||
impl BuildErrorLike for std::io::Error { | ||
fn code(&self) -> &'static str { | ||
"IO_ERROR" | ||
} | ||
|
||
// TODO: probably should remove this error | ||
#[diagnostic(code = "NAPI_ERROR")] | ||
#[error("Napi error: {status}: {reason}")] | ||
Napi { status: String, reason: String }, | ||
fn message(&self) -> String { | ||
format!("IO error: {self}") | ||
} | ||
} | ||
|
||
// --- end |
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