webframework/
error.rs

1use failure::{Context, Fail, Backtrace};
2use std::fmt::{self, Display};
3
4#[derive(Clone, Debug, Fail)]
5pub enum ServiceErrorKind {
6    #[fail(display = "An Error spawning a new service")]
7    ServiceCreation,
8    #[fail(display = "An Error during handling a request")]
9    RequestError,
10    #[fail(display = "Unhandled request for path: {}", _0)]
11    UnhandledError(String),
12    #[fail(display = "Internal Error")]
13    InternalError,
14}
15
16#[derive(Debug)]
17pub struct ServiceError {
18    inner: Context<ServiceErrorKind>,
19}
20
21impl Fail for ServiceError {
22    fn cause(&self) -> Option<&Fail> {
23        self.inner.cause()
24    }
25
26    fn backtrace(&self) -> Option<&Backtrace> {
27        self.inner.backtrace()
28    }
29}
30
31impl ServiceError {
32    pub fn kind(&self) -> ServiceErrorKind {
33        self.inner.get_context().clone()
34    }
35}
36
37impl From<ServiceErrorKind> for ServiceError {
38    fn from(kind: ServiceErrorKind) -> ServiceError {
39        ServiceError { inner: Context::new(kind) }
40    }
41}
42
43impl From<Context<ServiceErrorKind>> for ServiceError {
44    fn from(inner: Context<ServiceErrorKind>) -> ServiceError {
45        ServiceError { inner: inner }
46    }
47}
48
49impl Display for ServiceError {
50    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51        Display::fmt(&self.inner, f)
52    }
53}