astro_run/types/
error.rs

1#[derive(thiserror::Error, Debug)]
2pub enum Error {
3  #[error("Failed to parse user config: {0}")]
4  WorkflowConfigError(String),
5
6  #[error("Error while running workflow: {0}")]
7  InternalRuntimeError(String),
8
9  #[error("Failed with exit code: {0:?}")]
10  Failed(usize),
11
12  #[error("Error: {0}")]
13  Error(String),
14
15  #[error("IO error: {0}")]
16  IOError(#[from] std::io::Error),
17
18  #[error("Failed to initialize workflow: {0}")]
19  InitError(String),
20
21  #[error("Unsupported feature: {0}")]
22  UnsupportedFeature(String),
23}
24
25impl Error {
26  pub fn workflow_config_error<T: ToString>(message: T) -> Self {
27    Self::WorkflowConfigError(message.to_string())
28  }
29
30  pub fn internal_runtime_error<T: ToString>(message: T) -> Self {
31    Self::InternalRuntimeError(message.to_string())
32  }
33
34  pub fn io_error(source: std::io::Error) -> Self {
35    Self::IOError(source)
36  }
37
38  pub fn failed(exit_code: usize) -> Self {
39    Self::Failed(exit_code)
40  }
41
42  pub fn unsupported_feature<T: ToString>(message: T) -> Self {
43    Self::UnsupportedFeature(message.to_string())
44  }
45
46  pub fn init_error<T: ToString>(message: T) -> Self {
47    Self::InitError(message.to_string())
48  }
49
50  #[allow(clippy::self_named_constructors)]
51  pub fn error<T: ToString>(message: T) -> Self {
52    Self::Error(message.to_string())
53  }
54}
55
56// implement Eq and PartialEq for Error so that we can compare errors in tests
57impl PartialEq for Error {
58  fn eq(&self, other: &Self) -> bool {
59    match (self, other) {
60      (Self::WorkflowConfigError(a), Self::WorkflowConfigError(b)) => a == b,
61      (Self::InternalRuntimeError(a), Self::InternalRuntimeError(b)) => a == b,
62      (Self::Failed(a), Self::Failed(b)) => a == b,
63      (Self::IOError(a), Self::IOError(b)) => a.kind() == b.kind(),
64      (Self::Error(a), Self::Error(b)) => a == b,
65      (Self::UnsupportedFeature(a), Self::UnsupportedFeature(b)) => a == b,
66      (Self::InitError(a), Self::InitError(b)) => a == b,
67      _ => false,
68    }
69  }
70}
71
72#[cfg(test)]
73mod tests {
74  use super::*;
75
76  #[test]
77  fn test_eq() {
78    assert_eq!(
79      Error::workflow_config_error("hello"),
80      Error::workflow_config_error("hello")
81    );
82    assert_eq!(
83      Error::internal_runtime_error("hello"),
84      Error::internal_runtime_error("hello")
85    );
86    assert_eq!(
87      Error::io_error(std::io::Error::new(std::io::ErrorKind::Other, "hello"),),
88      Error::io_error(std::io::Error::new(std::io::ErrorKind::Other, "hello"))
89    );
90    assert_eq!(
91      Error::unsupported_feature("hello"),
92      Error::unsupported_feature("hello")
93    );
94    assert_eq!(Error::error("hello"), Error::error("hello"));
95    assert_eq!(Error::failed(1), Error::failed(1));
96  }
97
98  #[test]
99  fn test_ne() {
100    assert_ne!(
101      Error::workflow_config_error("hello"),
102      Error::workflow_config_error("world")
103    );
104    assert_ne!(
105      Error::internal_runtime_error("hello"),
106      Error::internal_runtime_error("world")
107    );
108    assert_ne!(
109      Error::io_error(std::io::Error::new(std::io::ErrorKind::Other, "hello"),),
110      Error::io_error(std::io::Error::new(
111        std::io::ErrorKind::Unsupported,
112        "world"
113      ),)
114    );
115    assert_ne!(
116      Error::unsupported_feature("hello"),
117      Error::unsupported_feature("world")
118    );
119    assert_ne!(Error::error("hello"), Error::error("world"));
120    assert_ne!(Error::failed(1), Error::failed(2));
121    assert_ne!(Error::failed(1), Error::internal_runtime_error("hello"));
122  }
123}