Skip to main content

tidepool_effect/
error.rs

1use tidepool_bridge::BridgeError;
2use tidepool_eval::error::EvalError;
3
4#[derive(Debug)]
5pub enum EffectError {
6    Eval(EvalError),
7    Bridge(BridgeError),
8    UnhandledEffect {
9        tag: u64,
10    },
11    /// A required constructor was not found in the DataConTable.
12    MissingConstructor {
13        name: &'static str,
14    },
15    /// A constructor had the wrong number of fields.
16    FieldCountMismatch {
17        constructor: &'static str,
18        expected: usize,
19        got: usize,
20    },
21    /// Encountered an unexpected value shape during dispatch.
22    UnexpectedValue {
23        context: &'static str,
24        got: String,
25    },
26    /// An effect handler encountered a runtime error.
27    Handler(String),
28}
29
30impl From<EvalError> for EffectError {
31    fn from(e: EvalError) -> Self {
32        EffectError::Eval(e)
33    }
34}
35
36impl From<BridgeError> for EffectError {
37    fn from(e: BridgeError) -> Self {
38        EffectError::Bridge(e)
39    }
40}
41
42impl std::fmt::Display for EffectError {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        match self {
45            EffectError::Eval(e) => write!(f, "Eval error: {}", e),
46            EffectError::Bridge(e) => write!(f, "Bridge error: {}", e),
47            EffectError::UnhandledEffect { tag } => write!(f, "Unhandled effect at tag {}", tag),
48            EffectError::MissingConstructor { name } => {
49                write!(f, "{} constructor not found in DataConTable", name)
50            }
51            EffectError::FieldCountMismatch {
52                constructor,
53                expected,
54                got,
55            } => {
56                write!(f, "{} expects {} fields, got {}", constructor, expected, got)
57            }
58            EffectError::UnexpectedValue { context, got } => {
59                write!(f, "expected {}, got {}", context, got)
60            }
61            EffectError::Handler(msg) => write!(f, "handler error: {}", msg),
62        }
63    }
64}
65
66impl std::error::Error for EffectError {}