Skip to main content

notify_core/
error.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, NotifyError>;
6
7#[derive(Debug, Error)]
8pub enum NotifyError {
9    #[error("configuration file not found")]
10    ConfigNotFound,
11    #[error("failed to read configuration file {path}: {source}")]
12    ConfigRead {
13        path: PathBuf,
14        source: std::io::Error,
15    },
16    #[error("failed to parse configuration file {path}: {source}")]
17    ConfigParse {
18        path: PathBuf,
19        source: toml::de::Error,
20    },
21    #[error("channel \"{0}\" was not found")]
22    ChannelNotFound(String),
23    #[error("default_channel is not configured")]
24    DefaultChannelMissing,
25    #[error("{0}")]
26    InvalidInput(String),
27    #[error("{0}")]
28    Validation(String),
29    #[error("channel \"{channel}\" is missing environment variable {env}")]
30    MissingEnv { channel: String, env: String },
31    #[error("channel type \"{0}\" is not available for sending yet")]
32    UnsupportedProvider(String),
33    #[error("channel type \"{channel_type}\" does not support attachments")]
34    UnsupportedAttachment { channel_type: String },
35    #[error("HTTP request failed: {0}")]
36    Http(#[from] reqwest::Error),
37    #[error("provider request failed: {0}")]
38    Provider(String),
39    #[error("I/O error at {path}: {source}")]
40    Io {
41        path: PathBuf,
42        source: std::io::Error,
43    },
44    #[error("failed to serialize JSON output: {0}")]
45    Json(#[from] serde_json::Error),
46}
47
48impl NotifyError {
49    pub fn code(&self) -> &'static str {
50        match self {
51            Self::ConfigNotFound => "CONFIG_NOT_FOUND",
52            Self::ConfigRead { .. } => "CONFIG_READ",
53            Self::ConfigParse { .. } => "CONFIG_PARSE",
54            Self::ChannelNotFound(_) => "CHANNEL_NOT_FOUND",
55            Self::DefaultChannelMissing => "DEFAULT_CHANNEL_MISSING",
56            Self::InvalidInput(_) => "INVALID_INPUT",
57            Self::Validation(_) => "VALIDATION",
58            Self::MissingEnv { .. } => "MISSING_ENV",
59            Self::UnsupportedProvider(_) => "UNSUPPORTED_PROVIDER",
60            Self::UnsupportedAttachment { .. } => "UNSUPPORTED_ATTACHMENT",
61            Self::Http(_) => "HTTP",
62            Self::Provider(_) => "PROVIDER",
63            Self::Io { .. } => "IO",
64            Self::Json(_) => "JSON",
65        }
66    }
67}