coodev_runner/error/
mod.rs

1use octocrate::GithubError;
2use std::process::ExitStatus;
3
4#[derive(thiserror::Error, Debug)]
5pub enum Error {
6  #[error("Failed to parse user config: {0}")]
7  WorkflowConfigError(String),
8
9  #[error("Error while running workflow: {0}")]
10  InternalRuntimeError(String),
11
12  #[error("Failed with exit status: {0:?}")]
13  Failed(ExitStatus),
14
15  #[error("IO error: {message}")]
16  IOError {
17    source: std::io::Error,
18    message: String,
19  },
20
21  #[error("Github API error: {message}")]
22  GithubError {
23    source: GithubError,
24    message: String,
25  },
26
27  #[error("Unsupported feature: {0}")]
28  UnsupportedFeature(String),
29}
30
31impl Error {
32  pub fn workflow_config_error<T: ToString>(message: T) -> Self {
33    Self::WorkflowConfigError(message.to_string())
34  }
35
36  pub fn internal_runtime_error<T: ToString>(message: T) -> Self {
37    Self::InternalRuntimeError(message.to_string())
38  }
39
40  pub fn io_error<T: ToString>(source: std::io::Error, message: T) -> Self {
41    Self::IOError {
42      source,
43      message: message.to_string(),
44    }
45  }
46
47  pub fn github_error<T: ToString>(source: GithubError, message: T) -> Self {
48    Self::GithubError {
49      source,
50      message: message.to_string(),
51    }
52  }
53
54  pub fn failed(exit_status: ExitStatus) -> Self {
55    Self::Failed(exit_status)
56  }
57
58  pub fn unsupported_feature<T: ToString>(message: T) -> Self {
59    Self::UnsupportedFeature(message.to_string())
60  }
61}
62
63// implement Eq and PartialEq for Error so that we can compare errors in tests
64impl PartialEq for Error {
65  fn eq(&self, other: &Self) -> bool {
66    match (self, other) {
67      (Self::WorkflowConfigError(a), Self::WorkflowConfigError(b)) => a == b,
68      (Self::InternalRuntimeError(a), Self::InternalRuntimeError(b)) => a == b,
69      (Self::Failed(a), Self::Failed(b)) => a == b,
70      (Self::IOError { message: a, .. }, Self::IOError { message: b, .. }) => a == b,
71      (Self::GithubError { message: a, .. }, Self::GithubError { message: b, .. }) => a == b,
72      (Self::UnsupportedFeature(a), Self::UnsupportedFeature(b)) => a == b,
73      _ => false,
74    }
75  }
76}