1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use thiserror::Error;

use crate::{event::UnsavedEventError, storage::StorageError};

#[derive(Error, Debug)]
pub enum EventStoreError {
    #[error("No storage is defined")]
    NoStorage,
    #[error("The streamId is invalid")]
    InvalidStreamId,
    #[error(transparent)]
    Storage(StorageError),
    #[error(transparent)]
    EventProcessing(UnsavedEventError),
    #[error("Internal event store error: {0}")]
    InternalEventStoreError(#[source] BoxDynError),
}

impl From<StorageError> for EventStoreError {
    fn from(error: StorageError) -> Self {
        Self::Storage(error)
    }
}

impl std::convert::From<UnsavedEventError> for EventStoreError {
    fn from(e: UnsavedEventError) -> Self {
        Self::EventProcessing(e)
    }
}

pub type BoxDynError = Box<dyn std::error::Error + 'static + Send + Sync>;

#[cfg(feature = "actix-rt")]
impl From<actix::MailboxError> for EventStoreError {
    fn from(error: actix::MailboxError) -> Self {
        Self::InternalEventStoreError(Box::new(error))
    }
}
#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn testing_that_a_mailboxerror_can_be_converted() {
        let err = actix::MailboxError::Closed;

        let _c: EventStoreError = err.into();
    }

    #[test]
    fn testing_that_a_parse_event_error_can_be_converted() {
        let error = serde_json::from_str::<'_, String>("{").unwrap_err();
        let err = UnsavedEventError::SerializeError(error);

        let _: EventStoreError = err.into();
    }

    #[test]
    fn an_event_store_error_can_be_dispayed() {
        let err = EventStoreError::InvalidStreamId;

        assert_eq!("The streamId is invalid", err.to_string());
    }
}