Skip to content

Commit

Permalink
refactor: io errors
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Dec 5, 2023
1 parent 35be7b3 commit 7188841
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
12 changes: 5 additions & 7 deletions crates/rspack_core/src/resolver/resolver_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::{
sync::Arc,
};

use rspack_error::{internal_error, Error, InternalError, Severity, TraceableError};
use rspack_error::{
internal_error, DiagnosticError, Error, InternalError, Severity, TraceableError,
};
use rspack_loader_runner::DescriptionData;
use sugar_path::SugarPath;

Expand Down Expand Up @@ -374,9 +376,7 @@ fn map_nodejs_resolver_error(
plugin_driver: &SharedPluginDriver,
) -> ResolveError {
match error {
nodejs_resolver::Error::Io(error) => {
ResolveError(error.to_string(), Error::Io { source: error })
}
nodejs_resolver::Error::Io(error) => ResolveError(error.to_string(), Error::from(error)),
nodejs_resolver::Error::UnexpectedJson((json_path, error)) => ResolveError(
format!(
"{error:?} in {}",
Expand Down Expand Up @@ -423,9 +423,7 @@ fn map_oxc_resolver_error(
}
oxc_resolver::ResolveError::IOError(error) => ResolveError(
"IOError".to_string(),
Error::Io {
source: error.into(),
},
Error::InternalError(DiagnosticError(error.into()).into()),
),
oxc_resolver::ResolveError::Builtin(error) => ResolveError(
format!("Builtin module: {}", error),
Expand Down
6 changes: 0 additions & 6 deletions crates/rspack_error/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@ impl From<Error> for Vec<Diagnostic> {
severity,
..Default::default()
},
Error::Io { source } => Diagnostic {
message: source.to_string(),
kind,
severity,
..Default::default()
},
Error::Anyhow { source } => Diagnostic {
kind,
severity,
Expand Down
26 changes: 17 additions & 9 deletions crates/rspack_error/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use std::{fmt, io, path::Path};

use miette::MietteDiagnostic;
use miette::{Diagnostic, MietteDiagnostic};
use rspack_util::swc::normalize_custom_filename;
use swc_core::common::SourceFile;
use thiserror::Error;

use crate::{internal_error, Severity};

#[derive(Debug)]
pub struct InternalError(miette::Report);

impl<T: Diagnostic + Send + Sync + 'static> From<T> for InternalError {
fn from(value: T) -> Self {
InternalError(value.into())
}
}

impl InternalError {
pub fn new(error_message: String, severity: Severity) -> Self {
Self(miette::Report::new(
Expand Down Expand Up @@ -147,9 +154,6 @@ impl fmt::Display for TraceableError {
pub enum Error {
InternalError(InternalError),
TraceableError(TraceableError),
Io {
source: io::Error,
},
Anyhow {
source: anyhow::Error,
},
Expand All @@ -165,7 +169,7 @@ pub enum Error {
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io { source, .. } => Some(source as &(dyn std::error::Error + 'static)),
// Error::Io { source, .. } => Some(source as &(dyn std::error::Error + 'static)),
Error::Anyhow { source, .. } => Some(source.as_ref()),
_ => None,
}
Expand All @@ -177,7 +181,6 @@ impl fmt::Display for Error {
match self {
Error::InternalError(e) => write!(f, "{e}"),
Error::TraceableError(v) => write!(f, "{v}"),
Error::Io { source } => write!(f, "{source}"),
Error::Anyhow { source } => write!(f, "{source}"),
Error::BatchErrors(errs) => write!(
f,
Expand Down Expand Up @@ -205,7 +208,7 @@ impl From<serde_json::Error> for Error {

impl From<io::Error> for Error {
fn from(source: io::Error) -> Self {
Error::Io { source }
Error::InternalError(DiagnosticError(source.into()).into())
}
}

Expand All @@ -226,7 +229,6 @@ impl Error {
match self {
Error::InternalError(_) => DiagnosticKind::Internal,
Error::TraceableError(TraceableError { kind, .. }) => *kind,
Error::Io { .. } => DiagnosticKind::Io,
Error::Anyhow { .. } => DiagnosticKind::Internal,
Error::BatchErrors(_) => DiagnosticKind::Internal,
Error::Napi { .. } => DiagnosticKind::Internal,
Expand All @@ -236,7 +238,6 @@ impl Error {
match self {
Error::InternalError(_) => Severity::Error,
Error::TraceableError(TraceableError { severity, .. }) => *severity,
Error::Io { .. } => Severity::Error,
Error::Anyhow { .. } => Severity::Error,
Error::BatchErrors(_) => Severity::Error,
Error::Napi { .. } => Severity::Error,
Expand Down Expand Up @@ -277,3 +278,10 @@ impl std::fmt::Display for DiagnosticKind {
}
}
}

/// Convenience [`Diagnostic`] that can be used as an "anonymous" wrapper for
/// Errors. This is intended to be paired with [`IntoDiagnostic`].
#[derive(Debug, Error)]
#[error(transparent)]
pub struct DiagnosticError(pub Box<dyn std::error::Error + Send + Sync + 'static>);
impl Diagnostic for DiagnosticError {}
2 changes: 1 addition & 1 deletion crates/rspack_fs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl From<std::io::Error> for Error {
impl From<Error> for rspack_error::Error {
fn from(value: Error) -> Self {
match value {
Error::Io(err) => Self::Io { source: err },
Error::Io(err) => rspack_error::Error::from(err),
}
}
}
Expand Down

0 comments on commit 7188841

Please sign in to comment.