claude_agent_sdk/skills/
error.rs

1//! Error types for the Skills system
2
3use std::fmt;
4use thiserror::Error;
5
6/// Errors that can occur in the Skills system
7#[derive(Error, Debug, Clone)]
8pub enum SkillError {
9    /// Validation error
10    #[error("Skill validation failed: {0}")]
11    Validation(String),
12
13    /// Execution error
14    #[error("Skill execution failed: {0}")]
15    Execution(String),
16
17    /// IO error
18    #[error("IO error: {0}")]
19    Io(String),
20
21    /// Serialization error
22    #[error("Serialization error: {0}")]
23    Serialization(String),
24
25    /// Skill not found
26    #[error("Skill not found: {0}")]
27    NotFound(String),
28
29    /// Skill already exists
30    #[error("Skill already exists: {0}")]
31    AlreadyExists(String),
32
33    /// Invalid skill metadata
34    #[error("Invalid skill metadata: {0}")]
35    InvalidMetadata(String),
36
37    /// Skill version conflict
38    #[error("Skill version conflict: {0}")]
39    VersionConflict(String),
40
41    /// Configuration error
42    #[error("Configuration error: {0}")]
43    Configuration(String),
44}
45
46/// Result type for Skill operations
47pub type Result<T> = std::result::Result<T, SkillError>;
48
49/// Result of a Skill execution
50#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
51pub struct SkillOutput {
52    /// Whether execution was successful
53    pub success: bool,
54
55    /// Output data
56    pub data: serde_json::Value,
57
58    /// Error message if failed
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub error: Option<String>,
61
62    /// Additional metadata
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub metadata: Option<serde_json::Value>,
65}
66
67impl SkillOutput {
68    /// Create a successful output
69    pub fn ok(data: impl Into<serde_json::Value>) -> Self {
70        SkillOutput {
71            success: true,
72            data: data.into(),
73            error: None,
74            metadata: None,
75        }
76    }
77
78    /// Create a failed output
79    pub fn err(error: impl Into<String>) -> Self {
80        SkillOutput {
81            success: false,
82            data: serde_json::Value::Null,
83            error: Some(error.into()),
84            metadata: None,
85        }
86    }
87
88    /// Add metadata to the output
89    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
90        self.metadata = Some(metadata);
91        self
92    }
93}
94
95impl fmt::Display for SkillOutput {
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        if self.success {
98            write!(f, "Success: {}", self.data)
99        } else {
100            write!(
101                f,
102                "Error: {}",
103                self.error.as_deref().unwrap_or("Unknown error")
104            )
105        }
106    }
107}
108
109/// Convenience type for Skill execution results
110pub type SkillResult = Result<SkillOutput>;