Skip to main content

pforge_runtime/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum Error {
5    #[error("Tool not found: {0}")]
6    ToolNotFound(String),
7
8    #[error("Handler error: {0}")]
9    Handler(String),
10
11    #[error("Serialization error: {0}")]
12    Serialization(#[from] serde_json::Error),
13
14    #[error("IO error: {0}")]
15    Io(#[from] std::io::Error),
16
17    #[error("HTTP error: {0}")]
18    Http(String),
19
20    #[error("State error: {0}")]
21    StateError(String),
22
23    #[error("Timeout error")]
24    Timeout,
25}
26
27impl Error {
28    /// Error for functionality whose cargo feature was compiled out.
29    ///
30    /// `sse`, `websocket` and `http-handlers` are optional, but a config naming
31    /// them still PARSES with them off — the config types are feature-independent,
32    /// and they have to be, or the same `forge.yaml` would mean different things
33    /// to different builds of the same version.
34    ///
35    /// So the failure belongs here, at construction, naming the missing feature
36    /// and how to get it back. The tempting alternatives are worse: a panic turns
37    /// a configuration mistake into a crash with no remedy in the message, and
38    /// silently skipping the thing leaves a server that starts, reports healthy,
39    /// and does not serve what its operator configured.
40    pub fn feature_disabled(feature: &str, subject: &str) -> Self {
41        Error::Handler(format!(
42            "{subject} is unavailable: this binary was built without the `{feature}` \
43             cargo feature. Rebuild with `--features {feature}` (or with default \
44             features) to enable it."
45        ))
46    }
47}
48
49pub type Result<T> = std::result::Result<T, Error>;