acari_lib/
error.rs

1use std::error::Error;
2use std::fmt;
3use std::io;
4use std::num;
5
6#[derive(Debug)]
7pub enum AcariError {
8  Io(io::Error),
9  Time(std::time::SystemTimeError),
10  DateFormat(chrono::format::ParseError),
11  Request(reqwest::Error),
12  Json(serde_json::Error),
13  Url(url::ParseError),
14  Mite(u16, String),
15  UserError(String),
16  InternalError(String),
17  ParseNum(num::ParseIntError),
18}
19
20impl fmt::Display for AcariError {
21  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22    match self {
23      AcariError::Io(err) => write!(f, "IO error: {}", err),
24      AcariError::Time(err) => write!(f, "Time error: {}", err),
25      AcariError::DateFormat(err) => write!(f, "Date format error: {}", err),
26      AcariError::Request(err) => write!(f, "Request error: {}", err),
27      AcariError::Json(err) => write!(f, "Json error: {}", err),
28      AcariError::Url(err) => write!(f, "Url error: {}", err),
29      AcariError::Mite(status, error) => write!(f, "Mite error ({}): {}", status, error),
30      AcariError::UserError(s) => write!(f, "User error: {}", s),
31      AcariError::InternalError(s) => write!(f, "Internal error: {}", s),
32      AcariError::ParseNum(err) => write!(f, "Number error: {}", err),
33    }
34  }
35}
36
37impl Error for AcariError {
38  fn source(&self) -> Option<&(dyn Error + 'static)> {
39    match self {
40      AcariError::Io(err) => Some(err),
41      AcariError::Time(err) => Some(err),
42      AcariError::DateFormat(err) => Some(err),
43      AcariError::Request(err) => Some(err),
44      AcariError::Json(err) => Some(err),
45      AcariError::ParseNum(err) => Some(err),
46      _ => None,
47    }
48  }
49}
50
51macro_rules! acari_error_from {
52  ($error: ty, $app_error: ident) => {
53    impl From<$error> for AcariError {
54      fn from(err: $error) -> AcariError {
55        AcariError::$app_error(err)
56      }
57    }
58  };
59}
60
61acari_error_from!(io::Error, Io);
62acari_error_from!(std::time::SystemTimeError, Time);
63acari_error_from!(serde_json::Error, Json);
64acari_error_from!(url::ParseError, Url);
65acari_error_from!(chrono::format::ParseError, DateFormat);
66acari_error_from!(reqwest::Error, Request);
67acari_error_from!(num::ParseIntError, ParseNum);