Skip to main content

apcore_toolkit/output/
errors.rs

1// Error types for output writers.
2
3use thiserror::Error;
4
5/// Raised when a writer fails to write an artifact to disk.
6#[derive(Debug, Error)]
7#[error("Failed to write {path}: {cause}")]
8pub struct WriteError {
9    /// The file path that could not be written.
10    pub path: String,
11    /// Description of the underlying error.
12    pub cause: String,
13}
14
15impl WriteError {
16    /// Create a new WriteError.
17    pub fn new(path: String, cause: String) -> Self {
18        Self { path, cause }
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn test_write_error_display() {
28        let err = WriteError::new("/tmp/test.yaml".into(), "permission denied".into());
29        assert_eq!(
30            err.to_string(),
31            "Failed to write /tmp/test.yaml: permission denied"
32        );
33    }
34
35    #[test]
36    fn test_write_error_fields() {
37        let err = WriteError::new("/path".into(), "cause".into());
38        assert_eq!(err.path, "/path");
39        assert_eq!(err.cause, "cause");
40    }
41
42    #[test]
43    fn test_write_error_is_std_error() {
44        let err = WriteError::new("/file".into(), "io error".into());
45        // Verify it implements std::error::Error (source returns None by default)
46        let std_err: &dyn std::error::Error = &err;
47        assert!(std_err.source().is_none());
48    }
49
50    #[test]
51    fn test_write_error_debug_format() {
52        let err = WriteError::new("/tmp/out.yaml".into(), "disk full".into());
53        let debug_str = format!("{err:?}");
54        assert!(debug_str.contains("WriteError"));
55        assert!(debug_str.contains("/tmp/out.yaml"));
56        assert!(debug_str.contains("disk full"));
57    }
58}