47 lines
1.6 KiB
Rust
47 lines
1.6 KiB
Rust
use color_eyre::section::Section;
|
|
use std::fmt;
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
#[allow(dead_code)]
|
|
pub enum ErrorCategory {
|
|
Database,
|
|
Config,
|
|
Reddit,
|
|
Action,
|
|
Notification,
|
|
IO,
|
|
Unknown,
|
|
}
|
|
|
|
impl fmt::Display for ErrorCategory {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
ErrorCategory::Database => write!(f, "Database error"),
|
|
ErrorCategory::Config => write!(f, "Configuration error"),
|
|
ErrorCategory::Reddit => write!(f, "Reddit API error"),
|
|
ErrorCategory::Action => write!(f, "Action error"),
|
|
ErrorCategory::Notification => write!(f, "Notification error"),
|
|
ErrorCategory::IO => write!(f, "IO error"),
|
|
ErrorCategory::Unknown => write!(f, "Unknown error"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! define_error_fn {
|
|
($fn_name:ident, $category:expr) => {
|
|
/// Helper function to create an error with context
|
|
#[allow(dead_code)]
|
|
pub fn $fn_name<E: std::fmt::Display>(error: E, context: &str) -> color_eyre::eyre::Report {
|
|
color_eyre::eyre::eyre!("{}: {}", context, error).with_section(|| $category.to_string())
|
|
}
|
|
};
|
|
}
|
|
|
|
// Define all error helper functions using the macro
|
|
define_error_fn!(db_error, ErrorCategory::Database);
|
|
define_error_fn!(config_error, ErrorCategory::Config);
|
|
define_error_fn!(reddit_error, ErrorCategory::Reddit);
|
|
define_error_fn!(action_error, ErrorCategory::Action);
|
|
define_error_fn!(notification_error, ErrorCategory::Notification);
|
|
define_error_fn!(io_error, ErrorCategory::IO);
|