refact error

Signed-off-by: junxiang Mu <1948535941@qq.com>
This commit is contained in:
junxiang Mu
2024-09-27 16:51:54 +08:00
parent 04ab9d75a9
commit bd02676dbc
21 changed files with 110 additions and 22 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ use rmp_serde::Serializer;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use time::OffsetDateTime; use time::OffsetDateTime;
use common::error::Result; use crate::error::Result;
use crate::disk::BUCKET_META_PREFIX; use crate::disk::BUCKET_META_PREFIX;
+1 -1
View File
@@ -1,5 +1,5 @@
use crate::error::StdError;
use bytes::Bytes; use bytes::Bytes;
use common::error::StdError;
use futures::pin_mut; use futures::pin_mut;
use futures::stream::{Stream, StreamExt}; use futures::stream::{Stream, StreamExt};
use std::future::Future; use std::future::Future;
+1 -1
View File
@@ -1,5 +1,5 @@
use crate::error::{Error, Result};
use crate::utils::net; use crate::utils::net;
use common::error::{Error, Result};
use path_absolutize::Absolutize; use path_absolutize::Absolutize;
use path_clean::PathClean; use path_clean::PathClean;
use std::{fmt::Display, path::Path}; use std::{fmt::Display, path::Path};
+1 -1
View File
@@ -1,4 +1,4 @@
use common::error::{Error, Result}; use crate::error::{Error, Result};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum DiskError { pub enum DiskError {
+1 -1
View File
@@ -1,5 +1,5 @@
use super::error::DiskError; use super::error::DiskError;
use common::error::{Error, Result}; use crate::error::{Error, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Error as JsonError; use serde_json::Error as JsonError;
use uuid::Uuid; use uuid::Uuid;
+1 -1
View File
@@ -4,13 +4,13 @@ use super::{
ReadOptions, RenameDataResp, VolumeInfo, WalkDirOptions, ReadOptions, RenameDataResp, VolumeInfo, WalkDirOptions,
}; };
use crate::disk::{LocalFileReader, LocalFileWriter, STORAGE_FORMAT_FILE}; use crate::disk::{LocalFileReader, LocalFileWriter, STORAGE_FORMAT_FILE};
use crate::error::{Error, Result};
use crate::{ use crate::{
file_meta::FileMeta, file_meta::FileMeta,
store_api::{FileInfo, RawFileInfo}, store_api::{FileInfo, RawFileInfo},
utils, utils,
}; };
use bytes::Bytes; use bytes::Bytes;
use common::error::{Error, Result};
use path_absolutize::Absolutize; use path_absolutize::Absolutize;
use std::{ use std::{
fs::Metadata, fs::Metadata,
+1 -1
View File
@@ -14,11 +14,11 @@ const STORAGE_FORMAT_FILE: &str = "xl.meta";
use crate::{ use crate::{
erasure::{ReadAt, Write}, erasure::{ReadAt, Write},
error::{Error, Result},
file_meta::FileMeta, file_meta::FileMeta,
store_api::{FileInfo, RawFileInfo}, store_api::{FileInfo, RawFileInfo},
}; };
use bytes::Bytes; use bytes::Bytes;
use common::error::{Error, Result};
use futures::StreamExt; use futures::StreamExt;
use protos::proto_gen::node_service::{ use protos::proto_gen::node_service::{
node_service_client::NodeServiceClient, ReadAtRequest, ReadAtResponse, WriteRequest, WriteResponse, node_service_client::NodeServiceClient, ReadAtRequest, ReadAtResponse, WriteRequest, WriteResponse,
+1 -1
View File
@@ -1,7 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use bytes::Bytes; use bytes::Bytes;
use common::error::{Error, Result};
use futures::lock::Mutex; use futures::lock::Mutex;
use protos::{ use protos::{
node_service_time_out_client, node_service_time_out_client,
@@ -17,6 +16,7 @@ use uuid::Uuid;
use crate::{ use crate::{
disk::error::DiskError, disk::error::DiskError,
error::{Error, Result},
store_api::{FileInfo, RawFileInfo}, store_api::{FileInfo, RawFileInfo},
}; };
+1 -1
View File
@@ -1,5 +1,5 @@
use crate::error::{Error, Result};
use crate::utils::ellipses::*; use crate::utils::ellipses::*;
use common::error::{Error, Result};
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashSet; use std::collections::HashSet;
+1 -1
View File
@@ -1,9 +1,9 @@
use crate::{ use crate::{
disk::endpoint::{Endpoint, EndpointType}, disk::endpoint::{Endpoint, EndpointType},
disks_layout::DisksLayout, disks_layout::DisksLayout,
error::{Error, Result},
utils::net, utils::net,
}; };
use common::error::{Error, Result};
use std::{ use std::{
collections::{hash_map::Entry, HashMap, HashSet}, collections::{hash_map::Entry, HashMap, HashSet},
net::IpAddr, net::IpAddr,
+1 -1
View File
@@ -1,5 +1,5 @@
use crate::error::{Error, Result, StdError};
use bytes::Bytes; use bytes::Bytes;
use common::error::{Error, Result, StdError};
use futures::future::join_all; use futures::future::join_all;
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt};
use reed_solomon_erasure::galois_8::ReedSolomon; use reed_solomon_erasure::galois_8::ReedSolomon;
+82
View File
@@ -0,0 +1,82 @@
use tracing_error::{SpanTrace, SpanTraceStatus};
pub type StdError = Box<dyn std::error::Error + Send + Sync + 'static>;
pub type Result<T = (), E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
pub struct Error {
inner: Box<dyn std::error::Error + Send + Sync + 'static>,
span_trace: SpanTrace,
}
impl Error {
/// Create a new error from a `std::error::Error`.
#[must_use]
#[track_caller]
pub fn new<T: std::error::Error + Send + Sync + 'static>(source: T) -> Self {
Self::from_std_error(source.into())
}
/// Create a new error from a `std::error::Error`.
#[must_use]
#[track_caller]
pub fn from_std_error(inner: StdError) -> Self {
Self {
inner,
span_trace: SpanTrace::capture(),
}
}
/// Create a new error from a string.
#[must_use]
#[track_caller]
pub fn from_string(s: impl Into<String>) -> Self {
Self::msg(s)
}
/// Create a new error from a string.
#[must_use]
#[track_caller]
pub fn msg(s: impl Into<String>) -> Self {
Self::from_std_error(s.into().into())
}
/// Returns `true` if the inner type is the same as `T`.
#[inline]
pub fn is<T: std::error::Error + 'static>(&self) -> bool {
self.inner.is::<T>()
}
/// Returns some reference to the inner value if it is of type `T`, or
/// `None` if it isn't.
#[inline]
pub fn downcast_ref<T: std::error::Error + 'static>(&self) -> Option<&T> {
self.inner.downcast_ref()
}
/// Returns some mutable reference to the inner value if it is of type `T`, or
/// `None` if it isn't.
#[inline]
pub fn downcast_mut<T: std::error::Error + 'static>(&mut self) -> Option<&mut T> {
self.inner.downcast_mut()
}
}
impl<T: std::error::Error + Send + Sync + 'static> From<T> for Error {
fn from(e: T) -> Self {
Self::new(e)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)?;
if self.span_trace.status() != SpanTraceStatus::EMPTY {
write!(f, "\nspan_trace:\n{}", self.span_trace)?;
}
Ok(())
}
}
+1
View File
@@ -4,6 +4,7 @@ pub mod disk;
pub mod disks_layout; pub mod disks_layout;
pub mod endpoints; pub mod endpoints;
pub mod erasure; pub mod erasure;
pub mod error;
mod file_meta; mod file_meta;
pub mod peer; pub mod peer;
pub mod set_disk; pub mod set_disk;
+1 -1
View File
@@ -1,5 +1,4 @@
use async_trait::async_trait; use async_trait::async_trait;
use common::error::{Error, Result};
use futures::future::join_all; use futures::future::join_all;
use protos::node_service_time_out_client; use protos::node_service_time_out_client;
use protos::proto_gen::node_service::{DeleteBucketRequest, GetBucketInfoRequest, ListBucketRequest, MakeBucketRequest}; use protos::proto_gen::node_service::{DeleteBucketRequest, GetBucketInfoRequest, ListBucketRequest, MakeBucketRequest};
@@ -12,6 +11,7 @@ use crate::store::all_local_disk;
use crate::{ use crate::{
disk::{self, error::DiskError, VolumeInfo}, disk::{self, error::DiskError, VolumeInfo},
endpoints::{EndpointServerPools, Node}, endpoints::{EndpointServerPools, Node},
error::{Error, Result},
store_api::{BucketInfo, BucketOptions, MakeBucketOptions}, store_api::{BucketInfo, BucketOptions, MakeBucketOptions},
}; };
+1 -1
View File
@@ -1,6 +1,5 @@
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
use common::error::{Error, Result};
use common::globals::GLOBAL_Local_Node_Name; use common::globals::GLOBAL_Local_Node_Name;
use futures::future::join_all; use futures::future::join_all;
use http::HeaderMap; use http::HeaderMap;
@@ -14,6 +13,7 @@ use crate::{
DiskStore, DiskStore,
}, },
endpoints::PoolEndpoints, endpoints::PoolEndpoints,
error::{Error, Result},
set_disk::SetDisks, set_disk::SetDisks,
store::{GLOBAL_IsDistErasure, GLOBAL_LOCAL_DISK_SET_DRIVES}, store::{GLOBAL_IsDistErasure, GLOBAL_LOCAL_DISK_SET_DRIVES},
store_api::{ store_api::{
+1 -1
View File
@@ -5,6 +5,7 @@ use crate::{
BUCKET_META_PREFIX, RUSTFS_META_BUCKET, BUCKET_META_PREFIX, RUSTFS_META_BUCKET,
}, },
endpoints::{EndpointServerPools, SetupType}, endpoints::{EndpointServerPools, SetupType},
error::{Error, Result},
peer::S3PeerSys, peer::S3PeerSys,
sets::Sets, sets::Sets,
store_api::{ store_api::{
@@ -15,7 +16,6 @@ use crate::{
store_init, utils, store_init, utils,
}; };
use backon::{ExponentialBuilder, Retryable}; use backon::{ExponentialBuilder, Retryable};
use common::error::{Error, Result};
use common::globals::{GLOBAL_Local_Node_Name, GLOBAL_Rustfs_Host, GLOBAL_Rustfs_Port}; use common::globals::{GLOBAL_Local_Node_Name, GLOBAL_Rustfs_Host, GLOBAL_Rustfs_Port};
use futures::future::join_all; use futures::future::join_all;
use http::HeaderMap; use http::HeaderMap;
+1 -1
View File
@@ -1,4 +1,4 @@
use common::error::{Error, Result}; use crate::error::{Error, Result};
use http::HeaderMap; use http::HeaderMap;
use rmp_serde::Serializer; use rmp_serde::Serializer;
use s3s::dto::StreamingBlob; use s3s::dto::StreamingBlob;
+1 -1
View File
@@ -5,8 +5,8 @@ use crate::{
new_disk, DiskOption, DiskStore, FORMAT_CONFIG_FILE, RUSTFS_META_BUCKET, new_disk, DiskOption, DiskStore, FORMAT_CONFIG_FILE, RUSTFS_META_BUCKET,
}, },
endpoints::Endpoints, endpoints::Endpoints,
error::{Error, Result},
}; };
use common::error::{Error, Result};
use futures::future::join_all; use futures::future::join_all;
use std::{ use std::{
collections::{hash_map::Entry, HashMap}, collections::{hash_map::Entry, HashMap},
+1 -1
View File
@@ -1,4 +1,4 @@
use common::error::{Error, Result}; use crate::error::{Error, Result};
use lazy_static::*; use lazy_static::*;
use regex::Regex; use regex::Regex;
+1 -1
View File
@@ -1,4 +1,4 @@
use common::error::{Error, Result}; use crate::error::{Error, Result};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::{ use std::{
collections::HashSet, collections::HashSet,
+9 -4
View File
@@ -4,7 +4,7 @@ mod service;
mod storage; mod storage;
use clap::Parser; use clap::Parser;
use common::error::Result; use common::error::{Error, Result};
use ecstore::{ use ecstore::{
endpoints::EndpointServerPools, endpoints::EndpointServerPools,
store::{init_local_disks, update_erasure_type, ECStore}, store::{init_local_disks, update_erasure_type, ECStore},
@@ -87,12 +87,15 @@ async fn run(opt: config::Opt) -> Result<()> {
// }; // };
// 用于rpc // 用于rpc
let (endpoint_pools, setup_type) = EndpointServerPools::from_volumes(opt.address.clone().as_str(), opt.volumes.clone())?; let (endpoint_pools, setup_type) = EndpointServerPools::from_volumes(opt.address.clone().as_str(), opt.volumes.clone())
.map_err(|err| Error::from_string(err.to_string()))?;
update_erasure_type(setup_type).await; update_erasure_type(setup_type).await;
// 初始化本地磁盘 // 初始化本地磁盘
init_local_disks(endpoint_pools.clone()).await?; init_local_disks(endpoint_pools.clone())
.await
.map_err(|err| Error::from_string(err.to_string()))?;
// Setup S3 service // Setup S3 service
// 本项目使用s3s库来实现s3服务 // 本项目使用s3s库来实现s3服务
@@ -177,7 +180,9 @@ async fn run(opt: config::Opt) -> Result<()> {
warn!(" init store"); warn!(" init store");
// init store // init store
ECStore::new(opt.address.clone(), endpoint_pools.clone()).await?; ECStore::new(opt.address.clone(), endpoint_pools.clone())
.await
.map_err(|err| Error::from_string(err.to_string()))?;
warn!(" init store success!"); warn!(" init store success!");
tokio::select! { tokio::select! {