1use std::fmt;
2
3pub(crate) const FILE_CHANGED_SINCE_READ_MESSAGE: &str =
4 "{path} changed on disk after this call read it; nothing was written. Read the file again and retry.";
5
6#[derive(Debug)]
11pub enum AftError {
12 GrammarParseTimeout {
13 path: String,
14 language: String,
15 timeout_ms: u64,
16 },
17 GrammarUnavailable {
18 language: String,
19 state: crate::grammar_state::GrammarState,
20 message: String,
21 cause: Option<crate::grammar_state::FailureCause>,
22 retry_after_ms: Option<u64>,
23 },
24 SymbolNotFound {
25 name: String,
26 file: String,
27 },
28 AmbiguousSymbol {
29 name: String,
30 candidates: Vec<String>,
31 },
32 ParseError {
33 message: String,
34 },
35 FileNotFound {
36 path: String,
37 },
38 FileChangedSinceRead {
39 path: String,
40 },
41 InvalidRequest {
42 message: String,
43 },
44 CheckpointNotFound {
45 name: String,
46 },
47 NoUndoHistory {
48 path: String,
49 },
50 AmbiguousMatch {
51 pattern: String,
52 count: usize,
53 },
54 ScopeNotFound {
55 scope: String,
56 available: Vec<String>,
57 file: String,
58 },
59 MemberNotFound {
60 member: String,
61 scope: String,
62 file: String,
63 },
64 IoError {
65 path: String,
66 message: String,
67 },
68 AmbiguousIndentation {
72 message: String,
73 },
74 DeniedFloor {
75 path: String,
76 },
77}
78
79impl AftError {
80 pub fn code(&self) -> &'static str {
82 match self {
83 AftError::GrammarParseTimeout { .. } => "grammar_parse_timeout",
84 AftError::GrammarUnavailable { .. } => "grammar_unavailable",
85 AftError::SymbolNotFound { .. } => "symbol_not_found",
86 AftError::AmbiguousSymbol { .. } => "ambiguous_symbol",
87 AftError::ParseError { .. } => "parse_error",
88 AftError::FileNotFound { .. } => "file_not_found",
89 AftError::FileChangedSinceRead { .. } => "file_changed_since_read",
90 AftError::InvalidRequest { .. } => "invalid_request",
91 AftError::CheckpointNotFound { .. } => "checkpoint_not_found",
92 AftError::NoUndoHistory { .. } => "no_undo_history",
93 AftError::AmbiguousMatch { .. } => "ambiguous_match",
94 AftError::ScopeNotFound { .. } => "scope_not_found",
95 AftError::MemberNotFound { .. } => "member_not_found",
96 AftError::IoError { .. } => "io_error",
97 AftError::AmbiguousIndentation { .. } => "ambiguous_indentation",
98 AftError::DeniedFloor { .. } => "denied_floor",
99 }
100 }
101
102 pub fn to_error_json(&self) -> serde_json::Value {
106 if let Self::GrammarParseTimeout {
107 path,
108 language,
109 timeout_ms,
110 } = self
111 {
112 return serde_json::json!({ "code": self.code(), "message": self.to_string(), "path": path, "language": language, "timeout_ms": timeout_ms });
113 }
114 if let Self::GrammarUnavailable {
115 language,
116 state,
117 message,
118 cause,
119 retry_after_ms,
120 } = self
121 {
122 let mut value = serde_json::json!({
123 "code": self.code(), "language": language, "state": state, "message": message,
124 });
125 if let Some(cause) = cause {
126 value["cause"] = serde_json::json!(cause);
127 }
128 if let Some(retry_after_ms) = retry_after_ms {
129 value["retry_after_ms"] = serde_json::json!(retry_after_ms);
130 }
131 return value;
132 }
133 serde_json::json!({
134 "code": self.code(),
135 "message": self.to_string(),
136 })
137 }
138}
139
140impl fmt::Display for AftError {
141 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142 match self {
143 AftError::GrammarParseTimeout { path, timeout_ms, .. } => write!(f, "Luau parsing exceeded the {timeout_ms} ms time limit for {path}; no syntax tree was returned."),
144 AftError::GrammarUnavailable { message, .. } => f.write_str(message),
145 AftError::DeniedFloor { path } => {
146 f.write_str(&aft_storage_denial_message(path))
147 }
148 AftError::SymbolNotFound { name, file } => {
149 write!(f, "symbol '{}' not found in {}", name, file)
150 }
151 AftError::AmbiguousSymbol { name, candidates } => {
152 write!(
153 f,
154 "symbol '{}' is ambiguous, candidates: [{}]",
155 name,
156 candidates.join(", ")
157 )
158 }
159 AftError::ParseError { message } => {
160 write!(f, "parse error: {}", message)
161 }
162 AftError::FileNotFound { path } => {
163 write!(f, "file not found: {}", path)
164 }
165 AftError::FileChangedSinceRead { path } => {
166 f.write_str(&FILE_CHANGED_SINCE_READ_MESSAGE.replace("{path}", path))
167 }
168 AftError::InvalidRequest { message } => {
169 write!(f, "invalid request: {}", message)
170 }
171 AftError::CheckpointNotFound { name } => {
172 write!(f, "checkpoint not found: {}", name)
173 }
174 AftError::NoUndoHistory { path } => {
175 write!(f, "no undo history for: {}", path)
176 }
177 AftError::AmbiguousMatch { pattern, count } => {
178 write!(
179 f,
180 "pattern '{}' matches {} occurrences, expected exactly 1",
181 pattern, count
182 )
183 }
184 AftError::ScopeNotFound {
185 scope,
186 available,
187 file,
188 } => {
189 if available.is_empty() {
190 write!(
191 f,
192 "scope '{}' not found in {} (no scopes available)",
193 scope, file
194 )
195 } else {
196 write!(
197 f,
198 "scope '{}' not found in {}, available: [{}]",
199 scope,
200 file,
201 available.join(", ")
202 )
203 }
204 }
205 AftError::MemberNotFound {
206 member,
207 scope,
208 file,
209 } => {
210 write!(
211 f,
212 "member '{}' not found in scope '{}' in {}",
213 member, scope, file
214 )
215 }
216 AftError::IoError { path, message } => {
217 write!(f, "I/O error on {}: {}", path, message)
218 }
219 AftError::AmbiguousIndentation { message } => f.write_str(message),
220 }
221 }
222}
223
224impl std::error::Error for AftError {}
225
226pub fn aft_storage_denial_message(path: impl std::fmt::Display) -> String {
227 format!("path '{path}' is in AFT's own storage, which file tools never open. For a shell task's output, use bash_status.")
228}