Skip to main content

alien_commands/
error.rs

1use alien_error::{AlienError, AlienErrorData};
2use serde::{Deserialize, Serialize};
3
4/// Errors that occur in the commands protocol.
5#[derive(Debug, Clone, AlienErrorData, Serialize, Deserialize)]
6#[serde(rename_all = "camelCase")]
7pub enum ErrorData {
8    /// The receiver's authority expired before delegated credentials could be issued.
9    #[error(
10        code = "COMMAND_AUTHORITY_EXPIRED",
11        message = "Command receiver authority expired",
12        retryable = "false",
13        internal = "false",
14        http_status_code = 401
15    )]
16    CommandAuthorityExpired,
17
18    /// Command validation failed or contains invalid data.
19    #[error(
20        code = "INVALID_COMMAND",
21        message = "Invalid command: {message}",
22        retryable = "false",
23        internal = "false",
24        http_status_code = 400
25    )]
26    InvalidCommand {
27        /// Human-readable description of what makes the command invalid
28        message: String,
29    },
30
31    /// Requested command ID was not found.
32    #[error(
33        code = "COMMAND_NOT_FOUND",
34        message = "Command '{command_id}' not found",
35        retryable = "false",
36        internal = "false",
37        http_status_code = 404
38    )]
39    CommandNotFound {
40        /// ID of the command that was not found
41        command_id: String,
42    },
43
44    /// The manager serving a hosted command capability no longer owns its
45    /// deployment. Clients should refresh bootstrap discovery and retry.
46    #[error(
47        code = "COMMAND_CONNECTION_STALE",
48        message = "Command connection is stale for '{resource}'",
49        retryable = "true",
50        internal = "false",
51        http_status_code = 401
52    )]
53    CommandConnectionStale {
54        /// Deployment or command identifier whose manager assignment changed
55        resource: String,
56    },
57
58    /// Explicitly requested command target does not exist in the deployment
59    /// (or is not command-capable). Also returned for an empty resource id.
60    ///
61    /// 404 mirrors the crate's other lookup failures (COMMAND_NOT_FOUND,
62    /// LEASE_NOT_FOUND): the named resource does not exist.
63    #[error(
64        code = "COMMAND_TARGET_NOT_FOUND",
65        message = "Command target '{resource_id}' not found in deployment '{deployment_id}'",
66        retryable = "false",
67        internal = "false",
68        http_status_code = 404
69    )]
70    CommandTargetNotFound {
71        /// Resource ID that was requested but not found
72        resource_id: String,
73        /// Deployment the target was looked up in
74        deployment_id: String,
75    },
76
77    /// Single-target shorthand was used but the deployment has multiple
78    /// command-capable targets.
79    ///
80    /// 409 mirrors CONFLICT: the request conflicts with the deployment's
81    /// current state and the client resolves it by naming a target.
82    #[error(
83        code = "COMMAND_TARGET_AMBIGUOUS",
84        message = "Deployment '{deployment_id}' has more than one command-capable target {target_resource_ids:?}; name one with targetResourceId",
85        retryable = "false",
86        internal = "false",
87        http_status_code = 409
88    )]
89    CommandTargetAmbiguous {
90        /// Deployment with more than one command-capable target
91        deployment_id: String,
92        /// Resource ids the caller can name as the target, sorted
93        target_resource_ids: Vec<String>,
94    },
95
96    /// The deployment has no command-capable targets at all.
97    ///
98    /// 422: the request is well-formed but unsatisfiable for this deployment
99    /// (no resource has commands enabled) — unlike 400 (malformed) or 404
100    /// (a specific named thing missing).
101    #[error(
102        code = "NO_COMMAND_TARGETS",
103        message = "Deployment '{deployment_id}' has no command-capable targets",
104        retryable = "false",
105        internal = "false",
106        http_status_code = 422
107    )]
108    NoCommandTargets {
109        /// Deployment without any command-capable targets
110        deployment_id: String,
111    },
112
113    /// A command target resource id contains `:`, which would break the
114    /// pending-index and idempotency-key delimiter grammar.
115    ///
116    /// 400: the request (or a stored target) is malformed. The commands layer
117    /// permits every resource-id character except `:`, which delimits key
118    /// segments.
119    #[error(
120        code = "COMMAND_TARGET_ID_INVALID",
121        message = "Command target id '{resource_id}' is invalid: ids cannot contain ':'",
122        retryable = "false",
123        internal = "false",
124        http_status_code = 400
125    )]
126    CommandTargetIdInvalid {
127        /// The offending resource id
128        resource_id: String,
129    },
130
131    /// Pull command receiver configuration from the environment is missing
132    /// or invalid (e.g. a required `ALIEN_COMMANDS_*` variable is absent).
133    #[error(
134        code = "COMMAND_RECEIVER_CONFIG_INVALID",
135        message = "Command receiver configuration invalid: {message}",
136        retryable = "false",
137        internal = "false",
138        http_status_code = 400
139    )]
140    CommandReceiverConfigInvalid {
141        /// Human-readable description of what is missing or invalid
142        message: String,
143        /// Environment variable that is missing or invalid
144        env_var: String,
145    },
146
147    /// The command receiver bearer token was rejected by the commands API.
148    #[error(
149        code = "COMMAND_RECEIVER_UNAUTHORIZED",
150        message = "Command receiver authorization failed during {operation}",
151        retryable = "false",
152        internal = "false",
153        http_status_code = 401
154    )]
155    CommandReceiverUnauthorized {
156        /// Command API operation that was rejected
157        operation: String,
158        /// Commands API URL that rejected the token
159        url: String,
160    },
161
162    /// The command receiver request was permanently rejected by the commands API.
163    #[error(
164        code = "COMMAND_RECEIVER_REQUEST_REJECTED",
165        message = "Command receiver request was rejected with HTTP {status} during {operation}",
166        retryable = "false",
167        internal = "false",
168        http_status_code = 502
169    )]
170    CommandReceiverRequestRejected {
171        /// Command API operation that was rejected
172        operation: String,
173        /// HTTP status returned by the commands API
174        status: u16,
175        /// Commands API URL that rejected the request
176        url: String,
177    },
178
179    /// Invalid state transition attempted on command.
180    #[error(
181        code = "INVALID_STATE_TRANSITION",
182        message = "Invalid state transition from '{from}' to '{to}'",
183        retryable = "false",
184        internal = "false",
185        http_status_code = 400
186    )]
187    InvalidStateTransition {
188        /// Current state of the command
189        from: String,
190        /// Attempted new state
191        to: String,
192    },
193
194    /// A response came from a lease that another lease has replaced.
195    ///
196    /// 409: the command is held by a newer lease, and the receiver treats the
197    /// conflict as the command no longer being its own.
198    #[error(
199        code = "COMMAND_LEASE_SUPERSEDED",
200        message = "Lease '{lease_id}' no longer holds command '{command_id}'",
201        retryable = "false",
202        internal = "false",
203        http_status_code = 409
204    )]
205    LeaseSuperseded {
206        /// ID of the command
207        command_id: String,
208        /// ID of the lease that no longer holds it
209        lease_id: String,
210    },
211
212    /// Command has expired and can no longer be processed.
213    #[error(
214        code = "COMMAND_EXPIRED",
215        message = "Command '{command_id}' has expired",
216        retryable = "false",
217        internal = "false",
218        http_status_code = 410
219    )]
220    CommandExpired {
221        /// ID of the expired command
222        command_id: String,
223    },
224
225    /// Storage backend operation failed.
226    #[error(
227        code = "STORAGE_OPERATION_FAILED",
228        message = "Storage operation failed: {message}",
229        retryable = "true",
230        internal = "false"
231    )]
232    StorageOperationFailed {
233        /// Human-readable description of the storage failure
234        message: String,
235        /// Storage operation type (upload, download, etc.)
236        operation: Option<String>,
237        /// Storage path or URL that failed
238        path: Option<String>,
239    },
240
241    /// Key-value store operation failed.
242    #[error(
243        code = "KV_OPERATION_FAILED",
244        message = "KV operation '{operation}' failed on key '{key}': {message}",
245        retryable = "true",
246        internal = "false"
247    )]
248    KvOperationFailed {
249        /// Type of KV operation that failed
250        operation: String,
251        /// Key that was being operated on
252        key: String,
253        /// Human-readable description of the failure
254        message: String,
255    },
256
257    /// Transport dispatch to agent failed.
258    #[error(
259        code = "TRANSPORT_DISPATCH_FAILED",
260        message = "Transport dispatch failed: {message}",
261        retryable = "true",
262        internal = "false"
263    )]
264    TransportDispatchFailed {
265        /// Human-readable description of the dispatch failure
266        message: String,
267        /// Transport type that failed
268        transport_type: Option<String>,
269        /// Target endpoint or agent identifier
270        target: Option<String>,
271    },
272
273    /// Transport rejected a command before accepting it for execution.
274    ///
275    /// Unlike an acknowledgement timeout, this is a definite non-delivery:
276    /// the command server may safely record a terminal delivery failure.
277    #[error(
278        code = "TRANSPORT_DISPATCH_REJECTED",
279        message = "Transport rejected dispatch: {message}",
280        retryable = "false",
281        internal = "false"
282    )]
283    TransportDispatchRejected {
284        /// Safe description that contains no endpoint credentials or body.
285        message: String,
286        /// Transport type that rejected the command.
287        transport_type: Option<String>,
288        /// Command identifier (never a credential-bearing URL).
289        target: Option<String>,
290    },
291
292    /// Command envelope validation or parsing failed.
293    #[error(
294        code = "INVALID_ENVELOPE",
295        message = "Invalid command envelope: {message}",
296        retryable = "false",
297        internal = "false",
298        http_status_code = 400
299    )]
300    InvalidEnvelope {
301        /// Human-readable description of the envelope issue
302        message: String,
303        /// Envelope field that caused the validation failure
304        field: Option<String>,
305    },
306
307    /// Agent reported an error during command processing.
308    #[error(
309        code = "AGENT_ERROR",
310        message = "Agent error: {message}",
311        retryable = "true",
312        internal = "false"
313    )]
314    AgentError {
315        /// Human-readable description of the agent error
316        message: String,
317        /// Agent identifier if available
318        deployment_id: Option<String>,
319    },
320
321    /// Serialization or deserialization operation failed.
322    #[error(
323        code = "SERIALIZATION_FAILED",
324        message = "Serialization failed: {message}",
325        retryable = "false",
326        internal = "true"
327    )]
328    SerializationFailed {
329        /// Human-readable description of the serialization failure
330        message: String,
331        /// Data type that failed to serialize/deserialize
332        data_type: Option<String>,
333    },
334
335    /// HTTP operation failed during command processing.
336    #[error(
337        code = "HTTP_OPERATION_FAILED",
338        message = "HTTP operation failed: {message}",
339        retryable = "true",
340        internal = "false"
341    )]
342    HttpOperationFailed {
343        /// Human-readable description of the HTTP failure
344        message: String,
345        /// HTTP method if available
346        method: Option<String>,
347        /// URL that failed if available
348        url: Option<String>,
349    },
350
351    /// Operation is not supported by the current configuration.
352    #[error(
353        code = "OPERATION_NOT_SUPPORTED",
354        message = "Operation not supported: {message}",
355        retryable = "false",
356        internal = "false",
357        http_status_code = 501
358    )]
359    OperationNotSupported {
360        /// Human-readable description of what operation is not supported
361        message: String,
362        /// Operation type that was attempted
363        operation: Option<String>,
364    },
365
366    /// Resource conflict detected (e.g., concurrent modification).
367    #[error(
368        code = "CONFLICT",
369        message = "Conflict: {message}",
370        retryable = "true",
371        internal = "false",
372        http_status_code = 409
373    )]
374    Conflict {
375        /// Human-readable description of the conflict
376        message: String,
377        /// Resource identifier that has the conflict
378        resource_id: Option<String>,
379    },
380
381    /// Requested lease ID was not found.
382    #[error(
383        code = "LEASE_NOT_FOUND",
384        message = "Lease '{lease_id}' not found",
385        retryable = "false",
386        internal = "false",
387        http_status_code = 404
388    )]
389    LeaseNotFound {
390        /// ID of the lease that was not found
391        lease_id: String,
392    },
393
394    /// Generic commands error for uncommon cases.
395    #[error(
396        code = "COMMANDS_ERROR",
397        message = "Commands protocol error: {message}",
398        retryable = "true",
399        internal = "true"
400    )]
401    Other {
402        /// Human-readable description of the error
403        message: String,
404    },
405}
406
407pub type Error = AlienError<ErrorData>;
408pub type Result<T> = alien_error::Result<T, ErrorData>;