byteor_pipeline_exec/
error.rs

1//! Executor errors.
2
3/// Executor errors.
4#[derive(Debug)]
5pub struct ExecError {
6    msg: &'static str,
7    ctx: Option<String>,
8    source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
9}
10
11impl ExecError {
12    /// Create a new error.
13    pub fn new(msg: &'static str) -> Self {
14        Self {
15            msg,
16            ctx: None,
17            source: None,
18        }
19    }
20
21    /// Create a new error with additional human-readable context.
22    pub fn with_context(msg: &'static str, ctx: String) -> Self {
23        Self {
24            msg,
25            ctx: Some(ctx),
26            source: None,
27        }
28    }
29
30    /// Create a new error with an underlying source error.
31    pub fn with_source<E>(msg: &'static str, source: E) -> Self
32    where
33        E: std::error::Error + Send + Sync + 'static,
34    {
35        Self {
36            msg,
37            ctx: None,
38            source: Some(Box::new(source)),
39        }
40    }
41
42    /// Create a new error with both context and an underlying source error.
43    pub fn with_context_source<E>(msg: &'static str, ctx: String, source: E) -> Self
44    where
45        E: std::error::Error + Send + Sync + 'static,
46    {
47        Self {
48            msg,
49            ctx: Some(ctx),
50            source: Some(Box::new(source)),
51        }
52    }
53
54    /// Error message.
55    pub const fn message(&self) -> &'static str {
56        self.msg
57    }
58}
59
60impl core::fmt::Display for ExecError {
61    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
62        match &self.ctx {
63            Some(ctx) => write!(f, "{}: {}", self.msg, ctx),
64            None => write!(f, "{}", self.msg),
65        }
66    }
67}
68
69impl std::error::Error for ExecError {
70    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71        self.source
72            .as_deref()
73            .map(|e| e as &(dyn std::error::Error + 'static))
74    }
75}