1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//! Builder error types
//!
//! This module defines all error types for the Dockerfile builder subsystem,
//! covering parsing, context handling, build execution, and caching operations.
use std::path::PathBuf;
use thiserror::Error;
/// Build-specific errors
#[derive(Debug, Error)]
pub enum BuildError {
/// Dockerfile parsing failed
#[error("Dockerfile parse error at line {line}: {message}")]
DockerfileParse {
/// The underlying parsing error message
message: String,
/// Line number where the error occurred (1-indexed)
line: usize,
},
/// Failed to read build context
#[error("Failed to read build context at '{path}': {source}")]
ContextRead {
/// Path that could not be read
path: PathBuf,
/// Underlying IO error
source: std::io::Error,
},
/// Path escape attempt detected (security violation)
#[error("Path escape attempt: '{path}' escapes build context")]
PathEscape {
/// The offending path
path: PathBuf,
},
/// File was ignored by .dockerignore
#[error("File '{path}' is ignored by .dockerignore")]
FileIgnored {
/// The ignored file path
path: PathBuf,
},
/// Referenced stage not found
#[error("Stage '{name}' not found in Dockerfile")]
StageNotFound {
/// The stage name or index that was referenced
name: String,
},
/// RUN instruction failed
#[error("RUN command failed with exit code {exit_code}: {command}")]
RunFailed {
/// The command that failed
command: String,
/// Exit code returned by the command
exit_code: i32,
},
/// Failed to create layer
#[error("Failed to create layer: {message}")]
LayerCreate {
/// Underlying error description
message: String,
},
/// Cache operation failed
#[error("Cache error: {message}")]
CacheError {
/// Underlying cache error
message: String,
},
/// Registry operation failed
#[error("Registry error: {message}")]
RegistryError {
/// Underlying registry error
message: String,
},
/// IO error
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
/// Variable expansion failed
#[error("Variable expansion failed: {0}")]
VariableExpansion(String),
/// Invalid instruction
#[error("Invalid instruction '{instruction}': {reason}")]
InvalidInstruction {
/// The instruction that was invalid
instruction: String,
/// Reason why it was invalid
reason: String,
},
/// Buildah command execution failed
#[error("Buildah execution failed: {command} (exit code {exit_code}): {stderr}")]
BuildahExecution {
/// The buildah command that failed
command: String,
/// Exit code from buildah
exit_code: i32,
/// Standard error output
stderr: String,
},
/// Build context too large
#[error("Build context too large: {size} bytes (max: {max} bytes)")]
ContextTooLarge {
/// Actual size in bytes
size: u64,
/// Maximum allowed size
max: u64,
},
/// Base image not found
#[error("Base image not found: {image}")]
BaseImageNotFound {
/// The image reference that was not found
image: String,
},
/// Circular dependency in multi-stage build
#[error("Circular dependency detected in multi-stage build: {stages:?}")]
CircularDependency {
/// The stages involved in the cycle
stages: Vec<String>,
},
/// Buildah binary not found or installation failed
#[error("Buildah not found: {message}")]
BuildahNotFound {
/// Details about the failure
message: String,
},
/// `ZImagefile` YAML deserialization failed
#[error("ZImagefile parse error: {message}")]
ZImagefileParse {
/// The underlying YAML parse error message
message: String,
},
/// `ZImagefile` semantic validation failed
#[error("ZImagefile validation error: {message}")]
ZImagefileValidation {
/// Description of what validation rule was violated
message: String,
},
/// Pipeline validation or execution error
#[error("Pipeline error: {message}")]
PipelineError {
/// Description of the pipeline error
message: String,
},
/// WASM build failed
#[error("WASM build error: {0}")]
WasmBuild(#[from] crate::wasm_builder::WasmBuildError),
/// Operation not supported by this backend
#[error("Operation '{operation}' is not supported by this backend")]
NotSupported {
/// The operation that was attempted
operation: String,
},
}
impl BuildError {
/// Create a `DockerfileParse` error from a message and line number
pub fn parse_error(msg: impl Into<String>, line: usize) -> Self {
Self::DockerfileParse {
message: msg.into(),
line,
}
}
/// Create a `ContextRead` error from a path and IO error
pub fn context_read(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
Self::ContextRead {
path: path.into(),
source,
}
}
/// Create a `PathEscape` error
pub fn path_escape(path: impl Into<PathBuf>) -> Self {
Self::PathEscape { path: path.into() }
}
/// Create a `StageNotFound` error
pub fn stage_not_found(name: impl Into<String>) -> Self {
Self::StageNotFound { name: name.into() }
}
/// Create a `RunFailed` error
pub fn run_failed(command: impl Into<String>, exit_code: i32) -> Self {
Self::RunFailed {
command: command.into(),
exit_code,
}
}
/// Create a `LayerCreate` error
pub fn layer_create(msg: impl Into<String>) -> Self {
Self::LayerCreate {
message: msg.into(),
}
}
/// Create a `CacheError`
pub fn cache_error(msg: impl Into<String>) -> Self {
Self::CacheError {
message: msg.into(),
}
}
/// Create a `RegistryError`
pub fn registry_error(msg: impl Into<String>) -> Self {
Self::RegistryError {
message: msg.into(),
}
}
/// Create an `InvalidInstruction` error
pub fn invalid_instruction(instruction: impl Into<String>, reason: impl Into<String>) -> Self {
Self::InvalidInstruction {
instruction: instruction.into(),
reason: reason.into(),
}
}
/// Create a `BuildahExecution` error
pub fn buildah_execution(
command: impl Into<String>,
exit_code: i32,
stderr: impl Into<String>,
) -> Self {
Self::BuildahExecution {
command: command.into(),
exit_code,
stderr: stderr.into(),
}
}
/// Create a `BuildahNotFound` error
pub fn buildah_not_found(message: impl Into<String>) -> Self {
Self::BuildahNotFound {
message: message.into(),
}
}
/// Create a `ZImagefileParse` error
pub fn zimagefile_parse(message: impl Into<String>) -> Self {
Self::ZImagefileParse {
message: message.into(),
}
}
/// Create a `ZImagefileValidation` error
pub fn zimagefile_validation(message: impl Into<String>) -> Self {
Self::ZImagefileValidation {
message: message.into(),
}
}
/// Create a `PipelineError`
pub fn pipeline_error(message: impl Into<String>) -> Self {
Self::PipelineError {
message: message.into(),
}
}
}
/// Result type alias for build operations
pub type Result<T, E = BuildError> = std::result::Result<T, E>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = BuildError::parse_error("unexpected token", 42);
assert!(err.to_string().contains("line 42"));
assert!(err.to_string().contains("unexpected token"));
}
#[test]
fn test_path_escape_error() {
let err = BuildError::path_escape("/etc/passwd");
assert!(err.to_string().contains("/etc/passwd"));
assert!(err.to_string().contains("escape"));
}
#[test]
fn test_run_failed_error() {
let err = BuildError::run_failed("apt-get install foo", 127);
assert!(err.to_string().contains("exit code 127"));
assert!(err.to_string().contains("apt-get install foo"));
}
}