error.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. use failure::{Backtrace, Context, Fail};
  2. use std::fmt;
  3. #[derive(Debug)]
  4. struct BackendError {
  5. inner: Context<BackendErrorKind>,
  6. }
  7. #[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
  8. enum BackendErrorKind {
  9. #[fail(display = "System doesn't have enough memory.")]
  10. MemoryError,
  11. }
  12. impl Fail for BackendError {
  13. fn cause(&self) -> Option<&Fail> {
  14. self.inner.cause()
  15. }
  16. fn backtrace(&self) -> Option<&Backtrace> {
  17. self.inner.backtrace()
  18. }
  19. }
  20. impl fmt::Display for BackendError {
  21. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  22. fmt::Display::fmt(&self.inner, f)
  23. }
  24. }
  25. // impl BackendError {
  26. // pub fn kind(&self) -> BackendErrorKind {
  27. // *self.inner.get_context()
  28. // }
  29. // }
  30. impl From<BackendErrorKind> for BackendError {
  31. fn from(kind: BackendErrorKind) -> BackendError {
  32. BackendError {
  33. inner: Context::new(kind),
  34. }
  35. }
  36. }
  37. impl From<Context<BackendErrorKind>> for BackendError {
  38. fn from(inner: Context<BackendErrorKind>) -> BackendError {
  39. BackendError { inner }
  40. }
  41. }