indexdb_es/
error.rs

1// use anyhow::Error;
2use cqrs_es::persist::PersistenceError;
3use cqrs_es::AggregateError;
4use std::fmt::{Debug, Display, Formatter};
5
6#[derive(Debug)]
7pub enum IndexDBAggregateError {
8    OptimisticLock,
9    ConnectionError(String),
10    DeserializationError(String),
11    UnknownError(String),
12}
13
14impl Display for IndexDBAggregateError {
15    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16        match self {
17            IndexDBAggregateError::OptimisticLock => write!(f, "optimistic lock error"),
18            IndexDBAggregateError::UnknownError(error) => write!(f, "{}", error),
19            IndexDBAggregateError::DeserializationError(error) => write!(f, "{}", error),
20            IndexDBAggregateError::ConnectionError(error) => write!(f, "{}", error),
21        }
22    }
23}
24
25impl std::error::Error for IndexDBAggregateError {}
26
27// impl From<DomException> for IndexDBAggregateError {
28//     fn from(err: DomException) -> Self {
29//         println!("error: {:?}", err);
30//         // TODO: improve error handling
31//         match err {
32//             _ => IndexDBAggregateError::UnknownError(err.message()),
33//         }
34//     }
35// }
36
37impl<T: std::error::Error> From<IndexDBAggregateError> for AggregateError<T> {
38    fn from(err: IndexDBAggregateError) -> Self {
39        match err {
40            IndexDBAggregateError::OptimisticLock => AggregateError::AggregateConflict,
41            IndexDBAggregateError::ConnectionError(_) => {
42                AggregateError::DatabaseConnectionError(Box::new(err))
43            }
44            IndexDBAggregateError::DeserializationError(_) => {
45                AggregateError::DeserializationError(Box::new(err))
46            }
47            IndexDBAggregateError::UnknownError(_) => {
48                AggregateError::UnexpectedError(Box::new(err))
49            }
50        }
51    }
52}
53
54impl From<serde_json::Error> for IndexDBAggregateError {
55    fn from(err: serde_json::Error) -> Self {
56        match err.classify() {
57            serde_json::error::Category::Data | serde_json::error::Category::Syntax => {
58                IndexDBAggregateError::DeserializationError(err.to_string())
59            }
60            serde_json::error::Category::Io | serde_json::error::Category::Eof => {
61                IndexDBAggregateError::UnknownError(err.to_string())
62            }
63        }
64    }
65}
66
67impl From<IndexDBAggregateError> for PersistenceError {
68    fn from(err: IndexDBAggregateError) -> Self {
69        match err {
70            IndexDBAggregateError::OptimisticLock => PersistenceError::OptimisticLockError,
71            IndexDBAggregateError::ConnectionError(_) => {
72                PersistenceError::ConnectionError(Box::new(err))
73            }
74            IndexDBAggregateError::DeserializationError(_) => {
75                PersistenceError::UnknownError(Box::new(err))
76            }
77            IndexDBAggregateError::UnknownError(_) => PersistenceError::UnknownError(Box::new(err)),
78        }
79    }
80}