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