unified_agent_sdk/
error.rs1use std::fmt;
8
9use thiserror::Error;
10
11pub type Result<T> = std::result::Result<T, ExecutorError>;
13
14#[derive(Error, Debug)]
19pub enum ExecutorError {
20 #[error("IO error: {0}")]
22 Io(#[from] std::io::Error),
23
24 #[error("Process spawn failed: {0}")]
26 SpawnFailed(String),
27
28 #[error("Process execution failed: {0}")]
30 ExecutionFailed(String),
31
32 #[error("Session not found: {0}")]
34 SessionNotFound(String),
35
36 #[error("Invalid configuration: {0}")]
38 InvalidConfig(String),
39
40 #[error("Executor unavailable: {0}")]
42 Unavailable(String),
43
44 #[error("Serialization error: {0}")]
46 Serialization(#[from] serde_json::Error),
47
48 #[error("{0}")]
50 Other(String),
51}
52
53impl ExecutorError {
54 pub fn error_type(&self) -> &'static str {
59 match self {
60 ExecutorError::Io(_) => "io",
61 ExecutorError::SpawnFailed(_) => "spawn_failed",
62 ExecutorError::ExecutionFailed(_) => "execution_failed",
63 ExecutorError::SessionNotFound(_) => "session_not_found",
64 ExecutorError::InvalidConfig(_) => "invalid_config",
65 ExecutorError::Unavailable(_) => "unavailable",
66 ExecutorError::Serialization(_) => "serialization",
67 ExecutorError::Other(_) => "other",
68 }
69 }
70
71 pub fn spawn_failed(context: impl AsRef<str>, error: impl fmt::Display) -> Self {
73 Self::SpawnFailed(format_with_context(context.as_ref(), error))
74 }
75
76 pub fn execution_failed(context: impl AsRef<str>, error: impl fmt::Display) -> Self {
78 Self::ExecutionFailed(format_with_context(context.as_ref(), error))
79 }
80
81 pub fn invalid_config(context: impl AsRef<str>, error: impl fmt::Display) -> Self {
83 Self::InvalidConfig(format_with_context(context.as_ref(), error))
84 }
85
86 pub fn unavailable(context: impl AsRef<str>, error: impl fmt::Display) -> Self {
88 Self::Unavailable(format_with_context(context.as_ref(), error))
89 }
90
91 pub fn other(context: impl AsRef<str>, error: impl fmt::Display) -> Self {
93 Self::Other(format_with_context(context.as_ref(), error))
94 }
95}
96
97fn format_with_context(context: &str, error: impl fmt::Display) -> String {
98 if context.is_empty() {
99 error.to_string()
100 } else {
101 format!("{context}: {error}")
102 }
103}