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
use bytecodec;
use cannyls;
use fibers::sync::oneshot::MonitorError;
use libfrugalos;
use raftlog;
use std;
use std::convert::Into;
use trackable::error::TrackableError;
use trackable::error::{ErrorKind as TrackableErrorKind, ErrorKindExt};

/// クレート固有の`Error`型。
#[derive(Debug, Clone, TrackableError)]
pub struct Error(TrackableError<ErrorKind>);
impl From<std::net::AddrParseError> for Error {
    fn from(f: std::net::AddrParseError) -> Self {
        ErrorKind::InvalidInput.cause(f).into()
    }
}
impl From<std::io::Error> for Error {
    fn from(f: std::io::Error) -> Self {
        ErrorKind::Other.cause(f).into()
    }
}
impl From<cannyls::Error> for Error {
    fn from(f: cannyls::Error) -> Self {
        ErrorKind::Other.takes_over(f).into()
    }
}
impl From<raftlog::Error> for Error {
    fn from(f: raftlog::Error) -> Self {
        let kind = match *f.kind() {
            raftlog::ErrorKind::InvalidInput => ErrorKind::InvalidInput,
            raftlog::ErrorKind::NotLeader => ErrorKind::NotLeader,
            _ => ErrorKind::Other,
        };
        kind.takes_over(f).into()
    }
}
impl From<bytecodec::Error> for Error {
    fn from(f: bytecodec::Error) -> Self {
        ErrorKind::InvalidInput.takes_over(f).into()
    }
}
impl<E: Into<Error>> From<MonitorError<E>> for Error {
    fn from(f: MonitorError<E>) -> Self {
        f.map(Into::into).unwrap_or_else(|| {
            ErrorKind::Other
                .cause("Monitor channel disconnected")
                .into()
        })
    }
}
impl From<libfrugalos::Error> for Error {
    fn from(f: libfrugalos::Error) -> Self {
        let kind = match *f.kind() {
            libfrugalos::ErrorKind::InvalidInput => ErrorKind::InvalidInput,
            libfrugalos::ErrorKind::NotLeader => ErrorKind::NotLeader,
            _ => ErrorKind::Other,
        };
        kind.takes_over(f).into()
    }
}

pub fn to_rpc_error(e: Error) -> libfrugalos::Error {
    let kind = match *e.kind() {
        ErrorKind::InvalidInput => libfrugalos::ErrorKind::InvalidInput,
        ErrorKind::NotLeader => libfrugalos::ErrorKind::NotLeader,
        _ => libfrugalos::ErrorKind::Other,
    };
    kind.cause(e).into()
}

/// エラー種類。
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub enum ErrorKind {
    InvalidInput,
    NotLeader,
    Other,
}
impl TrackableErrorKind for ErrorKind {}