From c67cfc68a25d3cd38239539937a8cdff50d860e6 Mon Sep 17 00:00:00 2001 From: Delta1925 Date: Tue, 18 Apr 2023 22:10:23 +0200 Subject: [PATCH] Add macro for easier logging --- backend/src/errors.rs | 25 +++++++++++++++++++++++++ backend/src/settings.rs | 1 + 2 files changed, 26 insertions(+) diff --git a/backend/src/errors.rs b/backend/src/errors.rs index dce8921..e61ee50 100644 --- a/backend/src/errors.rs +++ b/backend/src/errors.rs @@ -5,6 +5,31 @@ use thiserror::Error as DeriveError; use crate::utils::return_outcome; +#[macro_export] +macro_rules! log_err { + ($var: expr, $level: ident) => {{ + let test = $var; + if test.is_err() { + $level!("{} {}", $crate::settings::ERROR_TEXT, test.as_ref().unwrap_err()); + test + } else { + test + } + }}; + ($var: expr, $level: ident, $panic: expr) => {{ + let test = $var; + if log_err!(test, $level).is_err() { + if $panic == true { + panic!("{} {}", $crate::settings::ERROR_TEXT, test.unwrap_err()); + } else { + test + } + } else { + $var + } + }}; +} + #[derive(Debug, DeriveError)] pub enum SpecialErrors { #[error("Could not find any primay user email in the keyblock!")] diff --git a/backend/src/settings.rs b/backend/src/settings.rs index 13dfa86..fe2c871 100644 --- a/backend/src/settings.rs +++ b/backend/src/settings.rs @@ -85,6 +85,7 @@ fn get_mailer() -> SmtpTransport { mailer } +pub const ERROR_TEXT: &str = "An error occoured:"; pub const POLICY: &StandardPolicy = &StandardPolicy::new(); pub const ROOT_FOLDER: &str = "data"; pub static SETTINGS: Lazy = Lazy::new(get_settings);