1use serde::{Deserialize, Serialize, Serializer};
2use thiserror::Error;
3
4#[derive(Debug, Error, Deserialize)]
5pub enum AppError {
6 #[error("Profile not found: {0}")]
7 ProfileNotFound(String),
8
9 #[error("Profile already exists: {0}")]
10 ProfileExists(String),
11
12 #[error("Invalid credentials: {0}")]
13 InvalidCredentials(String),
14
15 #[error("Connection failed: {0}")]
16 ConnectionFailed(String),
17
18 #[error("S3 error: {0}")]
19 S3Error(String),
20 #[error("Access Denied: {0}")]
21 AccessDenied(String),
22
23 #[error("Keychain error: {0}")]
24 KeychainError(String),
25
26 #[error("IO error: {0}")]
27 IoError(String),
28
29 #[error("Serialization error: {0}")]
30 SerializationError(String),
31
32 #[error("Configuration error: {0}")]
33 ConfigError(String),
34
35 #[error("Invalid content: {0}")]
36 InvalidContent(String),
37}
38
39impl Serialize for AppError {
44 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
45 where
46 S: Serializer,
47 {
48 serializer.serialize_str(&self.to_string())
49 }
50}
51
52impl From<std::io::Error> for AppError {
53 fn from(err: std::io::Error) -> Self {
54 AppError::IoError(err.to_string())
55 }
56}
57
58impl From<serde_json::Error> for AppError {
59 fn from(err: serde_json::Error) -> Self {
60 AppError::SerializationError(err.to_string())
61 }
62}
63
64impl From<keyring::Error> for AppError {
65 fn from(err: keyring::Error) -> Self {
66 AppError::KeychainError(err.to_string())
67 }
68}
69
70pub type Result<T> = std::result::Result<T, AppError>;
71
72#[cfg(test)]
73mod tests {
74 use super::AppError;
75
76 #[test]
77 fn command_errors_serialize_as_human_readable_messages() {
78 let value = serde_json::to_value(AppError::S3Error("PutObject failed".to_string()))
79 .expect("AppError should serialize");
80
81 assert_eq!(value, "S3 error: PutObject failed");
82 }
83}