From 71d6d9ec484fff157a3466544dff9e419de010e5 Mon Sep 17 00:00:00 2001 From: Damonxue Date: Wed, 9 Apr 2025 10:25:41 +0800 Subject: [PATCH] feat: add FileAccessDeniedWithContext error type for better file access error handling --- ecstore/src/disk/error.rs | 14 ++++++++++++++ ecstore/src/disk/local.rs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ecstore/src/disk/error.rs b/ecstore/src/disk/error.rs index a0773b5e..d7ea2854 100644 --- a/ecstore/src/disk/error.rs +++ b/ecstore/src/disk/error.rs @@ -1,4 +1,5 @@ use std::io::{self, ErrorKind}; +use std::path::PathBuf; use tracing::error; @@ -345,6 +346,19 @@ pub fn os_err_to_file_err(e: io::Error) -> Error { } } +#[derive(Debug, thiserror::Error)] +pub struct FileAccessDeniedWithContext { + pub path: PathBuf, + #[source] + pub source: std::io::Error, +} + +impl std::fmt::Display for FileAccessDeniedWithContext { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "访问文件 '{}' 被拒绝: {}", self.path.display(), self.source) + } +} + pub fn is_unformatted_disk(err: &Error) -> bool { matches!(err.downcast_ref::(), Some(DiskError::UnformattedDisk)) } diff --git a/ecstore/src/disk/local.rs b/ecstore/src/disk/local.rs index 13c0ea26..5a69f761 100644 --- a/ecstore/src/disk/local.rs +++ b/ecstore/src/disk/local.rs @@ -14,7 +14,7 @@ use crate::bucket::metadata_sys::{self}; use crate::cache_value::cache::{Cache, Opts, UpdateFn}; use crate::disk::error::{ convert_access_error, is_err_os_not_exist, is_sys_err_handle_invalid, is_sys_err_invalid_arg, is_sys_err_is_dir, - is_sys_err_not_dir, map_err_not_exists, os_err_to_file_err, + is_sys_err_not_dir, map_err_not_exists, os_err_to_file_err, FileAccessDeniedWithContext }; use crate::disk::os::{check_path_length, is_empty_dir}; use crate::disk::STORAGE_FORMAT_FILE;