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