Skip to content

Commit

Permalink
refactor: improve storage error message
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Dec 17, 2024
1 parent a08f7b6 commit 1e32768
Show file tree
Hide file tree
Showing 21 changed files with 552 additions and 377 deletions.
4 changes: 2 additions & 2 deletions crates/rspack_core/src/cache/persistent/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{path::PathBuf, sync::Arc};
pub use memory::MemoryStorage;
use rspack_fs::IntermediateFileSystem;
pub use rspack_storage::Storage;
use rspack_storage::{PackBridgeFS, PackStorage, PackStorageOptions};
use rspack_storage::{PackStorage, PackStorageOptions, StorageBridgeFS};

/// Storage Options
///
Expand All @@ -31,7 +31,7 @@ pub fn create_storage(
bucket_size: 20,
pack_size: 500 * 1024,
expire: 7 * 24 * 60 * 60 * 1000,
fs: Arc::new(PackBridgeFS(fs)),
fs: Arc::new(StorageBridgeFS(fs)),
version,
};
Arc::new(PackStorage::new(option))
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod pack;

use std::sync::Arc;

pub use pack::{PackBridgeFS, PackFS, PackStorage, PackStorageOptions};
pub use pack::{PackStorage, PackStorageOptions, StorageBridgeFS, StorageFS};
use rspack_error::Result;
use tokio::sync::oneshot::Receiver;

Expand Down
8 changes: 5 additions & 3 deletions crates/rspack_storage/src/pack/data/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl ScopePacksState {

#[derive(Debug)]
pub struct PackScope {
pub name: &'static str,
pub path: Utf8PathBuf,
pub options: Arc<PackOptions>,
pub meta: ScopeMetaState,
Expand All @@ -103,8 +104,9 @@ pub struct PackScope {
}

impl PackScope {
pub fn new(path: Utf8PathBuf, options: Arc<PackOptions>) -> Self {
pub fn new(name: &'static str, path: Utf8PathBuf, options: Arc<PackOptions>) -> Self {
Self {
name,
path,
options,
meta: ScopeMetaState::Pending,
Expand All @@ -113,8 +115,8 @@ impl PackScope {
}
}

pub fn empty(path: Utf8PathBuf, options: Arc<PackOptions>) -> Self {
let mut scope = Self::new(path, options);
pub fn empty(name: &'static str, path: Utf8PathBuf, options: Arc<PackOptions>) -> Self {
let mut scope = Self::new(name, path, options);
scope.clear();
scope
}
Expand Down
114 changes: 93 additions & 21 deletions crates/rspack_storage/src/pack/fs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use std::io::ErrorKind;
use cow_utils::CowUtils;
use rspack_error::{
miette::{self},
thiserror::{self, Error},
Error,
thiserror::Error,
Result,
};
use rspack_paths::Utf8Path;
use tokio::task::JoinError;

#[derive(Debug)]
pub enum PackFsErrorOpt {
pub enum StorageFSOperation {
Read,
Write,
Dir,
Expand All @@ -18,7 +19,7 @@ pub enum PackFsErrorOpt {
Move,
}

impl std::fmt::Display for PackFsErrorOpt {
impl std::fmt::Display for StorageFSOperation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Read => write!(f, "read"),
Expand All @@ -32,48 +33,119 @@ impl std::fmt::Display for PackFsErrorOpt {
}

#[derive(Debug, Error)]
#[error(r#"Rspack Storage FS Error: {opt} `{file}` failed with `{inner}`"#)]
pub struct PackFsError {
pub struct StorageFSError {
file: String,
inner: Error,
opt: PackFsErrorOpt,
kind: Option<ErrorKind>,
inner: rspack_fs::Error,
opt: StorageFSOperation,
}

impl PackFsError {
pub fn from_fs_error(file: &Utf8Path, opt: PackFsErrorOpt, error: rspack_fs::Error) -> Self {
let kind = match &error {
rspack_fs::Error::Io(e) => Some(e.kind()),
};
impl StorageFSError {
pub fn from_fs_error(file: &Utf8Path, opt: StorageFSOperation, error: rspack_fs::Error) -> Self {
Self {
file: file.to_string(),
inner: error.into(),
inner: error,
opt,
kind,
}
}
pub fn is_not_found(&self) -> bool {
if self.kind.is_some_and(|k| matches!(k, ErrorKind::NotFound)) {
if matches!(self.kind(), ErrorKind::NotFound) {
return true;
}
let error_content = self.inner.to_string();
let lower_case_error_content = error_content.cow_to_lowercase();
lower_case_error_content.contains("no such file")
|| lower_case_error_content.contains("file not exists")
}
pub fn kind(&self) -> ErrorKind {
match &self.inner {
rspack_fs::Error::Io(e) => e.kind(),
}
}
}

impl miette::Diagnostic for PackFsError {
impl std::fmt::Display for StorageFSError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} `{}` failed due to `{}`",
self.opt,
self.file,
match &self.inner {
rspack_fs::Error::Io(e) => e,
}
)
}
}

impl miette::Diagnostic for StorageFSError {
fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
Some(Box::new("PackFsError"))
Some(Box::new("StorageFSError"))
}
fn severity(&self) -> Option<miette::Severity> {
Some(miette::Severity::Warning)
}
fn url<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
Some(Box::new(self.file.clone()))
}
fn diagnostic_source(&self) -> Option<&dyn miette::Diagnostic> {
Some(self.inner.as_ref())
}

#[derive(Debug, Error)]
pub struct BatchStorageFSError {
message: String,
join_error: Option<JoinError>,
errors: Vec<rspack_error::Error>,
}

impl std::fmt::Display for BatchStorageFSError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)?;
if let Some(join_error) = &self.join_error {
write!(f, " due to `{}`", join_error)?;
}
for error in &self.errors {
write!(f, "\n- {}", error)?;
}
Ok(())
}
}

impl BatchStorageFSError {
pub fn try_from_joined_result(
message: &str,
res: Result<Vec<Result<()>>, JoinError>,
) -> Option<Self> {
match res {
Ok(res) => Self::try_from_results(message, res),
Err(join_error) => Some(Self {
message: message.to_string(),
errors: vec![],
join_error: Some(join_error),
}),
}
}

pub fn try_from_results(message: &str, results: Vec<Result<()>>) -> Option<Self> {
let errors = results
.into_iter()
.filter_map(|res| res.err())
.collect::<Vec<_>>();
if errors.is_empty() {
None
} else {
Some(Self {
message: message.to_string(),
errors,
join_error: None,
})
}
}
}

impl miette::Diagnostic for BatchStorageFSError {
fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
Some(Box::new("BatchStorageFSError"))
}
fn severity(&self) -> Option<miette::Severity> {
Some(miette::Severity::Warning)
}
}
Loading

0 comments on commit 1e32768

Please sign in to comment.