neon_arch commited on
Commit
c2280b7
1 Parent(s): 4460730

chore: add source and an appropriate message to different error variants

Browse files
Files changed (1) hide show
  1. src/engines/engine_models.rs +37 -10
src/engines/engine_models.rs CHANGED
@@ -13,12 +13,15 @@ use scraper::error::SelectorErrorKind;
13
  /// search engines.
14
  /// * `UnexpectedError` - This variant handles all the errors which are unexpected or occur rarely
15
  /// and are errors mostly related to failure in initialization of HeaderMap, Selector errors and
16
- /// all other errors.
17
  #[derive(Debug)]
18
  pub enum EngineErrorKind {
19
  RequestError(reqwest::Error),
20
  EmptyResultSet,
21
- UnexpectedError(String),
 
 
 
22
  }
23
 
24
  /// Implementing `Display` trait to make errors writable on the stdout and also providing/passing the
@@ -26,29 +29,53 @@ pub enum EngineErrorKind {
26
  impl std::fmt::Display for EngineErrorKind {
27
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28
  match self {
29
- EngineErrorKind::RequestError(request_error) => write!(f, "{}", request_error),
 
 
30
  EngineErrorKind::EmptyResultSet => {
31
  write!(f, "The upstream search engine returned an empty result set")
32
  }
33
- EngineErrorKind::UnexpectedError(unexpected_error) => write!(f, "{}", unexpected_error),
 
 
 
 
 
 
34
  }
35
  }
36
  }
37
 
38
- /// Implementing `Error` trait to make the the `EngineErrorKind` enum an error type.
39
- impl std::error::Error for EngineErrorKind {}
 
 
 
 
 
 
 
 
 
 
40
 
41
  /// Implementing `From` trait to map the `SelectorErrorKind` to `UnexpectedError` variant.
42
- impl<'a> From<SelectorErrorKind<'a>> for EngineErrorKind {
43
- fn from(err: SelectorErrorKind<'a>) -> Self {
44
- Self::UnexpectedError(err.to_string())
 
 
 
45
  }
46
  }
47
 
48
  /// Implementing `From` trait to map the `InvalidHeaderValue` to `UnexpectedError` variant.
49
  impl From<InvalidHeaderValue> for EngineErrorKind {
50
  fn from(err: InvalidHeaderValue) -> Self {
51
- Self::UnexpectedError(err.to_string())
 
 
 
52
  }
53
  }
54
 
 
13
  /// search engines.
14
  /// * `UnexpectedError` - This variant handles all the errors which are unexpected or occur rarely
15
  /// and are errors mostly related to failure in initialization of HeaderMap, Selector errors and
16
+ /// all other errors occuring within the code handling the `upstream search engines`.
17
  #[derive(Debug)]
18
  pub enum EngineErrorKind {
19
  RequestError(reqwest::Error),
20
  EmptyResultSet,
21
+ UnexpectedError {
22
+ message: String,
23
+ source: Option<Box<dyn std::error::Error>>,
24
+ },
25
  }
26
 
27
  /// Implementing `Display` trait to make errors writable on the stdout and also providing/passing the
 
29
  impl std::fmt::Display for EngineErrorKind {
30
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31
  match self {
32
+ EngineErrorKind::RequestError(request_error) => {
33
+ write!(f, "Request error: {}", request_error)
34
+ }
35
  EngineErrorKind::EmptyResultSet => {
36
  write!(f, "The upstream search engine returned an empty result set")
37
  }
38
+ EngineErrorKind::UnexpectedError { message, source } => {
39
+ write!(f, "Unexpected error: {}", message)?;
40
+ if let Some(source) = source {
41
+ write!(f, "\nCaused by: {}", source)?;
42
+ }
43
+ Ok(())
44
+ }
45
  }
46
  }
47
  }
48
 
49
+ /// Implementing `Error` trait to make the the `EngineErrorKind` enum an error type and
50
+ /// mapping `ReqwestErrors` to `RequestError` and `UnexpectedError` errors to all other unexpected
51
+ /// errors ocurring within the code handling the upstream search engines.
52
+ impl std::error::Error for EngineErrorKind {
53
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54
+ match self {
55
+ EngineErrorKind::RequestError(request_error) => Some(request_error),
56
+ EngineErrorKind::UnexpectedError { source, .. } => source.as_deref().map(|s| s),
57
+ _ => None,
58
+ }
59
+ }
60
+ }
61
 
62
  /// Implementing `From` trait to map the `SelectorErrorKind` to `UnexpectedError` variant.
63
+ impl From<SelectorErrorKind<'_>> for EngineErrorKind {
64
+ fn from(err: SelectorErrorKind<'_>) -> Self {
65
+ Self::UnexpectedError {
66
+ message: err.to_string(),
67
+ source: None,
68
+ }
69
  }
70
  }
71
 
72
  /// Implementing `From` trait to map the `InvalidHeaderValue` to `UnexpectedError` variant.
73
  impl From<InvalidHeaderValue> for EngineErrorKind {
74
  fn from(err: InvalidHeaderValue) -> Self {
75
+ Self::UnexpectedError {
76
+ message: err.to_string(),
77
+ source: Some(Box::new(err)),
78
+ }
79
  }
80
  }
81