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
97
98
99
use cannyls;
use ecpool;
use fibers::sync::oneshot::MonitorError;
use frugalos_mds;
use libfrugalos;
use libfrugalos::entity::object::ObjectVersion;
use raftlog;
use std::io;
use std::sync::mpsc::RecvError;
use trackable::error::TrackableError;
use trackable::error::{ErrorKind as TrackableErrorKind, ErrorKindExt};

/// エラーの種類。
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub enum ErrorKind {
    UnexpectedVersion { current: Option<ObjectVersion> },
    Invalid,
    Busy,
    Corrupted,
    Other,
}
impl TrackableErrorKind for ErrorKind {}

/// クレート固有の`Error`型。
#[derive(Debug, Clone, TrackableError)]
pub struct Error(TrackableError<ErrorKind>);
impl From<libfrugalos::Error> for Error {
    fn from(f: libfrugalos::Error) -> Self {
        let kind = match *f.kind() {
            libfrugalos::ErrorKind::InvalidInput => ErrorKind::Invalid,
            libfrugalos::ErrorKind::Unexpected(current) => ErrorKind::UnexpectedVersion { current },
            libfrugalos::ErrorKind::Unavailable => ErrorKind::Busy,
            libfrugalos::ErrorKind::Timeout
            | libfrugalos::ErrorKind::NotLeader
            | libfrugalos::ErrorKind::Other => ErrorKind::Other,
        };
        kind.takes_over(f).into()
    }
}
impl From<io::Error> for Error {
    fn from(f: io::Error) -> Self {
        ErrorKind::Other.cause(f).into()
    }
}
impl From<RecvError> for Error {
    fn from(f: RecvError) -> Self {
        ErrorKind::Other.cause(f).into()
    }
}
impl From<frugalos_mds::Error> for Error {
    fn from(f: frugalos_mds::Error) -> Self {
        match *f.kind() {
            frugalos_mds::ErrorKind::Unexpected(version) => {
                let current = version.map(|v| ObjectVersion(v.0));
                ErrorKind::UnexpectedVersion { current }
                    .takes_over(f)
                    .into()
            }
            _ => ErrorKind::Other.takes_over(f).into(),
        }
    }
}
impl From<raftlog::Error> for Error {
    fn from(f: raftlog::Error) -> Self {
        // TODO: kindを見る
        ErrorKind::Other.takes_over(f).into()
    }
}
impl From<ecpool::Error> for Error {
    fn from(f: ecpool::Error) -> Self {
        // TODO: kindを見る
        ErrorKind::Other.takes_over(f).into()
    }
}
impl From<fibers_rpc::Error> for Error {
    fn from(f: fibers_rpc::Error) -> Self {
        let kind = match *f.kind() {
            fibers_rpc::ErrorKind::InvalidInput => ErrorKind::Invalid,
            fibers_rpc::ErrorKind::Unavailable => ErrorKind::Busy,
            fibers_rpc::ErrorKind::Timeout | fibers_rpc::ErrorKind::Other => ErrorKind::Other,
        };
        kind.takes_over(f).into()
    }
}
impl From<cannyls::Error> for Error {
    fn from(f: cannyls::Error) -> Self {
        ErrorKind::Other.takes_over(f).into()
    }
}
impl From<MonitorError<Error>> for Error {
    fn from(f: MonitorError<Error>) -> Self {
        f.unwrap_or_else(|| {
            ErrorKind::Other
                .cause("Monitor channel disconnected")
                .into()
        })
    }
}