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
use failure::{Context, Fail, Backtrace};
use std::fmt::{self, Display};

#[derive(Clone, Debug, Fail)]
pub enum ServiceErrorKind {
    #[fail(display = "An Error spawning a new service")]
    ServiceCreation,
    #[fail(display = "An Error during handling a request")]
    RequestError,
    #[fail(display = "Unhandled request for path: {}", _0)]
    UnhandledError(String),
    #[fail(display = "Internal Error")]
    InternalError,
}

#[derive(Debug)]
pub struct ServiceError {
    inner: Context<ServiceErrorKind>,
}

impl Fail for ServiceError {
    fn cause(&self) -> Option<&Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl ServiceError {
    pub fn kind(&self) -> ServiceErrorKind {
        self.inner.get_context().clone()
    }
}

impl From<ServiceErrorKind> for ServiceError {
    fn from(kind: ServiceErrorKind) -> ServiceError {
        ServiceError { inner: Context::new(kind) }
    }
}

impl From<Context<ServiceErrorKind>> for ServiceError {
    fn from(inner: Context<ServiceErrorKind>) -> ServiceError {
        ServiceError { inner: inner }
    }
}

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