1use std::io;
2use std::fmt;
3use std::str;
4use std::error::Error;
5use std::convert;
6use std::string::FromUtf8Error;
7
8use protocol::Response;
9
10
11#[derive(Debug)]
12pub enum ErrorKind {
13 Io(io::Error),
14 Response(Response),
15 Parse,
16}
17
18#[derive(Debug)]
19pub struct MemcacheError {
20 kind: ErrorKind,
21}
22
23impl Error for MemcacheError {
24 fn description(&self) -> &str {
25 match self.kind {
26 ErrorKind::Io(ref err) => err.description(),
27 ErrorKind::Response(ref resp) => {
28 str::from_utf8(resp.value().unwrap_or(b"Unknown")).expect("Failed to parse memcached response")
29 },
30 ErrorKind::Parse => "TODO"
31 }
32 }
33
34 fn cause(&self) -> Option<&Error> {
35 match self.kind {
36 ErrorKind::Io(ref err) => Some(err),
37 _ => None,
38 }
39 }
40}
41
42impl fmt::Display for MemcacheError {
43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44 f.write_str(self.description())
45 }
46}
47
48impl convert::From<io::Error> for MemcacheError {
49 fn from(err: io::Error) -> Self {
50 MemcacheError {
51 kind: ErrorKind::Io(err),
52 }
53 }
54}
55
56impl convert::From<Response> for MemcacheError {
57 fn from(resp: Response) -> Self {
58 MemcacheError {
59 kind: ErrorKind::Response(resp)
60 }
61 }
62}
63
64impl convert::From<FromUtf8Error> for MemcacheError {
65 fn from(_err: FromUtf8Error) -> Self {
66 MemcacheError {
67 kind: ErrorKind::Parse,
68 }
69 }
70}