1use super::ErrorKind;
4use super::definition::ErrorCodeDefinition;
5use super::definition::drains;
6use super::definition::fatal;
7use super::definition::preempted;
8
9pub const SESSION_NOT_LOADED_CODE: i64 = -32024;
12
13pub const SESSION_STREAM_MISMATCH_CODE: i64 = -32025;
17
18pub const LIMITS_ABOVE_MAX_CODE: i64 = -32028;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32#[non_exhaustive]
33pub enum ErrorCode {
34 ParseError,
36 InvalidRequest,
38 MethodNotFound,
40 InvalidParams,
42 Internal,
44 Overloaded,
46 InputTooLarge,
48 CapabilityRequired,
50 NotFound,
52 Interrupted,
54 Cancelled,
56 SessionNotFound,
58 SessionInUse,
60 SessionAmbiguous,
62 ForkBoundaryInvalid,
64 SessionNotLoaded,
66 SessionStreamMismatch,
69 LimitsAboveMax,
72 WorkspaceNotTrusted,
74 CommandRejected,
76 Backpressured,
78 SkillNotFound,
81 ComputerUseUnavailable,
83 MarketplaceNotFound,
85 PluginNotFound,
87 MarketplaceAlreadyExists,
89 PluginMutationsDisabled,
91 UploadNotFound,
94 ViewTruncated,
96 OutputUnavailable,
100 Boundary,
102 ApprovalNotFound,
104 ApprovalAlreadyResolved,
106 ApprovalChoiceInvalid,
108 ApprovalRequirementStale,
110 ApprovalReviewerUnavailable,
112 UserInputNotFound,
114 UserInputAlreadySettled,
116 UserInputAnswerInvalid,
118 MissingAnchor,
120 PrunedPrefix,
122 WrongStream,
124 UnknownStream,
126 UnknownProjection,
128 AdmissionNotFound,
130 AdmissionAlreadyResolved,
132 AdmissionChoiceInvalid,
134 UsageRefreshRateLimited,
136 UsageRefreshUnavailable,
138 UsageRefreshFailed,
140}
141
142impl ErrorCode {
143 pub const ALL: [Self; 50] = [
148 Self::ParseError,
149 Self::InvalidRequest,
150 Self::MethodNotFound,
151 Self::InvalidParams,
152 Self::Internal,
153 Self::Overloaded,
154 Self::InputTooLarge,
155 Self::CapabilityRequired,
156 Self::NotFound,
157 Self::Interrupted,
158 Self::Cancelled,
159 Self::SessionNotFound,
160 Self::SessionInUse,
161 Self::SessionAmbiguous,
162 Self::ForkBoundaryInvalid,
163 Self::SessionNotLoaded,
164 Self::SessionStreamMismatch,
165 Self::LimitsAboveMax,
166 Self::WorkspaceNotTrusted,
167 Self::CommandRejected,
168 Self::Backpressured,
169 Self::SkillNotFound,
170 Self::ComputerUseUnavailable,
171 Self::MarketplaceNotFound,
172 Self::PluginNotFound,
173 Self::MarketplaceAlreadyExists,
174 Self::PluginMutationsDisabled,
175 Self::UploadNotFound,
176 Self::ViewTruncated,
177 Self::OutputUnavailable,
178 Self::Boundary,
179 Self::ApprovalNotFound,
180 Self::ApprovalAlreadyResolved,
181 Self::ApprovalChoiceInvalid,
182 Self::ApprovalRequirementStale,
183 Self::ApprovalReviewerUnavailable,
184 Self::UserInputNotFound,
185 Self::UserInputAlreadySettled,
186 Self::UserInputAnswerInvalid,
187 Self::MissingAnchor,
188 Self::PrunedPrefix,
189 Self::WrongStream,
190 Self::UnknownStream,
191 Self::UnknownProjection,
192 Self::AdmissionNotFound,
193 Self::AdmissionAlreadyResolved,
194 Self::AdmissionChoiceInvalid,
195 Self::UsageRefreshRateLimited,
196 Self::UsageRefreshUnavailable,
197 Self::UsageRefreshFailed,
198 ];
199
200 const fn definition(self) -> ErrorCodeDefinition {
207 match self {
208 Self::ParseError => fatal(-32700, ErrorKind::ParseError),
209 Self::InvalidRequest => ErrorCodeDefinition::new(
210 -32600,
211 ErrorKind::InvalidRequest,
212 &[ErrorKind::NotInitialized, ErrorKind::AlreadyInitialized],
213 Some(false),
214 ),
215 Self::MethodNotFound => ErrorCodeDefinition::new(
216 -32601,
217 ErrorKind::MethodNotFound,
218 &[ErrorKind::ExperimentalRequired],
219 Some(false),
220 ),
221 Self::InvalidParams => ErrorCodeDefinition::new(
222 -32602,
223 ErrorKind::InvalidParams,
224 &[ErrorKind::ExperimentalRequired],
225 Some(false),
226 ),
227 Self::Internal => ErrorCodeDefinition::new(
228 -32603,
229 ErrorKind::Internal,
230 &[
231 ErrorKind::PageEventTooLarge,
232 ErrorKind::OutputResultTooLarge,
233 ],
234 Some(false),
235 ),
236 Self::Overloaded => drains(-32001, ErrorKind::Overloaded),
237 Self::InputTooLarge => fatal(-32002, ErrorKind::InputTooLarge),
238 Self::CapabilityRequired => fatal(-32010, ErrorKind::CapabilityRequired),
239 Self::NotFound => fatal(-32011, ErrorKind::NotFound),
240 Self::Interrupted => preempted(-32013, ErrorKind::Interrupted),
241 Self::Cancelled => preempted(-32014, ErrorKind::Cancelled),
242 Self::SessionNotFound => fatal(-32020, ErrorKind::SessionNotFound),
243 Self::SessionInUse => fatal(-32021, ErrorKind::SessionInUse),
244 Self::SessionAmbiguous => fatal(-32022, ErrorKind::SessionAmbiguous),
245 Self::ForkBoundaryInvalid => fatal(-32023, ErrorKind::ForkBoundaryInvalid),
246 Self::SessionNotLoaded => fatal(SESSION_NOT_LOADED_CODE, ErrorKind::SessionNotLoaded),
247 Self::SessionStreamMismatch => fatal(
248 SESSION_STREAM_MISMATCH_CODE,
249 ErrorKind::SessionStreamMismatch,
250 ),
251 Self::LimitsAboveMax => {
252 ErrorCodeDefinition::experimental(LIMITS_ABOVE_MAX_CODE, ErrorKind::LimitsAboveMax)
253 }
254 Self::WorkspaceNotTrusted => {
255 ErrorCodeDefinition::experimental(-32029, ErrorKind::WorkspaceNotTrusted)
256 }
257 Self::CommandRejected => fatal(-32030, ErrorKind::CommandRejected),
258 Self::Backpressured => drains(-32031, ErrorKind::Backpressured),
259 Self::SkillNotFound => fatal(-32032, ErrorKind::SkillNotFound),
260 Self::ComputerUseUnavailable => fatal(-32033, ErrorKind::ComputerUseUnavailable),
261 Self::MarketplaceNotFound => fatal(-32034, ErrorKind::MarketplaceNotFound),
262 Self::PluginNotFound => fatal(-32035, ErrorKind::PluginNotFound),
263 Self::MarketplaceAlreadyExists => fatal(-32036, ErrorKind::MarketplaceAlreadyExists),
264 Self::PluginMutationsDisabled => fatal(-32037, ErrorKind::PluginMutationsDisabled),
265 Self::UploadNotFound => fatal(-32038, ErrorKind::UploadNotFound),
266 Self::ViewTruncated => fatal(-32040, ErrorKind::ViewTruncated),
267 Self::OutputUnavailable => fatal(-32041, ErrorKind::OutputUnavailable),
268 Self::Boundary => ErrorCodeDefinition::new(
269 -32042,
270 ErrorKind::BoundaryPruned,
271 &[ErrorKind::BoundaryUnusable, ErrorKind::NoBoundary],
272 Some(false),
273 ),
274 Self::ApprovalNotFound => fatal(-32050, ErrorKind::ApprovalNotFound),
275 Self::ApprovalAlreadyResolved => fatal(-32051, ErrorKind::ApprovalAlreadyResolved),
276 Self::ApprovalChoiceInvalid => fatal(-32052, ErrorKind::ApprovalChoiceInvalid),
277 Self::ApprovalRequirementStale => fatal(-32053, ErrorKind::ApprovalRequirementStale),
278 Self::ApprovalReviewerUnavailable => {
279 fatal(-32054, ErrorKind::ApprovalReviewerUnavailable)
280 }
281 Self::UserInputNotFound => fatal(-32055, ErrorKind::UserInputNotFound),
282 Self::UserInputAlreadySettled => fatal(-32056, ErrorKind::UserInputAlreadySettled),
283 Self::UserInputAnswerInvalid => fatal(-32057, ErrorKind::UserInputAnswerInvalid),
284 Self::MissingAnchor => {
285 ErrorCodeDefinition::experimental(-32060, ErrorKind::MissingAnchor)
286 }
287 Self::PrunedPrefix => {
288 ErrorCodeDefinition::experimental(-32061, ErrorKind::PrunedPrefix)
289 }
290 Self::WrongStream => ErrorCodeDefinition::experimental(-32062, ErrorKind::WrongStream),
291 Self::UnknownStream => {
292 ErrorCodeDefinition::experimental(-32063, ErrorKind::UnknownStream)
293 }
294 Self::UnknownProjection => {
295 ErrorCodeDefinition::experimental(-32064, ErrorKind::UnknownProjection)
296 }
297 Self::AdmissionNotFound => {
298 ErrorCodeDefinition::experimental(-32070, ErrorKind::AdmissionNotFound)
299 }
300 Self::AdmissionAlreadyResolved => {
301 ErrorCodeDefinition::experimental(-32071, ErrorKind::AdmissionAlreadyResolved)
302 }
303 Self::AdmissionChoiceInvalid => {
304 ErrorCodeDefinition::experimental(-32072, ErrorKind::AdmissionChoiceInvalid)
305 }
306 Self::UsageRefreshRateLimited => ErrorCodeDefinition::experimental_with_retry(
307 -32080,
308 ErrorKind::UsageRefreshRateLimited,
309 Some(true),
310 ),
311 Self::UsageRefreshUnavailable => {
312 ErrorCodeDefinition::experimental(-32081, ErrorKind::UsageRefreshUnavailable)
313 }
314 Self::UsageRefreshFailed => {
315 ErrorCodeDefinition::experimental(-32082, ErrorKind::UsageRefreshFailed)
316 }
317 }
318 }
319
320 #[must_use]
322 pub const fn code(self) -> i64 {
323 self.definition().code
324 }
325
326 #[must_use]
328 pub const fn kind(self) -> ErrorKind {
329 self.definition().kind
330 }
331
332 #[must_use]
337 pub const fn override_kinds(self) -> &'static [ErrorKind] {
338 self.definition().override_kinds
339 }
340
341 #[must_use]
344 pub const fn retryable(self) -> Option<bool> {
345 self.definition().retryable
346 }
347
348 #[must_use]
350 pub const fn is_experimental(self) -> bool {
351 self.definition().experimental
352 }
353}