Skip to main content

cuenv_hooks/
error.rs

1//! Error types for the cuenv-hooks crate
2
3use miette::Diagnostic;
4use std::path::PathBuf;
5use thiserror::Error;
6
7/// Main error type for cuenv-hooks operations
8#[derive(Error, Debug, Diagnostic)]
9pub enum Error {
10    /// Configuration error
11    #[error("Configuration error: {message}")]
12    #[diagnostic(code(cuenv_hooks::config::invalid))]
13    Configuration {
14        /// The error message describing the configuration issue
15        message: String,
16    },
17
18    /// I/O error with path context
19    #[error("I/O error during {operation}: {source}")]
20    #[diagnostic(code(cuenv_hooks::io::error))]
21    Io {
22        /// The underlying I/O error
23        #[source]
24        source: std::io::Error,
25        /// The path where the I/O error occurred, if applicable
26        path: Option<Box<std::path::Path>>,
27        /// Description of the operation that failed
28        operation: String,
29    },
30
31    /// Timeout error
32    #[error("Operation timed out after {seconds} seconds")]
33    #[diagnostic(code(cuenv_hooks::timeout))]
34    Timeout {
35        /// The number of seconds before the timeout occurred
36        seconds: u64,
37    },
38
39    /// State not found error
40    #[error("Execution state not found for instance: {instance_id}")]
41    #[diagnostic(code(cuenv_hooks::state::not_found))]
42    StateNotFound {
43        /// The instance ID that was not found
44        instance_id: String,
45    },
46
47    /// Serialization/deserialization error
48    #[error("Serialization error: {message}")]
49    #[diagnostic(code(cuenv_hooks::serialization))]
50    Serialization {
51        /// The error message describing the serialization issue
52        message: String,
53    },
54
55    /// Process execution error
56    #[error("Process execution failed: {message}")]
57    #[diagnostic(code(cuenv_hooks::process))]
58    Process {
59        /// The error message describing the process failure
60        message: String,
61    },
62}
63
64impl Error {
65    /// Create a configuration error with a message
66    pub fn configuration(message: impl Into<String>) -> Self {
67        Self::Configuration {
68            message: message.into(),
69        }
70    }
71
72    /// Create an I/O error with context
73    pub fn io(source: std::io::Error, path: Option<PathBuf>, operation: impl Into<String>) -> Self {
74        Self::Io {
75            source,
76            path: path.map(|p| p.into_boxed_path()),
77            operation: operation.into(),
78        }
79    }
80
81    /// Create a state not found error
82    pub fn state_not_found(instance_id: impl Into<String>) -> Self {
83        Self::StateNotFound {
84            instance_id: instance_id.into(),
85        }
86    }
87
88    /// Create a serialization error
89    pub fn serialization(message: impl Into<String>) -> Self {
90        Self::Serialization {
91            message: message.into(),
92        }
93    }
94
95    /// Create a process execution error
96    pub fn process(message: impl Into<String>) -> Self {
97        Self::Process {
98            message: message.into(),
99        }
100    }
101}
102
103/// Result type for cuenv-hooks operations
104pub type Result<T> = std::result::Result<T, Error>;