Skip to main content

csm_rs/
error.rs

1use std::{env::VarError, io, num::ParseIntError, str::Utf8Error};
2
3use aws_smithy_types::byte_stream;
4use globset::Error as GlobsetError;
5use manta_backend_dispatcher::error::Error as MantaError;
6use serde_json::Value;
7use tokio::task::JoinError;
8
9#[derive(thiserror::Error, Debug)]
10pub enum Error {
11  #[error("CSM-RS > Generic error: {0}")]
12  Message(String),
13  #[error("CSM-RS > Environment variable: {0}")]
14  EnvVarError(#[from] VarError),
15  #[error("CSM-RS > IO: {0}")]
16  IoError(#[from] io::Error),
17  #[error("CSM-RS > Serde JSON: {0}")]
18  SerdeJsonError(#[from] serde_json::Error),
19  #[error("CSM-RS > Serde YAML: {0}")]
20  SerdeYamlError(#[from] serde_yaml::Error),
21  #[error("CSM-RS > Net: {0}")]
22  NetError(#[from] reqwest::Error),
23  #[error("CSM-RS > Tokio: {0}")]
24  TokioError(#[from] JoinError),
25  #[error("CSM-RS > Error converting from UTF8 to String: {0}")]
26  UtfError(#[from] Utf8Error),
27  #[error("CSM-RS > Glob error: {0}")]
28  GlobError(#[from] GlobsetError),
29  #[error("CSM-RS > Parse int error: {0}")]
30  ParseStrIntError(#[from] ParseIntError),
31  #[error("CSM-RS > URL parse error: {0}")]
32  SmithyDataStreamError(#[from] byte_stream::error::Error),
33  #[error("http request:\nresponse: {response}\npayload: {payload}")]
34  RequestError {
35    response: reqwest::Error,
36    payload: String, // NOTE: CSM/OCHAMI Apis either returns plain text or a json therefore, we
37                     // will just return a String
38  },
39  #[error("CSM-RS > CSM: {}", .0.get("detail").and_then(|detail| detail.as_str()).unwrap_or("Unknown error"))]
40  CsmError(Value),
41  #[error("CSM-RS > Console: {0}")]
42  ConsoleError(String),
43  #[error("CSM-RS > K8s: {0}")]
44  K8sError(String),
45  #[error("CSM-RS > K8s: field '{0}' missing in k8s credentials")]
46  K8sCredentialMissingError(String),
47  #[error("CSM-RS > K8s: '{0}' value not a string")]
48  K8sCredentialNotStringError(String),
49  #[error("CSM-RS > K8s: {0}")]
50  K8sExecError(#[from] kube::Error),
51  #[error("CSM-RS > CFS Session")]
52  ImageNotFound(String),
53  #[error("CSM-RS > Group '{0}' not found")]
54  GroupNotFound(String),
55  #[error("CSM-RS > No derivatives found for CFS Configuration: {0}")]
56  ConfigurationDerivativesNotFound(String),
57  #[error("CSM-RS > Configuration '{0}' does not have a name defined")]
58  ConfigurationNameNotDefined(String),
59  #[error("CSM-RS > CFS Configuration already exists: {0}")]
60  ConfigurationAlreadyExists(String),
61  #[error(
62    "CSM-RS > CFS Configuration used as a runtime configuration for a cluster and/or used to build an image used to boot node(s)"
63  )]
64  ConfigurationUsedAsRuntimeConfigurationOrUsedToBuildBootImageUsed,
65  #[error("CSM-RS > Session '{0}' not found")]
66  SessionNotFound(String),
67  #[error("CSM-RS > Session '{0}' does not have a name defined")]
68  SessionNameNotDefined(String),
69  #[error("CSM-RS > Session '{0}' does not have a configuration defined")]
70  SessionConfigurationNotDefined(String),
71  #[error("CSM-RS > IMS key '{0}' not found")]
72  ImsKeyNotFound(String),
73  #[error("CSM-RS > HSM component '{0}' not found")]
74  HsmComponentNotFound(String),
75  #[error("CSM-RS > HSM component '{0}' does not have a ID defined")]
76  HsmComponentIdNotDefined(String),
77  #[error("CSM-RS > HSM component '{0}' does not have a NID defined")]
78  HsmComponentNidNotDefined(String),
79  #[error("CSM-RS > HSM component '{0}' does not have a power state defined")]
80  HsmComponentPowerStateNotDefined(String),
81  #[error("CSM-RS > HSM component '{0}' does not have a '{1}' defined")]
82  HsmComponentFieldNotDefined(String, String),
83  #[error("CSM-RS > CFS component field '{0}' not defined")]
84  CfsComponentFieldNotDefined(String),
85  #[error("CSM-RS > CFS component does not have a 'name' defined")]
86  CfsComponentNameFieldNotDefined(),
87  #[error("CSM-RS > CFS component does not have a 'desired_conf' defined")]
88  CfsComponentDesiredConfFieldNotDefined(),
89}
90
91// Convert Error to manta_backend_dispatcher::error::Error
92impl Into<MantaError> for crate::error::Error {
93  fn into(self) -> MantaError {
94    match self {
95      Error::IoError(e) => MantaError::IoError(e),
96      Error::SerdeJsonError(e) => MantaError::SerdeError(e),
97      Error::NetError(e) => MantaError::NetError(e),
98      Error::RequestError { response, payload } => {
99        MantaError::RequestError { response, payload }
100      }
101      Error::CsmError(v) => MantaError::CsmError(v),
102      // Variants with direct dispatcher equivalents
103      Error::Message(s) => MantaError::Message(s),
104      Error::ConsoleError(s) => MantaError::ConsoleError(s),
105      Error::ConfigurationAlreadyExists(s) => MantaError::ConfigurationAlreadyExistsError(s),
106      Error::SessionNotFound(_) => MantaError::SessionNotFound,
107      Error::ConfigurationUsedAsRuntimeConfigurationOrUsedToBuildBootImageUsed => {
108        MantaError::Conflict(
109          Error::ConfigurationUsedAsRuntimeConfigurationOrUsedToBuildBootImageUsed
110            .to_string(),
111        )
112      }
113      // Not-found variants mapped to NotFound
114      Error::ImageNotFound(s) => MantaError::NotFound(format!("Image '{s}'")),
115      Error::GroupNotFound(s) => MantaError::NotFound(format!("Group '{s}'")),
116      Error::HsmComponentNotFound(s) => MantaError::NotFound(format!("HSM component '{s}'")),
117      Error::ImsKeyNotFound(s) => MantaError::NotFound(format!("IMS key '{s}'")),
118      Error::ConfigurationDerivativesNotFound(s) => {
119        MantaError::NotFound(format!("No derivatives for CFS configuration '{s}'"))
120      }
121      // K8s variants collapsed to K8sError
122      Error::K8sError(s) => MantaError::K8sError(s),
123      Error::K8sCredentialMissingError(s) => {
124        MantaError::K8sError(format!("field '{s}' missing in k8s credentials"))
125      }
126      Error::K8sCredentialNotStringError(s) => {
127        MantaError::K8sError(format!("'{s}' value not a string"))
128      }
129      Error::K8sExecError(e) => MantaError::K8sError(e.to_string()),
130      // Technical/internal errors — preserve message text
131      _ => MantaError::Message(self.to_string()),
132    }
133  }
134}