|
| 1 | +use std::error::Error; |
| 2 | +use std::fmt; |
| 3 | + |
| 4 | +use crate::pkexec::PkexecError; |
| 5 | +use crate::svg::renderer::RenderError; |
| 6 | +use crate::tailscale::peer::PeerError; |
| 7 | +use crate::tailscale::status::StatusError; |
| 8 | +use crate::tray::menu::TrayError; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +pub enum AppError { |
| 12 | + // Subsystem errors |
| 13 | + Pkexec(PkexecError), |
| 14 | + Render(RenderError), |
| 15 | + Peer(PeerError), |
| 16 | + Status(StatusError), |
| 17 | + Tray(TrayError), |
| 18 | + |
| 19 | + // External library errors |
| 20 | + Clipboard(arboard::Error), |
| 21 | + Io(std::io::Error), |
| 22 | + SerdeJson(serde_json::Error), |
| 23 | + Notify(notify_rust::error::Error), |
| 24 | + |
| 25 | + // Generic string error |
| 26 | + Message(String), |
| 27 | +} |
| 28 | + |
| 29 | +impl fmt::Display for AppError { |
| 30 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 31 | + match self { |
| 32 | + AppError::Pkexec(e) => write!(f, "Pkexec error: {e}"), |
| 33 | + AppError::Render(e) => write!(f, "SVG render error: {e}"), |
| 34 | + AppError::Peer(e) => write!(f, "Peer error: {e}"), |
| 35 | + AppError::Status(e) => write!(f, "Tailscale status error: {e}"), |
| 36 | + AppError::Tray(e) => write!(f, "Tray error: {e}"), |
| 37 | + AppError::Clipboard(e) => write!(f, "Clipboard error: {e}"), |
| 38 | + AppError::Io(e) => write!(f, "IO error: {e}"), |
| 39 | + AppError::SerdeJson(e) => write!(f, "JSON error: {e}"), |
| 40 | + AppError::Notify(e) => write!(f, "Notification error: {e}"), |
| 41 | + AppError::Message(msg) => write!(f, "{msg}"), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl Error for AppError { |
| 47 | + fn source(&self) -> Option<&(dyn Error + 'static)> { |
| 48 | + match self { |
| 49 | + AppError::Pkexec(e) => Some(e), |
| 50 | + AppError::Render(e) => Some(e), |
| 51 | + AppError::Peer(e) => Some(e), |
| 52 | + AppError::Status(e) => Some(e), |
| 53 | + AppError::Tray(e) => Some(e), |
| 54 | + AppError::Clipboard(e) => Some(e), |
| 55 | + AppError::Io(e) => Some(e), |
| 56 | + AppError::SerdeJson(e) => Some(e), |
| 57 | + AppError::Notify(e) => Some(e), |
| 58 | + AppError::Message(_) => None, |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// From conversions for subsystem errors |
| 64 | +impl From<PkexecError> for AppError { |
| 65 | + fn from(e: PkexecError) -> Self { |
| 66 | + AppError::Pkexec(e) |
| 67 | + } |
| 68 | +} |
| 69 | +impl From<RenderError> for AppError { |
| 70 | + fn from(e: RenderError) -> Self { |
| 71 | + AppError::Render(e) |
| 72 | + } |
| 73 | +} |
| 74 | +impl From<PeerError> for AppError { |
| 75 | + fn from(e: PeerError) -> Self { |
| 76 | + AppError::Peer(e) |
| 77 | + } |
| 78 | +} |
| 79 | +impl From<StatusError> for AppError { |
| 80 | + fn from(e: StatusError) -> Self { |
| 81 | + AppError::Status(e) |
| 82 | + } |
| 83 | +} |
| 84 | +impl From<TrayError> for AppError { |
| 85 | + fn from(e: TrayError) -> Self { |
| 86 | + AppError::Tray(e) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// From conversions for external errors |
| 91 | +impl From<arboard::Error> for AppError { |
| 92 | + fn from(e: arboard::Error) -> Self { |
| 93 | + AppError::Clipboard(e) |
| 94 | + } |
| 95 | +} |
| 96 | +impl From<std::io::Error> for AppError { |
| 97 | + fn from(e: std::io::Error) -> Self { |
| 98 | + AppError::Io(e) |
| 99 | + } |
| 100 | +} |
| 101 | +impl From<serde_json::Error> for AppError { |
| 102 | + fn from(e: serde_json::Error) -> Self { |
| 103 | + AppError::SerdeJson(e) |
| 104 | + } |
| 105 | +} |
| 106 | +impl From<notify_rust::error::Error> for AppError { |
| 107 | + fn from(e: notify_rust::error::Error) -> Self { |
| 108 | + AppError::Notify(e) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// From conversion for string messages |
| 113 | +impl From<String> for AppError { |
| 114 | + fn from(e: String) -> Self { |
| 115 | + AppError::Message(e) |
| 116 | + } |
| 117 | +} |
| 118 | +impl From<&str> for AppError { |
| 119 | + fn from(e: &str) -> Self { |
| 120 | + AppError::Message(e.to_owned()) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// From conversion for Box<dyn Error> |
| 125 | +impl From<Box<dyn std::error::Error>> for AppError { |
| 126 | + fn from(e: Box<dyn std::error::Error>) -> Self { |
| 127 | + AppError::Message(e.to_string()) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// From conversion for ksni::Error |
| 132 | +impl From<ksni::Error> for AppError { |
| 133 | + fn from(e: ksni::Error) -> Self { |
| 134 | + AppError::Message(e.to_string()) |
| 135 | + } |
| 136 | +} |
0 commit comments