icydb_core/
runtime_error.rs

1use crate::ThisError;
2use std::fmt;
3
4///
5/// ErrorClass
6/// Internal error taxonomy for runtime classification.
7/// Not a stable API; may change without notice.
8///
9
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum ErrorClass {
12    Corruption,
13    Internal,
14    Conflict,
15    Unsupported,
16    InvariantViolation,
17}
18
19impl fmt::Display for ErrorClass {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        let label = match self {
22            Self::Corruption => "corruption",
23            Self::Internal => "internal",
24            Self::Conflict => "conflict",
25            Self::Unsupported => "unsupported",
26            Self::InvariantViolation => "invariant_violation",
27        };
28        write!(f, "{label}")
29    }
30}
31
32///
33/// ErrorOrigin
34/// Internal origin taxonomy for runtime classification.
35/// Not a stable API; may change without notice.
36///
37
38#[derive(Clone, Copy, Debug, Eq, PartialEq)]
39pub enum ErrorOrigin {
40    Serialize,
41    Store,
42    Index,
43    Query,
44    Response,
45    Executor,
46    Interface,
47}
48
49impl fmt::Display for ErrorOrigin {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        let label = match self {
52            Self::Serialize => "serialize",
53            Self::Store => "store",
54            Self::Index => "index",
55            Self::Query => "query",
56            Self::Response => "response",
57            Self::Executor => "executor",
58            Self::Interface => "interface",
59        };
60        write!(f, "{label}")
61    }
62}
63
64///
65/// RuntimeError
66/// Structured runtime error with a stable internal classification.
67/// Not a stable API; intended for internal use and may change without notice.
68///
69
70#[derive(Debug, ThisError)]
71#[error("{message}")]
72pub struct RuntimeError {
73    pub class: ErrorClass,
74    pub origin: ErrorOrigin,
75    pub message: String,
76}
77
78impl RuntimeError {
79    pub fn new(class: ErrorClass, origin: ErrorOrigin, message: impl Into<String>) -> Self {
80        Self {
81            class,
82            origin,
83            message: message.into(),
84        }
85    }
86
87    #[must_use]
88    pub fn display_with_class(&self) -> String {
89        format!("{}:{}: {}", self.origin, self.class, self.message)
90    }
91}