Skip to main content

teaql_runtime/
error.rs

1use teaql_core::EntityError;
2use teaql_sql::SqlCompileError;
3
4use crate::CheckResult;
5
6#[derive(Debug)]
7pub enum RuntimeError {
8    MissingEntity(String),
9    SqlCompile(SqlCompileError),
10    Behavior(String),
11    Event(String),
12    Check(Vec<CheckResult>),
13    Graph(String),
14    IdGeneration(String),
15    Language(String),
16    Schema(String),
17    MissingRelation { entity: String, relation: String },
18    OptimisticLockConflict { entity: String, id: String },
19}
20
21impl std::fmt::Display for RuntimeError {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Self::MissingEntity(entity) => write!(f, "missing entity descriptor: {entity}"),
25            Self::SqlCompile(err) => err.fmt(f),
26            Self::Behavior(message) => write!(f, "repository behavior error: {message}"),
27            Self::Event(message) => write!(f, "entity event error: {message}"),
28            Self::Check(results) => {
29                let messages = results
30                    .iter()
31                    .map(ToString::to_string)
32                    .collect::<Vec<_>>()
33                    .join("; ");
34                write!(f, "check failed: {messages}")
35            }
36            Self::Graph(message) => write!(f, "graph write error: {message}"),
37            Self::IdGeneration(message) => write!(f, "id generation error: {message}"),
38            Self::Language(message) => write!(f, "language error: {message}"),
39            Self::Schema(message) => write!(f, "schema provider error: {message}"),
40            Self::MissingRelation { entity, relation } => {
41                write!(f, "missing relation {relation} on entity {entity}")
42            }
43            Self::OptimisticLockConflict { entity, id } => {
44                write!(f, "optimistic lock conflict on {entity}({id})")
45            }
46        }
47    }
48}
49
50impl std::error::Error for RuntimeError {}
51
52impl From<SqlCompileError> for RuntimeError {
53    fn from(value: SqlCompileError) -> Self {
54        Self::SqlCompile(value)
55    }
56}
57
58#[derive(Debug)]
59pub enum ContextError {
60    MissingResource(String),
61    MissingTypedResource(&'static str),
62    MissingRepository(String),
63}
64
65impl std::fmt::Display for ContextError {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        match self {
68            Self::MissingResource(name) => write!(f, "missing named resource: {name}"),
69            Self::MissingTypedResource(name) => write!(f, "missing typed resource: {name}"),
70            Self::MissingRepository(name) => write!(f, "missing repository for entity: {name}"),
71        }
72    }
73}
74
75impl std::error::Error for ContextError {}
76
77#[derive(Debug)]
78pub enum RepositoryError<ExecError> {
79    Runtime(RuntimeError),
80    Entity(EntityError),
81    Executor(ExecError),
82}
83
84impl<ExecError> std::fmt::Display for RepositoryError<ExecError>
85where
86    ExecError: std::fmt::Display,
87{
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        match self {
90            Self::Runtime(err) => err.fmt(f),
91            Self::Entity(err) => err.fmt(f),
92            Self::Executor(err) => err.fmt(f),
93        }
94    }
95}
96
97impl<ExecError> std::error::Error for RepositoryError<ExecError> where
98    ExecError: std::error::Error + 'static
99{
100}