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
154impl From<String> for ConfigError {
155    fn from(message: String) -> Self {
156        ConfigError::Message(message)
157    }
158}
159
160impl From<&str> for ConfigError {
161    fn from(message: &str) -> Self {
162        ConfigError::Message(message.to_string())
163    }
164}
165
166impl From<toml::de::Error> for LeanCtxError {
167    fn from(e: toml::de::Error) -> Self {
168        LeanCtxError::Config(e.to_string())
169    }
170}
171
172pub type Result<T> = std::result::Result<T, LeanCtxError>;