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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
use super::models::FeverError; use failure::{Backtrace, Context, Fail}; use std::fmt; #[derive(Debug)] pub struct ApiError { inner: Context<ApiErrorKind>, } #[derive(Clone, Eq, PartialEq, Debug, Fail)] pub enum ApiErrorKind { #[fail(display = "Url Error")] Url, #[fail(display = "Json Error")] Json, #[fail(display = "Http request failed")] Http, #[fail(display = "Fever error")] Fever(FeverError), #[fail(display = "Malformed input arguments")] Input, #[fail(display = "IO Error")] IO, #[fail(display = "No valid access token available")] Token, #[fail(display = "Request failed with message access denied")] AccessDenied, #[fail(display = "Internal error")] InternalMutabilty, #[fail(display = "Access token expired")] TokenExpired, #[fail(display = "Error parsing string (enum/date) returned by fever")] Parse, #[fail(display = "Unknown error")] Unknown, } impl Fail for ApiError { fn cause(&self) -> Option<&dyn Fail> { self.inner.cause() } fn backtrace(&self) -> Option<&Backtrace> { self.inner.backtrace() } } impl fmt::Display for ApiError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.inner, f) } } impl ApiError { pub fn kind(&self) -> ApiErrorKind { self.inner.get_context().clone() } } impl From<ApiErrorKind> for ApiError { fn from(kind: ApiErrorKind) -> ApiError { ApiError { inner: Context::new(kind), } } } impl From<Context<ApiErrorKind>> for ApiError { fn from(inner: Context<ApiErrorKind>) -> ApiError { ApiError { inner: inner } } }