cakerabbit_core/
errors.rs

1use rmpv::decode;
2use std::{error, fmt, io};
3
4#[derive(Debug)]
5pub struct CakeError(pub String);
6
7#[derive(Debug)]
8pub enum DecodeError {
9  Truncated,
10  Invalid,
11  UnknownIo(io::Error),
12}
13
14impl DecodeError {
15  fn description(&self) -> &str {
16    match *self {
17      DecodeError::Truncated => "could not read enough bytes to decode a complete message",
18      DecodeError::UnknownIo(_) => "Unknown IO error while decoding a message",
19      DecodeError::Invalid => "the byte sequence is not a valid msgpack-rpc message",
20    }
21  }
22}
23
24impl fmt::Display for DecodeError {
25  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
26    self.description().fmt(f)
27  }
28}
29
30impl error::Error for DecodeError {
31  fn description(&self) -> &str {
32    self.description()
33  }
34
35  fn cause(&self) -> Option<&dyn error::Error> {
36    match *self {
37      DecodeError::UnknownIo(ref e) => Some(e),
38      _ => None,
39    }
40  }
41}
42
43impl From<io::Error> for DecodeError {
44  fn from(err: io::Error) -> DecodeError {
45    match err.kind() {
46      io::ErrorKind::UnexpectedEof => DecodeError::Truncated,
47      io::ErrorKind::Other => {
48        if let Some(cause) = err.get_ref().unwrap().source() {
49          if cause.to_string() == "type mismatch" {
50            return DecodeError::Invalid;
51          }
52        }
53        DecodeError::UnknownIo(err)
54      }
55      _ => DecodeError::UnknownIo(err),
56    }
57  }
58}
59
60impl From<decode::Error> for DecodeError {
61  fn from(err: decode::Error) -> DecodeError {
62    match err {
63      decode::Error::InvalidMarkerRead(io_err) | decode::Error::InvalidDataRead(io_err) => {
64        From::from(io_err)
65      }
66    }
67  }
68}
69
70pub fn cake_errors(e: &str) -> String {
71  match e {
72    "callFailtryErr" => { "--@@@--cakeClient call Failtry max retries Error--@@@--".to_string() }
73    _ => { "unknow error".to_string() }
74  }
75}
76
77
78
79