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