From 18cc77b6266d0fc90237a7ccb297d3eeb574f78a Mon Sep 17 00:00:00 2001 From: Franklin Date: Sat, 30 Sep 2023 10:13:03 -0400 Subject: [PATCH] added a few macros to help with error propagation --- Cargo.toml | 3 ++- src/lib.rs | 19 +++++++++++++++++- src/macros.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/macros.rs diff --git a/Cargo.toml b/Cargo.toml index 70ef24c..c0bf3e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ repository = "https://github.com/franklinblanco/err.git" [dependencies] serde = { version = "1.0", features = ["derive"] } thiserror = "1.0.48" -sqlx = { version = "0.7", features = ["json"] } \ No newline at end of file +sqlx = { version = "0.7", features = ["json"] } +stdext = "0.3.1" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 3e51805..9584798 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,12 @@ +mod macros; + use std::fmt::Display; use serde::{Serialize, Deserialize, Serializer}; use serde::ser::SerializeMap; use thiserror::Error; +pub use stdext::function_name; + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub struct Trace { pub line: u32, @@ -10,9 +14,18 @@ pub struct Trace { pub file: String, pub service: String, } +impl Trace { + pub fn set_func_name(mut self, fn_name: impl ToString) -> Trace { + self.function = fn_name.to_string(); + self + } +} #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct Traces(pub Vec); -#[derive(Serialize, Debug)] + + +#[derive(Serialize, Debug, Error)] +#[error("MessageResource: {message_resource} ErrorType: {error_type} Trace: {trace:#?}")] pub struct Error { pub trace: Traces, #[serde(rename = "messageResource")] @@ -48,6 +61,10 @@ impl Error { self.message_resource.message = message.to_string(); self } + pub fn error_type(mut self, error_type: ErrorType) -> Self { + self.error_type = error_type; + self + } } /// This is for sending errors back from requests conveniently. diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..4ae8207 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,55 @@ +/// Macro used to generate the trace object, must be called from the place where it originates, don't call from another function. +#[allow(unused_macros)] +#[macro_export] +macro_rules! trace { + () => { + err::Trace { + line: line!(), + function: err::function_name!().into(), + file: file!().into(), + service: env!("CARGO_PKG_NAME").into(), + } + }; +} + +/// Macro used to 'unwrap' a result that returns a Error +/// +/// If there's an error returns the generated Error and push a trace on it. +#[allow(unused_macros)] +#[macro_export] +macro_rules! u_res_or_res { + ( $e:expr ) => { + match $e { + Ok(result) => result, + Err(mut error) => return Err(error.push_trace(err::trace!())) + } + }; +} + +/// Macro used to 'unwrap' a result that returns a Error +/// +/// If there's an error returns the generated Error and push a trace on it. +#[allow(unused_macros)] +#[macro_export] +macro_rules! x_u_res_or_res { + ( $e:expr, $t:expr ) => { + match $e { + Ok(result) => result, + Err(error) => return Err(err::Error::new(err::trace!()).message(error.to_string()).error_type($t)) + } + }; +} + +/// Macro used to 'unwrap' a result that returns a Error +/// +/// If there's an error returns the generated Error and push a trace on it. +#[allow(unused_macros)] +#[macro_export] +macro_rules! x_u_res_db_or_res { + ( $e:expr ) => { + match $e { + Ok(result) => result, + Err(error) => return Err(err::Error::new(err::trace!()).message(error.to_string()).error_type(err::ErrorType::Service(err::ServiceError::DatabaseError(error)))) + } + }; +} \ No newline at end of file