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, "entity data service 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    MissingEntityDataService(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::MissingEntityDataService(name) => {
73                write!(f, "missing entity data service for entity: {name}")
74            }
75        }
76    }
77}
78
79impl std::error::Error for ContextError {}
80
81#[derive(Debug)]
82pub enum DataServiceError<ExecError> {
83    Runtime(RuntimeError),
84    Entity(EntityError),
85    Executor(ExecError),
86}
87
88impl<ExecError> std::fmt::Display for DataServiceError<ExecError>
89where
90    ExecError: std::fmt::Display,
91{
92    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        match self {
94            Self::Runtime(err) => err.fmt(f),
95            Self::Entity(err) => err.fmt(f),
96            Self::Executor(err) => err.fmt(f),
97        }
98    }
99}
100
101impl<ExecError> std::error::Error for DataServiceError<ExecError> where
102    ExecError: std::error::Error + 'static
103{
104}