Skip to main content

coralstack_cmd_ipc/
error.rs

1//! Error types for the command registry.
2//!
3//! Mirrors the error hierarchy in
4//! `packages/cmd-ipc/src/registry/command-errors.ts` so the wire-level
5//! error codes stay byte-identical across the Rust and TypeScript
6//! implementations.
7
8use serde::{Deserialize, Serialize};
9
10/// Error codes returned when registering a command fails.
11///
12/// Matches the TypeScript `CommandRegisterErrorCode` enum.
13#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
14#[serde(rename_all = "snake_case")]
15pub enum RegisterErrorCode {
16    DuplicateCommand,
17}
18
19/// Error codes returned when executing a command fails.
20///
21/// Matches the TypeScript `ExecuteCommandResponseErrorCode` enum.
22#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
23#[serde(rename_all = "snake_case")]
24pub enum ExecuteErrorCode {
25    NotFound,
26    InvalidRequest,
27    InternalError,
28    Timeout,
29    ChannelDisconnected,
30}
31
32/// Errors raised by the command registry on the Rust side.
33#[derive(thiserror::Error, Debug)]
34pub enum CommandError {
35    #[error("invalid message: {0}")]
36    InvalidMessage(String),
37
38    #[error("duplicate command registration: {0}")]
39    DuplicateCommand(String),
40
41    #[error("command not found: {0}")]
42    NotFound(String),
43
44    #[error("invalid request for command {command_id}: {message}")]
45    InvalidRequest { command_id: String, message: String },
46
47    #[error("internal error executing command {command_id}: {message}")]
48    Internal { command_id: String, message: String },
49
50    #[error("request timed out")]
51    Timeout,
52
53    #[error("channel disconnected")]
54    ChannelDisconnected,
55
56    #[error("serialization error: {0}")]
57    Serde(#[from] serde_json::Error),
58}
59
60/// Errors raised by a [`CommandChannel`](crate::channel::CommandChannel).
61#[derive(thiserror::Error, Debug)]
62pub enum ChannelError {
63    #[error("channel already closed")]
64    Closed,
65
66    #[error("channel send failed: {0}")]
67    Send(String),
68
69    #[error("channel error: {0}")]
70    Other(String),
71}