Skip to main content

lean_ctx/core/
error.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum LeanCtxError {
6    #[error("configuration error: {0}")]
7    Config(String),
8
9    #[error("I/O error: {0}")]
10    Io(#[from] std::io::Error),
11
12    #[error("parse error: {0}")]
13    Parse(#[from] serde_json::Error),
14
15    #[error("lock error: {0}")]
16    Lock(String),
17
18    #[error("network error: {0}")]
19    Network(String),
20
21    #[error("plugin error: {0}")]
22    Plugin(String),
23
24    #[error("internal error: {0}")]
25    Internal(String),
26}
27
28#[derive(Error, Debug)]
29pub enum ConfigError {
30    #[error("Cannot determine config path")]
31    MissingPath,
32
33    #[error("Unknown config key: {key}")]
34    UnknownKey { key: String },
35
36    #[error("Cannot read config: {source}")]
37    Read {
38        #[source]
39        source: std::io::Error,
40    },
41
42    #[error("Config parse error: {source}")]
43    ParseToml {
44        #[source]
45        source: toml::de::Error,
46    },
47
48    #[error("Invalid value for '{key}': {message}")]
49    InvalidValue { key: String, message: String },
50
51    #[error("Error saving config: {source}")]
52    Save {
53        #[source]
54        source: Box<LeanCtxError>,
55    },
56
57    #[error("Expected bool (true/false), got: {value}")]
58    ExpectedBool { value: String },
59
60    #[error("Expected integer, got: {value}")]
61    ExpectedInteger { value: String },
62
63    #[error("Expected unsigned integer, got: {value}")]
64    ExpectedUnsignedInteger { value: String },
65
66    #[error("Expected number, got: {value}")]
67    ExpectedNumber { value: String },
68
69    #[error("Invalid value '{value}'. Allowed: {allowed}")]
70    InvalidEnumValue { value: String, allowed: String },
71
72    #[error("Cannot set table '{value}' via CLI. Edit config.toml directly.")]
73    CannotSetTable { value: String },
74
75    #[error(
76        "Cannot set '{key}': '{part}' already holds a non-table value in config.toml. Fix or remove that key first."
77    )]
78    NonTableParent { key: String, part: String },
79
80    #[error("{0}")]
81    Message(String),
82}
83
84#[derive(Error, Debug)]
85pub enum DispatchError {
86    #[error("path resolution failed: {message}")]
87    PathResolution { message: String },
88
89    #[error("{message}")]
90    Tool { message: String },
91}
92
93#[derive(Error, Debug)]
94pub enum ShellError {
95    #[error("{message}")]
96    Blocked { message: String },
97}
98
99impl From<String> for ShellError {
100    fn from(message: String) -> Self {
101        Self::Blocked { message }
102    }
103}
104
105impl From<&str> for ShellError {
106    fn from(message: &str) -> Self {
107        Self::Blocked {
108            message: message.to_string(),
109        }
110    }
111}
112
113impl ShellError {
114    pub fn message(&self) -> &str {
115        match self {
116            Self::Blocked { message } => message,
117        }
118    }
119
120    pub fn contains(&self, needle: &str) -> bool {
121        self.message().contains(needle)
122    }
123
124    pub fn lines(&self) -> std::str::Lines<'_> {
125        self.message().lines()
126    }
127}
128
129#[derive(Error, Debug)]
130pub enum PathJailError {
131    #[error("protected runtime context access denied: {reason}")]
132    ProtectedContext { reason: String },
133
134    #[error("path contains null byte")]
135    NullByte,
136
137    #[error("path does not exist and has no existing ancestor: {path}")]
138    NoExistingAncestor { path: PathBuf },
139
140    #[error("path escapes project root: {path} (root: {root}){hint}")]
141    EscapesRoot {
142        path: PathBuf,
143        root: PathBuf,
144        hint: String,
145    },
146
147    #[error("post-canonicalize jail escape detected: {path} resolves to {resolved}")]
148    PostCanonicalizeEscape { path: PathBuf, resolved: PathBuf },
149
150    #[error("symlink not allowed in jailed path: {path}")]
151    Symlink { path: PathBuf },
152
153    /// Outside every root of the launcher-bound Workspace (#2120).
154    #[error(
155        "path is outside the Workspace '{workspace}' (rev {revision}): {path} — attached roots: {roots}"
156    )]
157    OutsideWorkspace {
158        path: PathBuf,
159        workspace: String,
160        revision: u64,
161        roots: String,
162    },
163
164    /// A Workspace is configured but unusable: every path is denied (#2120).
165    #[error("{reason}")]
166    WorkspaceUnavailable { reason: String },
167}
168
169impl From<String> for ConfigError {
170    fn from(message: String) -> Self {
171        ConfigError::Message(message)
172    }
173}
174
175impl From<&str> for ConfigError {
176    fn from(message: &str) -> Self {
177        ConfigError::Message(message.to_string())
178    }
179}
180
181impl From<toml::de::Error> for LeanCtxError {
182    fn from(e: toml::de::Error) -> Self {
183        LeanCtxError::Config(e.to_string())
184    }
185}
186
187pub type Result<T> = std::result::Result<T, LeanCtxError>;