1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Crate-level error type and exit-code mapping.
//!
//! [`AppError`] aggregates errors from every layer via `#[from]`, and
//! [`AppError::exit_code`] maps each variant to its process exit code. No
//! other module decides exit codes. Exit `1` is reserved for one meaning —
//! a check ran and found violations — while the BSD sysexits range covers
//! the tool failing to do its job at all.
use camino::Utf8PathBuf;
use thiserror::Error;
use crate::domain::debt::DebtError;
use crate::domain::marker::MarkerError;
/// Every failure the binary can exit with.
#[derive(Debug, Error)]
pub enum AppError {
/// Semantically invalid arguments clap cannot reject on shape alone.
#[error("usage: {0}")]
Usage(String),
/// A check ran to completion and found violations; the findings are
/// already on stdout, so the process only carries the red exit.
#[error("{count} violation(s) found")]
Violations {
/// How many findings were reported.
count: usize,
},
/// The target has no instance manifest where one is required.
#[error("missing manifest: {0}")]
ManifestMissing(Utf8PathBuf),
/// The manifest exists but does not parse as a supported schema.
#[error("invalid manifest: {0}")]
ManifestInvalid(String),
/// The managed pre-commit block or its host file is malformed.
#[error(transparent)]
Marker(#[from] MarkerError),
/// The instance's debt file cannot be trusted, or a debt verb was asked
/// for a state it refuses.
#[error(transparent)]
Debt(#[from] DebtError),
/// The install or upgrade refused to touch the target as found, and the
/// target was left (or restored) unchanged.
#[error("{0}")]
Refused(String),
/// Another process holds the lock this run needs. Nothing was written.
#[error("busy: {0}")]
Busy(String),
/// A run the process did not finish could not be put back, so the
/// destinations it names are in an unknown state and no new work may
/// start over them.
#[error("unrecovered: {0}")]
Unrecovered(String),
/// The apply could not write the record that vouches for what it wrote.
/// The files were put back, because a landing this tool cannot vouch
/// for is a landing it will refuse to take back.
#[error("receipt: {0}")]
Receipt(String),
/// A tracked-upstream lookup failed. The code is the sysexit the failure
/// class maps to, chosen where the `git` adapter is called.
#[error("git: {message}")]
Git {
/// The redacted failure reason.
message: String,
/// The sysexit code for this failure class.
code: u8,
},
/// Filesystem failure, classified by its I/O kind.
#[error("io: {0}")]
Io(#[from] std::io::Error),
/// Escape hatch for ad-hoc contexts at the binary boundary.
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl AppError {
/// Map to the process exit code.
///
/// `1` means a completed check found violations; `64..=78` follow BSD
/// `sysexits(3)` and mean the tool itself could not do its job.
#[must_use]
pub fn exit_code(&self) -> u8 {
match self {
Self::Violations { .. } => 1,
Self::Usage(_) => 64,
Self::ManifestInvalid(_)
| Self::Marker(_)
| Self::Debt(
DebtError::Shape(_) | DebtError::Malformed { .. } | DebtError::TwoFormats,
) => 65,
Self::ManifestMissing(_) => 66,
Self::Refused(_)
| Self::Busy(_)
| Self::Unrecovered(_)
| Self::Receipt(_)
| Self::Debt(_) => 73,
Self::Git { code, .. } => *code,
Self::Io(e) if e.kind() == std::io::ErrorKind::NotFound => 66,
Self::Io(e) if e.kind() == std::io::ErrorKind::PermissionDenied => 77,
Self::Io(_) => 74,
Self::Other(_) => 70,
}
}
/// Stable machine-readable kind for the error envelope.
#[must_use]
pub const fn kind(&self) -> &'static str {
match self {
Self::Usage(_) => "Usage",
Self::Violations { .. } => "Violations",
Self::ManifestMissing(_) => "ManifestMissing",
Self::ManifestInvalid(_) => "ManifestInvalid",
Self::Marker(_) => "Marker",
Self::Debt(_) => "Debt",
Self::Refused(_) => "Refused",
Self::Busy(_) => "Busy",
Self::Unrecovered(_) => "Unrecovered",
Self::Receipt(_) => "Receipt",
Self::Git { .. } => "Git",
Self::Io(_) => "Io",
Self::Other(_) => "Other",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn io(kind: std::io::ErrorKind) -> AppError {
AppError::Io(std::io::Error::from(kind))
}
#[test]
fn violations_are_one() {
assert_eq!(AppError::Violations { count: 3 }.exit_code(), 1);
}
#[test]
fn usage_is_sixty_four() {
assert_eq!(AppError::Usage("bad target".into()).exit_code(), 64);
}
#[test]
fn invalid_manifest_is_sixty_five() {
assert_eq!(
AppError::ManifestInvalid("schema_version 3".into()).exit_code(),
65
);
}
#[test]
fn malformed_marker_is_sixty_five() {
assert_eq!(AppError::Marker(MarkerError::Malformed).exit_code(), 65);
}
#[test]
fn a_malformed_debt_file_is_sixty_five_and_a_refused_debt_verb_is_seventy_three() {
assert_eq!(
AppError::Debt(DebtError::Shape("not yaml".into())).exit_code(),
65
);
assert_eq!(AppError::Debt(DebtError::TwoFormats).exit_code(), 65);
assert_eq!(AppError::Debt(DebtError::AlreadyBaselined).exit_code(), 73);
assert_eq!(AppError::Debt(DebtError::NothingToMigrate).exit_code(), 73);
}
#[test]
fn missing_manifest_is_sixty_six() {
assert_eq!(
AppError::ManifestMissing("x/.spec-driven-docs/manifest.json".into()).exit_code(),
66
);
}
#[test]
fn refused_is_seventy_three() {
assert_eq!(
AppError::Refused("apply aborted; the target was restored".into()).exit_code(),
73
);
}
#[test]
fn not_found_io_is_sixty_six() {
assert_eq!(io(std::io::ErrorKind::NotFound).exit_code(), 66);
}
#[test]
fn permission_denied_io_is_seventy_seven() {
assert_eq!(io(std::io::ErrorKind::PermissionDenied).exit_code(), 77);
}
#[test]
fn other_io_is_seventy_four() {
assert_eq!(io(std::io::ErrorKind::BrokenPipe).exit_code(), 74);
}
#[test]
fn anyhow_is_seventy() {
assert_eq!(AppError::Other(anyhow::anyhow!("boom")).exit_code(), 70);
}
}