Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. use std::fmt;
  2. use std::result;
  3.  
  4. pub type Result<T> = result::Result<T, AppError>;
  5.  
  6. pub struct AppError(Box<ErrorKind>);
  7.  
  8. impl AppError {
  9. pub(crate) fn new(kind: ErrorKind) -> AppError {
  10. AppError(Box::new(kind))
  11. }
  12.  
  13. pub(crate) fn from_string(msg: &str) -> AppError {
  14. AppError(Box::new(ErrorKind::Generic(msg.into())))
  15. }
  16.  
  17. pub fn kind(&self) -> &ErrorKind {
  18. &self.0
  19. }
  20.  
  21. pub fn into_kind(self) -> ErrorKind {
  22. *self.0
  23. }
  24.  
  25. //pub fn is_io_error(&self) -> bool {
  26. //match *self.0 {
  27. //ErrorKind::Io(_) => true,
  28. //_ => false,
  29. //}
  30. //}
  31. }
  32.  
  33. #[derive(Debug)]
  34. pub enum ErrorKind {
  35. Generic(String),
  36. R2D2(r2d2::Error),
  37. DbResult(diesel::result::Error),
  38. Regex(regex::Error)
  39. }
  40.  
  41. impl fmt::Display for AppError {
  42. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  43. match *self.0 {
  44. ErrorKind::Generic(ref msg) => {
  45. write!(f, "Generic error: {}", msg)
  46. }
  47. ErrorKind::R2D2(ref err) => err.fmt(f),
  48. ErrorKind::DbResult(ref err) => err.fmt(f),
  49. ErrorKind::Regex(ref err) => err.fmt(f),
  50. }
  51. }
  52. }
  53.  
  54. impl From<r2d2::Error> for AppError {
  55. fn from(err: r2d2::Error) -> AppError {
  56. AppError::new(ErrorKind::R2D2(err))
  57. }
  58. }
  59.  
  60. impl From<diesel::result::Error> for AppError {
  61. fn from(err: diesel::result::Error) -> AppError {
  62. AppError::new(ErrorKind::DbResult(err))
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement