1use auths_core::error::AuthsErrorInfo;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5#[non_exhaustive]
6pub enum FreezeError {
7 #[error(transparent)]
8 Io(#[from] std::io::Error),
9 #[error("failed to parse freeze state: {0}")]
10 Deserialization(#[from] serde_json::Error),
11 #[error("invalid duration format: {0}")]
12 InvalidDuration(String),
13 #[error("duration must be greater than zero")]
14 ZeroDuration,
15}
16
17#[derive(Debug, Error)]
18#[non_exhaustive]
19pub enum StorageError {
20 #[cfg(feature = "git-storage")]
21 #[error(transparent)]
22 Git(#[from] git2::Error),
23 #[error("serialization error: {0}")]
24 Serialization(#[from] serde_json::Error),
25 #[error("I/O error: {0}")]
26 Io(#[from] std::io::Error),
27 #[error("not found: {0}")]
28 NotFound(String),
29 #[error("{0}")]
30 InvalidData(String),
31 #[error("schema validation failed: {0}")]
32 SchemaValidation(String),
33 #[error("index error: {0}")]
34 Index(String),
35}
36
37#[derive(Debug, Error)]
38#[non_exhaustive]
39pub enum InitError {
40 #[cfg(feature = "git-storage")]
41 #[error(transparent)]
42 Git(#[from] git2::Error),
43 #[error("KERI operation failed: {0}")]
44 Keri(String),
45 #[error("key operation failed: {0}")]
46 Key(#[from] auths_core::error::AgentError),
47 #[error("{0}")]
48 InvalidData(String),
49 #[error("storage operation failed: {0}")]
50 Storage(#[from] StorageError),
51 #[error("registry error: {0}")]
52 Registry(String),
53 #[error("crypto operation failed: {0}")]
54 Crypto(String),
55 #[error("identity error: {0}")]
56 Identity(#[from] crate::identity::helpers::IdentityError),
57}
58
59impl AuthsErrorInfo for FreezeError {
60 fn error_code(&self) -> &'static str {
61 match self {
62 Self::Io(_) => "AUTHS-E4001",
63 Self::Deserialization(_) => "AUTHS-E4002",
64 Self::InvalidDuration(_) => "AUTHS-E4003",
65 Self::ZeroDuration => "AUTHS-E4004",
66 }
67 }
68
69 fn suggestion(&self) -> Option<&'static str> {
70 match self {
71 Self::Io(_) => Some("Check file permissions and disk space"),
72 Self::Deserialization(_) => {
73 Some("The freeze state file may be corrupted; try deleting it")
74 }
75 Self::InvalidDuration(_) => {
76 Some("Use a valid duration format (e.g. '30m', '2h', '7d')")
77 }
78 Self::ZeroDuration => Some("Specify a positive duration"),
79 }
80 }
81}
82
83impl AuthsErrorInfo for StorageError {
84 fn error_code(&self) -> &'static str {
85 match self {
86 #[cfg(feature = "git-storage")]
87 Self::Git(_) => "AUTHS-E4101",
88 Self::Serialization(_) => "AUTHS-E4102",
89 Self::Io(_) => "AUTHS-E4103",
90 Self::NotFound(_) => "AUTHS-E4104",
91 Self::InvalidData(_) => "AUTHS-E4105",
92 Self::SchemaValidation(_) => "AUTHS-E4106",
93 Self::Index(_) => "AUTHS-E4107",
94 }
95 }
96
97 fn suggestion(&self) -> Option<&'static str> {
98 match self {
99 #[cfg(feature = "git-storage")]
100 Self::Git(_) => Some("Check that the Git repository is not corrupted"),
101 Self::Serialization(_) => {
102 Some("Failed to serialize storage data; this may indicate a version mismatch")
103 }
104 Self::Io(_) => Some("Check file permissions and disk space"),
105 Self::NotFound(_) => Some("Verify the identity or resource exists"),
106 Self::InvalidData(_) => Some("The stored data may be corrupted; try re-initializing"),
107 Self::SchemaValidation(_) => Some("Ensure data matches the expected schema version"),
108 Self::Index(_) => Some("Try rebuilding the index"),
109 }
110 }
111}
112
113impl AuthsErrorInfo for InitError {
114 fn error_code(&self) -> &'static str {
115 match self {
116 #[cfg(feature = "git-storage")]
117 Self::Git(_) => "AUTHS-E4201",
118 Self::Keri(_) => "AUTHS-E4202",
119 Self::Key(_) => "AUTHS-E4203",
120 Self::InvalidData(_) => "AUTHS-E4204",
121 Self::Storage(_) => "AUTHS-E4205",
122 Self::Registry(_) => "AUTHS-E4206",
123 Self::Crypto(_) => "AUTHS-E4207",
124 Self::Identity(_) => "AUTHS-E4208",
125 }
126 }
127
128 fn suggestion(&self) -> Option<&'static str> {
129 match self {
130 #[cfg(feature = "git-storage")]
131 Self::Git(_) => Some("Check that the Git repository is accessible"),
132 Self::Keri(_) => Some("KERI event processing failed; check identity state"),
133 Self::Key(_) => Some("Check keychain access and passphrase"),
134 Self::InvalidData(_) => {
135 Some("Identity data is malformed; try re-initializing with `auths init`")
136 }
137 Self::Storage(_) => Some("Check storage backend connectivity"),
138 Self::Registry(_) => Some("Check registry backend configuration"),
139 Self::Crypto(_) => Some(
140 "A cryptographic operation during initialization failed; check your keychain access",
141 ),
142 Self::Identity(_) => {
143 Some("Identity initialization failed; check storage and keychain configuration")
144 }
145 }
146 }
147}