Skip to main content

sokf_core/
error.rs

1//! The core has one error type, `Error`. The command-line interface (CLI)
2//! exits with code 2 for every variant.
3
4use std::path::PathBuf;
5
6use crate::RepoPath;
7
8/// `Result` is the result type of this crate, with `Error` as its error.
9pub type Result<T> = std::result::Result<T, Error>;
10
11/// `Error` holds the errors of this crate. Every variant renders a message
12/// that the CLI prints unchanged after `error: `.
13#[derive(Debug, thiserror::Error)]
14pub enum Error {
15    /// The tool finds no repository root (CLI-1_AC-5).
16    #[error("not in a Git repository; pass --repo-root")]
17    NoRepository,
18
19    /// The tool refuses a `config.toml` file (CONF-1_AC-3, CONF-1_AC-4,
20    /// CONF-2_AC-6, CONF-2_AC-7, CONF-3_AC-5, CONF-3_AC-6), or a file of
21    /// texts (CONF-12_AC-3, CONF-12_AC-4, CONF-12_AC-5).
22    #[error("{path}: {message}")]
23    Config {
24        /// `path` is the path of the configuration file that the tool refuses.
25        path: RepoPath,
26        /// The message says what is wrong with the file.
27        message: String,
28    },
29
30    /// The tool refuses a `formats.yaml` file.
31    #[error("{path}: {message}")]
32    FormatsFile {
33        /// `path` is the path of the formats file that the tool refuses.
34        path: RepoPath,
35        /// The message says what is wrong with the file.
36        message: String,
37    },
38
39    /// The tool refuses a `PATHS` argument whose path is not in the
40    /// repository (CLI-4_AC-15).
41    #[error("{0}: no such path in the repository")]
42    Path(String),
43
44    /// `init` refuses to run (contract-cli CLI-2), and the message identifies
45    /// the path or the option at fault.
46    #[error("init: {0}")]
47    Init(String),
48
49    /// `harness` refuses to run (contract-cli CLI-3), and the message
50    /// identifies the harness, part, event, or file at fault.
51    #[error("harness: {0}")]
52    Harness(String),
53
54    /// `new` refuses to run (contract-cli CLI-6).
55    #[error("new: {0}")]
56    New(String),
57
58    /// `info` refuses to run (contract-cli CLI-5).
59    #[error("info: {0}")]
60    Info(String),
61
62    /// `graph` refuses a `START` argument, and the message lists each way the
63    /// tool can read the argument (CLI-7_AC-19).
64    #[error("graph: {0}")]
65    Start(String),
66
67    /// `guide` refuses an argument, a document, or a `--state` (contract-cli
68    /// CLI-17), and the message names the argument or the document.
69    #[error("guide: {0}")]
70    Guide(String),
71
72    /// `locate` refuses an `id`, and the message gives the `id` and the paths
73    /// of the documents that declare it (CLI-8_AC-6).
74    #[error("locate: {0}")]
75    Locate(String),
76
77    /// A JMESPath expression doesn't compile (CLI-4_AC-17,
78    /// CLI-7_AC-21).
79    #[error("query: {0}")]
80    Query(String),
81
82    /// An input or output (I/O) operation fails at a path.
83    #[error("I/O error at {path}: {source}")]
84    Io {
85        /// `path` is the path at which the operation fails.
86        path: PathBuf,
87        /// `source` is the I/O error that causes the failure.
88        #[source]
89        source: std::io::Error,
90    },
91
92    /// The code breaks one of its own invariants.
93    #[error("internal: {0}")]
94    Internal(String),
95}
96
97impl From<tramlines_md::Error> for Error {
98    fn from(e: tramlines_md::Error) -> Self {
99        match e {
100            tramlines_md::Error::Internal(m) => Error::Internal(m),
101        }
102    }
103}
104
105/// An error of agent-harness-adapter is an I/O error at its path, or else a
106/// refusal of `harness`.
107impl From<agent_harness_adapter_core::Error> for Error {
108    fn from(e: agent_harness_adapter_core::Error) -> Self {
109        match e {
110            agent_harness_adapter_core::Error::Io { path, source } => Error::Io { path, source },
111            agent_harness_adapter_core::Error::Internal(m) => Error::Internal(m),
112            other => Error::Harness(other.to_string()),
113        }
114    }
115}
116
117impl Error {
118    /// `io` builds an [`Error::Io`] for `path` from a [`std::io::Error`].
119    pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
120        Error::Io {
121            path: path.into(),
122            source,
123        }
124    }
125
126    /// `config` builds an [`Error::Config`].
127    pub fn config(path: RepoPath, message: impl Into<String>) -> Self {
128        Error::Config {
129            path,
130            message: message.into(),
131        }
132    }
133
134    /// `formats` builds an [`Error::FormatsFile`].
135    pub fn formats(path: RepoPath, message: impl Into<String>) -> Self {
136        Error::FormatsFile {
137            path,
138            message: message.into(),
139        }
140    }
141}
142
143#[cfg(test)]
144mod tests {
145    use std::{error::Error as _, io, path::Path};
146
147    use super::*;
148
149    #[test]
150    fn io_constructor_and_display() {
151        let e = Error::io(
152            Path::new("/tmp/x"),
153            io::Error::new(io::ErrorKind::PermissionDenied, "denied"),
154        );
155        assert!(matches!(e, Error::Io { .. }));
156        let msg = e.to_string();
157        assert!(
158            msg.contains("/tmp/x"),
159            "the message contains the path: {msg}"
160        );
161        assert!(
162            msg.contains("denied"),
163            "the message contains the source error: {msg}"
164        );
165        assert!(e.source().is_some());
166    }
167
168    #[test]
169    fn a_kit_error_is_an_io_error_or_a_harness_refusal() {
170        let e: Error = agent_harness_adapter_core::Error::io(
171            "/tmp/x",
172            io::Error::new(io::ErrorKind::PermissionDenied, "denied"),
173        )
174        .into();
175        assert!(matches!(e, Error::Io { .. }), "{e}");
176        let e: Error = agent_harness_adapter_core::Error::file("/.sokf/config.toml", "bad").into();
177        assert_eq!(e.to_string(), "harness: /.sokf/config.toml: bad");
178        let e: Error = agent_harness_adapter_core::Error::Internal("bug".into()).into();
179        assert_eq!(e.to_string(), "internal: bug");
180        let e: Error = agent_harness_adapter_core::Error::UnknownHarness {
181            harness: "cline".into(),
182        }
183        .into();
184        assert_eq!(e.to_string(), "harness: no harness named `cline`");
185    }
186
187    #[test]
188    fn every_message_names_its_subject() {
189        let path = RepoPath::parse("/.sokf/config.toml").unwrap();
190        assert_eq!(
191            Error::config(path.clone(), "unknown key").to_string(),
192            "/.sokf/config.toml: unknown key"
193        );
194        assert_eq!(
195            Error::formats(path, "bad").to_string(),
196            "/.sokf/config.toml: bad"
197        );
198        assert_eq!(
199            Error::Path("x".into()).to_string(),
200            "x: no such path in the repository"
201        );
202        assert_eq!(Error::New("no schema".into()).to_string(), "new: no schema");
203        assert_eq!(Error::Info("x".into()).to_string(), "info: x");
204        assert_eq!(Error::Init("x".into()).to_string(), "init: x");
205        assert_eq!(Error::Harness("x".into()).to_string(), "harness: x");
206        assert_eq!(Error::Start("x".into()).to_string(), "graph: x");
207        assert_eq!(Error::Locate("x".into()).to_string(), "locate: x");
208        assert_eq!(Error::Guide("x".into()).to_string(), "guide: x");
209        assert_eq!(Error::Query("x".into()).to_string(), "query: x");
210        assert_eq!(Error::Internal("x".into()).to_string(), "internal: x");
211        assert!(Error::NoRepository.to_string().contains("--repo-root"));
212        assert!(Error::NoRepository.source().is_none());
213    }
214}