spec_driven_docs/
error.rs1use camino::Utf8PathBuf;
10use thiserror::Error;
11
12use crate::domain::debt::DebtError;
13use crate::domain::marker::MarkerError;
14
15#[derive(Debug, Error)]
17pub enum AppError {
18 #[error("usage: {0}")]
20 Usage(String),
21
22 #[error("{count} violation(s) found")]
25 Violations {
26 count: usize,
28 },
29
30 #[error("missing manifest: {0}")]
32 ManifestMissing(Utf8PathBuf),
33
34 #[error("invalid manifest: {0}")]
36 ManifestInvalid(String),
37
38 #[error(transparent)]
40 Marker(#[from] MarkerError),
41
42 #[error(transparent)]
45 Debt(#[from] DebtError),
46
47 #[error("{0}")]
50 Refused(String),
51
52 #[error("busy: {0}")]
54 Busy(String),
55
56 #[error("unrecovered: {0}")]
60 Unrecovered(String),
61
62 #[error("receipt: {0}")]
66 Receipt(String),
67
68 #[error("git: {message}")]
71 Git {
72 message: String,
74 code: u8,
76 },
77
78 #[error("unavailable: {0}")]
81 Unavailable(String),
82
83 #[error("io: {0}")]
85 Io(#[from] std::io::Error),
86
87 #[error(transparent)]
89 Other(#[from] anyhow::Error),
90}
91
92impl AppError {
93 #[must_use]
98 pub fn exit_code(&self) -> u8 {
99 match self {
100 Self::Violations { .. } => 1,
101 Self::Usage(_) => 64,
102 Self::ManifestInvalid(_)
103 | Self::Marker(_)
104 | Self::Debt(
105 DebtError::Shape(_) | DebtError::Malformed { .. } | DebtError::TwoFormats,
106 ) => 65,
107 Self::ManifestMissing(_) => 66,
108 Self::Refused(_)
109 | Self::Busy(_)
110 | Self::Unrecovered(_)
111 | Self::Receipt(_)
112 | Self::Debt(_) => 73,
113 Self::Git { code, .. } => *code,
114 Self::Unavailable(_) => 69,
115 Self::Io(e) if e.kind() == std::io::ErrorKind::NotFound => 66,
116 Self::Io(e) if e.kind() == std::io::ErrorKind::PermissionDenied => 77,
117 Self::Io(_) => 74,
118 Self::Other(_) => 70,
119 }
120 }
121
122 #[must_use]
124 pub const fn kind(&self) -> &'static str {
125 match self {
126 Self::Usage(_) => "Usage",
127 Self::Violations { .. } => "Violations",
128 Self::ManifestMissing(_) => "ManifestMissing",
129 Self::ManifestInvalid(_) => "ManifestInvalid",
130 Self::Marker(_) => "Marker",
131 Self::Debt(_) => "Debt",
132 Self::Refused(_) => "Refused",
133 Self::Busy(_) => "Busy",
134 Self::Unrecovered(_) => "Unrecovered",
135 Self::Receipt(_) => "Receipt",
136 Self::Git { .. } => "Git",
137 Self::Unavailable(_) => "Unavailable",
138 Self::Io(_) => "Io",
139 Self::Other(_) => "Other",
140 }
141 }
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 fn io(kind: std::io::ErrorKind) -> AppError {
149 AppError::Io(std::io::Error::from(kind))
150 }
151
152 #[test]
153 fn violations_are_one() {
154 assert_eq!(AppError::Violations { count: 3 }.exit_code(), 1);
155 }
156
157 #[test]
158 fn usage_is_sixty_four() {
159 assert_eq!(AppError::Usage("bad target".into()).exit_code(), 64);
160 }
161
162 #[test]
163 fn invalid_manifest_is_sixty_five() {
164 assert_eq!(
165 AppError::ManifestInvalid("schema_version 3".into()).exit_code(),
166 65
167 );
168 }
169
170 #[test]
171 fn malformed_marker_is_sixty_five() {
172 assert_eq!(AppError::Marker(MarkerError::Malformed).exit_code(), 65);
173 }
174
175 #[test]
176 fn a_malformed_debt_file_is_sixty_five_and_a_refused_debt_verb_is_seventy_three() {
177 assert_eq!(
178 AppError::Debt(DebtError::Shape("not yaml".into())).exit_code(),
179 65
180 );
181 assert_eq!(AppError::Debt(DebtError::TwoFormats).exit_code(), 65);
182 assert_eq!(AppError::Debt(DebtError::AlreadyBaselined).exit_code(), 73);
183 assert_eq!(AppError::Debt(DebtError::NothingToMigrate).exit_code(), 73);
184 }
185
186 #[test]
187 fn missing_manifest_is_sixty_six() {
188 assert_eq!(
189 AppError::ManifestMissing("x/.spec-driven-docs/manifest.json".into()).exit_code(),
190 66
191 );
192 }
193
194 #[test]
195 fn refused_is_seventy_three() {
196 assert_eq!(
197 AppError::Refused("apply aborted; the target was restored".into()).exit_code(),
198 73
199 );
200 }
201
202 #[test]
203 fn an_unavailable_program_is_sixty_nine() {
204 assert_eq!(
205 AppError::Unavailable("pre-commit could not run".into()).exit_code(),
206 69
207 );
208 }
209
210 #[test]
211 fn not_found_io_is_sixty_six() {
212 assert_eq!(io(std::io::ErrorKind::NotFound).exit_code(), 66);
213 }
214
215 #[test]
216 fn permission_denied_io_is_seventy_seven() {
217 assert_eq!(io(std::io::ErrorKind::PermissionDenied).exit_code(), 77);
218 }
219
220 #[test]
221 fn other_io_is_seventy_four() {
222 assert_eq!(io(std::io::ErrorKind::BrokenPipe).exit_code(), 74);
223 }
224
225 #[test]
226 fn anyhow_is_seventy() {
227 assert_eq!(AppError::Other(anyhow::anyhow!("boom")).exit_code(), 70);
228 }
229}