Skip to main content

adk_skill/
error.rs

1use std::path::PathBuf;
2
3#[derive(Debug, thiserror::Error)]
4pub enum SkillError {
5    #[error("io error: {0}")]
6    Io(#[from] std::io::Error),
7
8    /// The frontmatter is not valid YAML, or a draft's frontmatter cannot be written as YAML.
9    /// The message is the YAML library's, so this type does not depend on that library.
10    #[error("yaml error: {message}. Check the frontmatter between the `---` delimiters")]
11    Yaml { message: String },
12
13    #[error("invalid skill frontmatter in {path}: {message}")]
14    InvalidFrontmatter { path: PathBuf, message: String },
15
16    #[error("missing required field `{field}` in {path}")]
17    MissingField { path: PathBuf, field: &'static str },
18
19    #[error("invalid skills root, expected directory: {0}")]
20    InvalidSkillsRoot(PathBuf),
21
22    #[error("skill validation error: {0}")]
23    Validation(String),
24
25    #[error("index error: {0}")]
26    IndexError(String),
27
28    // ===== Skill Registry payloads (feature `vertex-skill-registry`) =====
29    /// A Skill Registry request failed; the inner error carries the
30    /// `skill.registry.*` code and category.
31    #[cfg(feature = "vertex-skill-registry")]
32    #[error("skill registry request failed: {0}")]
33    Registry(Box<adk_core::AdkError>),
34
35    /// The `zippedFilesystem` field was not valid standard base64.
36    #[cfg(feature = "vertex-skill-registry")]
37    #[error(
38        "skill registry payload is not valid base64: {message}. The zippedFilesystem field must be standard base64; re-fetch the skill or report the registry entry"
39    )]
40    RegistryPayloadDecode { message: String },
41
42    /// The decoded zip did not match the registry's `sha256` field.
43    #[cfg(feature = "vertex-skill-registry")]
44    #[error(
45        "skill registry payload sha256 mismatch: expected {expected}, computed {actual}. The payload may be corrupt or tampered with; re-fetch the skill before using its contents"
46    )]
47    RegistryChecksumMismatch { expected: String, actual: String },
48
49    /// The payload is not a well-formed zip archive.
50    #[cfg(feature = "vertex-skill-registry")]
51    #[error(
52        "skill archive is not a valid zip: {message}. The registry serves SKILL.md packages as zip archives; re-fetch the skill or report the registry entry"
53    )]
54    ArchiveFormat { message: String },
55
56    /// The archive exceeds the maximum accepted size.
57    #[cfg(feature = "vertex-skill-registry")]
58    #[error(
59        "skill archive is {size} bytes, exceeding the {limit}-byte limit. The registry rejects archives over this size; do not extract oversized payloads"
60    )]
61    ArchiveTooLarge { size: u64, limit: u64 },
62
63    /// The archive contains more entries than allowed.
64    #[cfg(feature = "vertex-skill-registry")]
65    #[error(
66        "skill archive contains {count} entries, exceeding the limit of {limit}. Split the skill into smaller packages or remove unneeded files"
67    )]
68    ArchiveTooManyEntries { count: usize, limit: usize },
69
70    /// An entry path contains a `..` component.
71    #[cfg(feature = "vertex-skill-registry")]
72    #[error(
73        "skill archive entry `{name}` contains a `..` path component. Path traversal is rejected; all entries must resolve inside the extraction root"
74    )]
75    ArchivePathTraversal { name: String },
76
77    /// An entry path is absolute.
78    #[cfg(feature = "vertex-skill-registry")]
79    #[error(
80        "skill archive entry `{name}` is an absolute path. Entries must be relative so extraction stays inside the target directory"
81    )]
82    ArchiveAbsolutePath { name: String },
83
84    /// An entry is a symbolic link.
85    #[cfg(feature = "vertex-skill-registry")]
86    #[error(
87        "skill archive entry `{name}` is a symlink. Symlinks are rejected because they can escape the extraction root"
88    )]
89    ArchiveSymlink { name: String },
90
91    /// Two entries share the same normalized name.
92    #[cfg(feature = "vertex-skill-registry")]
93    #[error(
94        "skill archive contains duplicate entry `{name}`. Duplicate names are rejected because later entries would silently overwrite earlier ones"
95    )]
96    ArchiveDuplicateEntry { name: String },
97
98    /// The declared uncompressed total exceeds the limit.
99    #[cfg(feature = "vertex-skill-registry")]
100    #[error(
101        "skill archive declares {total} uncompressed bytes, exceeding the {limit}-byte limit. Refusing to extract to avoid resource exhaustion"
102    )]
103    ArchiveUncompressedTooLarge { total: u64, limit: u64 },
104
105    /// An entry's compression ratio exceeds the limit (zip-bomb defense).
106    #[cfg(feature = "vertex-skill-registry")]
107    #[error(
108        "skill archive entry `{name}` has compression ratio {ratio}, exceeding the limit of {limit}. Highly compressed entries are rejected as potential zip bombs"
109    )]
110    ArchiveCompressionRatio { name: String, ratio: u64, limit: u64 },
111
112    /// An entry nests deeper than the allowed directory depth.
113    #[cfg(feature = "vertex-skill-registry")]
114    #[error(
115        "skill archive entry `{name}` nests {depth} directory levels, exceeding the limit of {limit}. Flatten the skill's directory layout"
116    )]
117    ArchiveDepthExceeded { name: String, depth: usize, limit: usize },
118}
119
120pub type SkillResult<T> = Result<T, SkillError>;
121
122#[cfg(feature = "vertex-skill-registry")]
123impl From<adk_core::AdkError> for SkillError {
124    fn from(err: adk_core::AdkError) -> Self {
125        SkillError::Registry(Box::new(err))
126    }
127}
128
129impl From<SkillError> for adk_core::AdkError {
130    fn from(err: SkillError) -> Self {
131        use adk_core::{ErrorCategory, ErrorComponent};
132        // Registry errors already carry their component, category, and
133        // `skill.registry.*` code; pass them through unchanged.
134        #[cfg(feature = "vertex-skill-registry")]
135        let err = match err {
136            SkillError::Registry(inner) => return *inner,
137            other => other,
138        };
139        let (category, code) = match &err {
140            SkillError::Io(_) => (ErrorCategory::Internal, "skill.io"),
141            SkillError::Yaml { .. } => (ErrorCategory::InvalidInput, "skill.yaml_parse"),
142            SkillError::InvalidFrontmatter { .. } => {
143                (ErrorCategory::InvalidInput, "skill.invalid_frontmatter")
144            }
145            SkillError::MissingField { .. } => (ErrorCategory::InvalidInput, "skill.missing_field"),
146            SkillError::InvalidSkillsRoot(_) => {
147                (ErrorCategory::NotFound, "skill.invalid_skills_root")
148            }
149            SkillError::Validation(_) => (ErrorCategory::InvalidInput, "skill.validation"),
150            SkillError::IndexError(_) => (ErrorCategory::Internal, "skill.index"),
151            #[cfg(feature = "vertex-skill-registry")]
152            SkillError::Registry(_) => unreachable!("Registry errors return early above"),
153            // Decode and checksum failures are corrupt upstream payloads;
154            // archive-rule violations are rejected input data.
155            #[cfg(feature = "vertex-skill-registry")]
156            SkillError::RegistryPayloadDecode { .. } => {
157                (ErrorCategory::Internal, "skill.registry.payload_decode")
158            }
159            #[cfg(feature = "vertex-skill-registry")]
160            SkillError::RegistryChecksumMismatch { .. } => {
161                (ErrorCategory::Internal, "skill.registry.checksum_mismatch")
162            }
163            #[cfg(feature = "vertex-skill-registry")]
164            SkillError::ArchiveFormat { .. } => {
165                (ErrorCategory::InvalidInput, "skill.archive.invalid_format")
166            }
167            #[cfg(feature = "vertex-skill-registry")]
168            SkillError::ArchiveTooLarge { .. } => {
169                (ErrorCategory::InvalidInput, "skill.archive.too_large")
170            }
171            #[cfg(feature = "vertex-skill-registry")]
172            SkillError::ArchiveTooManyEntries { .. } => {
173                (ErrorCategory::InvalidInput, "skill.archive.too_many_entries")
174            }
175            #[cfg(feature = "vertex-skill-registry")]
176            SkillError::ArchivePathTraversal { .. } => {
177                (ErrorCategory::InvalidInput, "skill.archive.path_traversal")
178            }
179            #[cfg(feature = "vertex-skill-registry")]
180            SkillError::ArchiveAbsolutePath { .. } => {
181                (ErrorCategory::InvalidInput, "skill.archive.absolute_path")
182            }
183            #[cfg(feature = "vertex-skill-registry")]
184            SkillError::ArchiveSymlink { .. } => {
185                (ErrorCategory::InvalidInput, "skill.archive.symlink")
186            }
187            #[cfg(feature = "vertex-skill-registry")]
188            SkillError::ArchiveDuplicateEntry { .. } => {
189                (ErrorCategory::InvalidInput, "skill.archive.duplicate_entry")
190            }
191            #[cfg(feature = "vertex-skill-registry")]
192            SkillError::ArchiveUncompressedTooLarge { .. } => {
193                (ErrorCategory::InvalidInput, "skill.archive.uncompressed_too_large")
194            }
195            #[cfg(feature = "vertex-skill-registry")]
196            SkillError::ArchiveCompressionRatio { .. } => {
197                (ErrorCategory::InvalidInput, "skill.archive.compression_ratio")
198            }
199            #[cfg(feature = "vertex-skill-registry")]
200            SkillError::ArchiveDepthExceeded { .. } => {
201                (ErrorCategory::InvalidInput, "skill.archive.depth_exceeded")
202            }
203        };
204        adk_core::AdkError::new(ErrorComponent::Tool, category, code, err.to_string())
205            .with_source(err)
206    }
207}