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 2247eee commit 2155d7d
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 155 deletions.
4 changes: 2 additions & 2 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ impl Compilation {
self
.diagnostics
.iter()
.filter(|d| matches!(d.severity, Severity::Error))
.filter(|d| matches!(d.severity(), Severity::Error))
}

pub fn get_warnings(&self) -> impl Iterator<Item = &Diagnostic> {
self
.diagnostics
.iter()
.filter(|d| matches!(d.severity, Severity::Warn))
.filter(|d| matches!(d.severity(), Severity::Warn))
}

pub fn get_logging(&self) -> &CompilationLogging {
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ pub enum NormalModuleSource {

impl NormalModuleSource {
pub fn new_built(source: BoxSource, diagnostics: &[Diagnostic]) -> Self {
if diagnostics.iter().any(|d| d.severity == Severity::Error) {
if diagnostics.iter().any(|d| d.severity() == Severity::Error) {
NormalModuleSource::BuiltFailed(
diagnostics
.iter()
.filter(|d| d.severity == Severity::Error)
.map(|d| d.message.clone())
.filter(|d| d.severity() == Severity::Error)
.map(|d| d.message().to_string())
.collect::<Vec<String>>()
.join("\n"),
)
Expand Down
38 changes: 13 additions & 25 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, TraceableRspackError,
};
use rspack_loader_runner::DescriptionData;
use sugar_path::SugarPath;

Expand Down Expand Up @@ -374,24 +376,17 @@ 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 {}",
json_path.relative(&plugin_driver.options.context).display()
),
Error::Anyhow {
source: anyhow::Error::msg(format!("{error:?} in {json_path:?}")),
},
),
nodejs_resolver::Error::UnexpectedValue(error) => ResolveError(
error.clone(),
Error::Anyhow {
source: anyhow::Error::msg(error),
},
internal_error!("{error:?} in {json_path:?}"),
),
nodejs_resolver::Error::UnexpectedValue(error) => {
ResolveError(error.clone(), internal_error!(error))
}
nodejs_resolver::Error::CantFindTsConfig(path) => ResolveError(
format!("{} is not a tsconfig", path.display()),
internal_error!("{} is not a tsconfig", path.display()),
Expand All @@ -414,18 +409,11 @@ fn map_oxc_resolver_error(
"Export should be relative path and start with \"./\", but got {}",
specifier
);
ResolveError(
message.clone(),
Error::Anyhow {
source: anyhow::Error::msg(message),
},
)
ResolveError(message.clone(), internal_error!(message))
}
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 Expand Up @@ -535,7 +523,7 @@ fn map_resolver_error(
};
ResolveError(
runtime_message,
TraceableError::from_real_file_path(
TraceableRspackError::from_real_file_path(
Path::new(
importer
.split_once('|')
Expand All @@ -549,9 +537,9 @@ fn map_resolver_error(
)
.map(|e| {
if args.optional {
Error::TraceableError(e.with_severity(Severity::Warn))
Error::TraceableRspackError(e.with_severity(Severity::Warn))
} else {
Error::TraceableError(e)
Error::TraceableRspackError(e)
}
})
.unwrap_or_else(|_| {
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_core/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ impl Stats<'_> {
.compilation
.get_errors()
.map(|d| StatsError {
title: d.title.clone(),
message: d.message.clone(),
title: d.title().to_string(),
message: d.message().to_string(),
formatted: diagnostic_displayer.emit_diagnostic(d).expect("TODO:"),
})
.collect()
Expand All @@ -315,7 +315,7 @@ impl Stats<'_> {
.compilation
.get_warnings()
.map(|d| StatsWarning {
message: d.message.clone(),
message: d.message().to_string(),
formatted: diagnostic_displayer.emit_diagnostic(d).expect("TODO:"),
})
.collect()
Expand Down
68 changes: 23 additions & 45 deletions crates/rspack_error/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{backtrace::Backtrace, fmt};
use std::fmt;

use crate::{DiagnosticKind, Error, TraceableError};
use crate::{DiagnosticKind, Error, TraceableRspackError};

#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash)]
pub enum Severity {
Expand Down Expand Up @@ -39,15 +39,15 @@ pub struct DiagnosticSourceInfo {

#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct Diagnostic {
pub severity: Severity,
pub message: String,
pub title: String,
pub(crate) severity: Severity,
pub(crate) message: String,
pub(crate) title: String,
/// Source code and path of current Diagnostic
pub source_info: Option<DiagnosticSourceInfo>,
pub start: usize,
pub end: usize,
pub kind: DiagnosticKind,
pub notes: Vec<String>,
pub(crate) source_info: Option<DiagnosticSourceInfo>,
pub(crate) start: usize,
pub(crate) end: usize,
pub(crate) kind: DiagnosticKind,
pub(crate) notes: Vec<String>,
}

impl Diagnostic {
Expand Down Expand Up @@ -75,6 +75,18 @@ impl Diagnostic {
}
}

pub fn title(&self) -> &str {
&self.title
}

pub fn message(&self) -> &str {
&self.message
}

pub fn severity(&self) -> Severity {
self.severity
}

pub fn with_kind(mut self, kind: DiagnosticKind) -> Self {
self.kind = kind;
self
Expand All @@ -92,8 +104,6 @@ impl Diagnostic {

impl From<Error> for Vec<Diagnostic> {
fn from(err: Error) -> Self {
let kind = err.kind();
let severity = err.severity();
let diagnostic = match err {
Error::InternalError(err) => Diagnostic {
message: err.error_message().to_string(),
Expand All @@ -103,19 +113,7 @@ impl From<Error> for Vec<Diagnostic> {
severity: err.severity(),
..Default::default()
},
Error::Napi {
status,
reason,
backtrace,
} => Diagnostic {
message: format!("Napi Error: {status} - {reason}\n{backtrace}"),
source_info: None,
start: 0,
end: 0,
severity: Severity::Error,
..Default::default()
},
Error::TraceableError(TraceableError {
Error::TraceableRspackError(TraceableRspackError {
start,
end,
error_message,
Expand All @@ -137,26 +135,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,
message: {
let backtrace = match Backtrace::capture().status() {
std::backtrace::BacktraceStatus::Captured => {
format!("\nbacktrace:\n{}", source.backtrace())
}
_ => "".to_string(),
};
format!("{source}{backtrace}")
},
..Default::default()
},
Error::BatchErrors(diagnostics) => {
return diagnostics
.into_iter()
Expand Down
Loading

0 comments on commit 2155d7d

Please sign in to comment.