1use std::fmt;
2
3use failure::{Fail, Context, Backtrace};
4
5#[derive(Debug)]
6pub struct Error {
7 inner: Context<ErrorKind>
8}
9
10impl Fail for Error {
11 fn cause(&self) -> Option<&Fail> {
12 self.inner.cause()
13 }
14
15 fn backtrace(&self) -> Option<&Backtrace> {
16 self.inner.backtrace()
17 }
18}
19
20impl fmt::Display for Error {
21 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22 fmt::Display::fmt(&self.inner, f)
23 }
24}
25
26impl From<ErrorKind> for Error {
27 fn from(kind: ErrorKind) -> Error {
28 Error { inner: Context::new(kind) }
29 }
30}
31
32impl From<Context<ErrorKind>> for Error {
33 fn from(inner: Context<ErrorKind>) -> Error {
34 Error { inner: inner }
35 }
36}
37
38impl From<::std::io::Error> for Error {
39 fn from(error: ::std::io::Error) -> Self {
40 Error {
41 inner: Context::new(ErrorKind::IoError {
42 error
43 })
44 }
45 }
46}
47
48#[derive(Debug, Fail)]
49pub enum ErrorKind {
50 #[fail(display = "invalid procotol")]
51 InvalidProtocol,
52 #[fail(display = "internal error")]
53 InternalError,
54 #[fail(display = "io error: {}", error)]
55 IoError {
56 error: ::std::io::Error
57 }
58}