-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror.rs
More file actions
95 lines (79 loc) · 2.53 KB
/
error.rs
File metadata and controls
95 lines (79 loc) · 2.53 KB
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
//! Common error types for coding tools.
use thiserror::Error;
/// Unified error type for all tool operations.
#[derive(Debug, Error)]
pub enum ToolError {
/// File I/O operation failed.
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
/// Path validation failed (not absolute, doesn't exist, etc.).
#[error("invalid path: {0}")]
InvalidPath(String),
/// Requested offset/limit exceeds file bounds.
#[error("out of bounds: {0}")]
OutOfBounds(String),
/// Glob/regex pattern is invalid.
#[error("invalid pattern: {0}")]
InvalidPattern(String),
/// HTTP request failed.
#[error("HTTP error: {0}")]
Http(String),
/// Command execution failed.
#[error("execution error: {0}")]
Execution(String),
/// Timeout exceeded.
#[error("timeout: {0}")]
Timeout(String),
/// Timeout with kill failure - process may still be running.
#[error("timeout: {message}\n(kill failed: {kill_error})")]
TimeoutWithKillFailure {
/// Timeout message including context.
message: String,
/// Kill error message.
kill_error: String,
},
/// Validation failed.
#[error("validation error: {message}")]
Validation {
/// Field that failed validation, if applicable.
field: Option<String>,
/// Validation error message.
message: String,
},
/// JSON serialization/deserialization failed.
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
/// Permission denied for the requested operation.
#[error("permission denied for tool '{tool}' on '{subject}'")]
PermissionDenied {
/// Tool name that was denied.
tool: &'static str,
/// Path or command that was denied.
subject: String,
},
}
/// Result type alias for tool operations.
pub type ToolResult<T> = Result<T, ToolError>;
impl From<globset::Error> for ToolError {
fn from(e: globset::Error) -> Self {
ToolError::InvalidPattern(e.to_string())
}
}
impl ToolError {
/// Create a validation error without a specific field.
#[must_use]
pub fn validation(message: impl Into<String>) -> Self {
Self::Validation {
field: None,
message: message.into(),
}
}
/// Create a validation error for a specific field.
#[must_use]
pub fn validation_for(field: impl Into<String>, message: impl Into<String>) -> Self {
Self::Validation {
field: Some(field.into()),
message: message.into(),
}
}
}