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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use failure::{Error, Fail};
use std::sync::Arc;

use tokio::sync::mpsc::error::SendError as MpscSendError;
use tokio::sync::oneshot::error::RecvError as OneShotRecvError;

#[derive(Debug, Clone, Fail)]
pub enum ShardError {
    #[fail(display = "shard is at capacity, and is unable to evict any records")]
    ShardAtCapacity,

    #[fail(display = "data returned from upstream is immediately expired.")]
    DataImmediatelyExpired,

    #[fail(display = "upstream error: {:?}", error)]
    UpstreamError { error: UpstreamError },

    #[fail(display = "the shard is no longer running")]
    ShardGone,
}

impl From<OneShotRecvError> for ShardError {
    fn from(_error: OneShotRecvError) -> Self {
        ShardError::ShardGone
    }
}

impl<T> From<MpscSendError<T>> for ShardError {
    fn from(_error: MpscSendError<T>) -> Self {
        ShardError::ShardGone
    }
}

#[derive(Debug, Fail, Clone)]
pub enum UpstreamError {
    #[fail(display = "operation aborted")]
    OperationAborted,

    #[fail(display = "key not found")]
    KeyNotFound,

    #[fail(display = "driver error: {:?}", error)]
    DriverError { error: Arc<Error> },

    #[fail(display = "serialization error: {:?}", error)]
    SerializationError { error: Arc<Error> },
}

impl UpstreamError {
    pub fn is_not_found(&self) -> bool {
        match self {
            UpstreamError::KeyNotFound => true,
            _ => false,
        }
    }

    pub fn serialization_error<E>(error: E) -> Self
    where
        E: std::error::Error + Send + 'static,
    {
        UpstreamError::SerializationError {
            error: Arc::new(failure::SyncFailure::new(error).into()),
        }
    }
}

impl From<Error> for UpstreamError {
    fn from(error: Error) -> Self {
        UpstreamError::DriverError {
            error: Arc::new(error),
        }
    }
}

#[cfg(feature = "cassandra")]
impl From<cassandra_cpp::Error> for UpstreamError {
    fn from(error: cassandra_cpp::Error) -> Self {
        UpstreamError::DriverError {
            error: Arc::new(failure::SyncFailure::new(error).into()),
        }
    }
}

#[cfg(feature = "cassandra")]
impl From<cassandra_cpp::Error> for ShardError {
    fn from(error: cassandra_cpp::Error) -> Self {
        let upstream_error: UpstreamError = error.into();
        upstream_error.into()
    }
}

impl From<UpstreamError> for ShardError {
    fn from(error: UpstreamError) -> Self {
        ShardError::UpstreamError { error }
    }
}