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
use std::io;
use std::fmt;
use std::str;
use std::error::Error;
use std::convert;
use std::string::FromUtf8Error;

use protocol::Response;


#[derive(Debug)]
pub enum ErrorKind {
    Io(io::Error),
    Response(Response),
    Parse,
}

#[derive(Debug)]
pub struct MemcacheError {
    kind: ErrorKind,
}

impl Error for MemcacheError {
    fn description(&self) -> &str {
        match self.kind {
            ErrorKind::Io(ref err) => err.description(),
            ErrorKind::Response(ref resp) => {
                str::from_utf8(resp.value().unwrap_or(b"Unknown")).expect("Failed to parse memcached response")
            },
            ErrorKind::Parse => "TODO"
        }
    }

    fn cause(&self) -> Option<&Error> {
        match self.kind {
            ErrorKind::Io(ref err) => Some(err),
            _ => None,
        }
    }
}

impl fmt::Display for MemcacheError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.description())
    }
}

impl convert::From<io::Error> for MemcacheError {
    fn from(err: io::Error) -> Self {
        MemcacheError {
            kind: ErrorKind::Io(err),
        }
    }
}

impl convert::From<Response> for MemcacheError {
    fn from(resp: Response) -> Self {
        MemcacheError {
            kind: ErrorKind::Response(resp)
        }
    }
}

impl convert::From<FromUtf8Error> for MemcacheError {
    fn from(_err: FromUtf8Error) -> Self {
        MemcacheError {
            kind: ErrorKind::Parse,
        }
    }
}