diff --git a/Cargo.toml b/Cargo.toml index 692ff72..b59f901 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cc-utils" description = "Rust Fullstack utils (strict error handling, `Consider` trait, MessagePack support, etc.) for Salvo and Yew/Dioxus/Leptos/*" -version = "0.2.5" +version = "0.2.6" edition = "2021" license = "MIT" authors = ["Klimenty Titov "] diff --git a/src/errors.rs b/src/errors.rs index d19d61f..6c45a8b 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,6 @@ //! Implementation of optional private errors for `salvo` and client errors for `reqwest`. +use std::any::Any; #[cfg(feature = "salvo")] use salvo::http::StatusCode; @@ -508,15 +509,56 @@ impl_consider!(std::sync::mpsc::RecvError); impl_consider!(log::SetLoggerError); #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] impl_consider!(serde_json::Error); + +#[cfg(feature = "salvo")] #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] impl_consider!(salvo::Error); + +#[cfg(feature = "salvo")] #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] impl_consider!(salvo::hyper::http::status::InvalidStatusCode); + +#[cfg(feature = "salvo")] #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] impl_consider!(salvo::http::ParseError); +#[cfg(feature = "salvo")] #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] +impl Consider for Result>> { + /// Изменяет параметры возможной ошибки на указанные. + fn consider( + self, + status_code: Option, + error_text_replacement: Option, + public: bool, + ) -> Result { + self.map_err(|_| { + let mut new_error = ErrorResponse { + status_code, + error_text: "Depot obtain failed!".into(), + original_text: None, + public_error: public, + }; + if error_text_replacement.is_some() { + new_error.original_text = Some(new_error.error_text.to_owned()); + new_error.error_text = error_text_replacement.unwrap(); + } + new_error + }) + } +} + #[cfg(feature = "salvo")] +#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] +impl From>> for ErrorResponse { + /// Создаёт `ErrorResponse` из данной ошибки. + fn from(_value: Option<&Box<(dyn std::any::Any + Send + Sync + 'static)>>) -> Self { + "Depot obtain failed!".into() + } +} + +#[cfg(feature = "salvo")] +#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] impl Consider for Result> { /// Изменяет параметры возможной ошибки на указанные. fn consider( @@ -541,8 +583,8 @@ impl Consider for Result> { } } -#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] #[cfg(feature = "salvo")] +#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] impl From> for ErrorResponse { /// Создаёт `ErrorResponse` из данной ошибки. fn from(value: std::sync::mpsc::SendError) -> Self { @@ -560,6 +602,8 @@ impl_consider_cli!(std::io::Error); impl_consider_cli!(log::SetLoggerError); #[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] impl_consider_cli!(serde_json::Error); + +#[cfg(feature = "reqwest")] #[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] impl_consider_cli!(reqwest::Error);