added a few macros to help with error propagation

This commit is contained in:
Franklin 2023-09-30 10:13:03 -04:00
parent c59526086f
commit 18cc77b626
3 changed files with 75 additions and 2 deletions

View File

@ -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"] }
sqlx = { version = "0.7", features = ["json"] }
stdext = "0.3.1"

View File

@ -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<Trace>);
#[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.

55
src/macros.rs Normal file
View File

@ -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))))
}
};
}