1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4#[derive(Error, Debug, Clone, Serialize, Deserialize)]
5#[serde(tag = "code", content = "details", rename_all = "snake_case")]
6pub enum EditFailure {
7 #[error("file not found: {file_path}")]
8 FileNotFound { file_path: String },
9
10 #[error(
11 "edit #{edit_index} has an empty old_string; use write_file to create or overwrite files"
12 )]
13 EmptyOldString { edit_index: usize },
14
15 #[error("string not found for edit #{edit_index} in file {file_path}")]
16 StringNotFound {
17 file_path: String,
18 edit_index: usize,
19 },
20
21 #[error(
22 "found {occurrences} matches for edit #{edit_index} in file {file_path}; old_string must match exactly once"
23 )]
24 NonUniqueMatch {
25 file_path: String,
26 edit_index: usize,
27 occurrences: usize,
28 },
29}
30
31#[derive(Error, Debug, Clone, Serialize, Deserialize)]
32pub enum WorkspaceError {
33 #[error("I/O error: {0}")]
34 Io(String),
35
36 #[error("Tool execution failed: {0}")]
37 ToolExecution(String),
38
39 #[error("Edit failed: {0}")]
40 Edit(EditFailure),
41
42 #[error("Transport error: {0}")]
43 Transport(String),
44
45 #[error("Status error: {0}")]
46 Status(String),
47
48 #[error("Not supported: {0}")]
49 NotSupported(String),
50
51 #[error("Invalid configuration: {0}")]
52 InvalidConfiguration(String),
53
54 #[error("Remote workspace error: {0}")]
55 Remote(String),
56}
57
58pub type Result<T> = std::result::Result<T, WorkspaceError>;
59
60#[derive(Error, Debug, Clone, Serialize, Deserialize)]
61pub enum EnvironmentManagerError {
62 #[error("Environment not found: {0}")]
63 NotFound(String),
64
65 #[error("Environment operation not supported: {0}")]
66 NotSupported(String),
67
68 #[error("Invalid environment request: {0}")]
69 InvalidRequest(String),
70
71 #[error("I/O error: {0}")]
72 Io(String),
73
74 #[error("Environment manager error: {0}")]
75 Other(String),
76}
77
78pub type EnvironmentManagerResult<T> = std::result::Result<T, EnvironmentManagerError>;
79
80impl From<std::io::Error> for EnvironmentManagerError {
81 fn from(err: std::io::Error) -> Self {
82 EnvironmentManagerError::Io(err.to_string())
83 }
84}
85
86impl From<WorkspaceError> for EnvironmentManagerError {
87 fn from(err: WorkspaceError) -> Self {
88 match err {
89 WorkspaceError::Io(message) => EnvironmentManagerError::Io(message),
90 other => EnvironmentManagerError::Other(other.to_string()),
91 }
92 }
93}
94
95#[derive(Error, Debug, Clone, Serialize, Deserialize)]
96pub enum WorkspaceManagerError {
97 #[error("Workspace not found: {0}")]
98 NotFound(String),
99
100 #[error("Workspace operation not supported: {0}")]
101 NotSupported(String),
102
103 #[error("Invalid workspace request: {0}")]
104 InvalidRequest(String),
105
106 #[error("I/O error: {0}")]
107 Io(String),
108
109 #[error("Workspace manager error: {0}")]
110 Other(String),
111}
112
113pub type WorkspaceManagerResult<T> = std::result::Result<T, WorkspaceManagerError>;
114
115impl From<std::io::Error> for WorkspaceManagerError {
116 fn from(err: std::io::Error) -> Self {
117 WorkspaceManagerError::Io(err.to_string())
118 }
119}
120
121impl From<WorkspaceError> for WorkspaceManagerError {
122 fn from(err: WorkspaceError) -> Self {
123 match err {
124 WorkspaceError::Io(message) => WorkspaceManagerError::Io(message),
125 other => WorkspaceManagerError::Other(other.to_string()),
126 }
127 }
128}
129
130impl From<tonic::transport::Error> for WorkspaceError {
131 fn from(err: tonic::transport::Error) -> Self {
132 WorkspaceError::Transport(err.to_string())
133 }
134}
135
136impl From<tonic::Status> for WorkspaceError {
137 fn from(err: tonic::Status) -> Self {
138 WorkspaceError::Status(err.to_string())
139 }
140}
141
142impl From<std::io::Error> for WorkspaceError {
143 fn from(err: std::io::Error) -> Self {
144 WorkspaceError::Io(err.to_string())
145 }
146}
147
148impl From<EditFailure> for WorkspaceError {
149 fn from(err: EditFailure) -> Self {
150 WorkspaceError::Edit(err)
151 }
152}