|
| 1 | +// Error handling based on the failure crate |
| 2 | + |
| 3 | +use std::fmt; |
| 4 | +use std::result; |
| 5 | + |
| 6 | +use failure::{Backtrace, Context, Error, Fail}; |
| 7 | + |
| 8 | +pub type VapidResult<T> = result::Result<T, Error>; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +pub struct VapidError { |
| 12 | + inner: Context<VapidErrorKind>, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Clone, Eq, PartialEq, Debug, Fail)] |
| 16 | +pub enum VapidErrorKind { |
| 17 | + #[fail(display = "Invalid public key")] |
| 18 | + PublicKeyError, |
| 19 | + #[fail(display = "VAPID error: {}", _0)] |
| 20 | + VapidError(String), |
| 21 | + #[fail(display = "Internal Error {:?}", _0)] |
| 22 | + InternalError(String), |
| 23 | +} |
| 24 | + |
| 25 | +/* |
| 26 | +impl VapidError { |
| 27 | + pub fn kind(&self) -> &VapidErrorKind { |
| 28 | + self.inner.get_context() |
| 29 | + } |
| 30 | +} |
| 31 | +*/ |
| 32 | + |
| 33 | +impl Fail for VapidError { |
| 34 | + fn cause(&self) -> Option<&Fail> { |
| 35 | + self.inner.cause() |
| 36 | + } |
| 37 | + |
| 38 | + fn backtrace(&self) -> Option<&Backtrace> { |
| 39 | + self.inner.backtrace() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl fmt::Display for VapidError { |
| 44 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 45 | + fmt::Display::fmt(&self.inner, f) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl From<VapidErrorKind> for VapidError { |
| 50 | + fn from(kind: VapidErrorKind) -> VapidError { |
| 51 | + Context::new(kind).into() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl From<Context<VapidErrorKind>> for VapidError { |
| 56 | + fn from(inner: Context<VapidErrorKind>) -> VapidError { |
| 57 | + VapidError { inner } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl From<Error> for VapidError { |
| 62 | + fn from(err: Error) -> VapidError { |
| 63 | + VapidErrorKind::InternalError(format!("Error: {:?}", err)).into() |
| 64 | + } |
| 65 | +} |
0 commit comments