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