Skip to main content

kms_aead/
errors.rs

1use rsb_derive::Builder;
2use std::error::Error;
3use std::fmt::Display;
4use std::fmt::Formatter;
5use std::time::SystemTimeError;
6
7pub type BoxedError = Box<dyn std::error::Error + Send + Sync>;
8
9#[cfg(feature = "serde")]
10use serde::{Deserialize, Serialize};
11
12#[derive(Debug)]
13pub enum KmsAeadError {
14    SystemError(KmsAeadSystemError),
15    NetworkError(KmsAeadNetworkError),
16    EncryptionError(KmsAeadEncryptionError),
17}
18
19impl Display for KmsAeadError {
20    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
21        match *self {
22            KmsAeadError::SystemError(ref err) => err.fmt(f),
23            KmsAeadError::NetworkError(ref err) => err.fmt(f),
24            KmsAeadError::EncryptionError(ref err) => err.fmt(f),
25        }
26    }
27}
28
29impl std::error::Error for KmsAeadError {
30    fn source(&self) -> Option<&(dyn Error + 'static)> {
31        match *self {
32            KmsAeadError::SystemError(ref err) => Some(err),
33            KmsAeadError::NetworkError(ref err) => Some(err),
34            KmsAeadError::EncryptionError(ref err) => Some(err),
35        }
36    }
37}
38
39#[derive(Debug, Eq, PartialEq, Clone, Builder)]
40#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
41pub struct KmsAeadErrorPublicGenericDetails {
42    pub code: String,
43}
44
45#[derive(Debug, Builder)]
46pub struct KmsAeadSystemError {
47    pub public: KmsAeadErrorPublicGenericDetails,
48    pub message: String,
49    pub root_cause: Option<BoxedError>,
50}
51
52impl Display for KmsAeadSystemError {
53    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
54        write!(
55            f,
56            "KmsAead system/internal error: {:?} / {}",
57            self.public, self.message
58        )
59    }
60}
61
62impl std::error::Error for KmsAeadSystemError {}
63
64#[derive(Debug, Eq, PartialEq, Clone, Builder)]
65pub struct KmsAeadNetworkError {
66    pub public: KmsAeadErrorPublicGenericDetails,
67    pub message: String,
68}
69
70impl Display for KmsAeadNetworkError {
71    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
72        write!(f, "Network error: {:?} / {}", self.public, self.message)
73    }
74}
75
76impl std::error::Error for KmsAeadNetworkError {}
77
78#[derive(Debug, Builder)]
79pub struct KmsAeadEncryptionError {
80    pub public: KmsAeadErrorPublicGenericDetails,
81    pub message: String,
82    pub root_cause: Option<BoxedError>,
83}
84
85impl KmsAeadEncryptionError {
86    pub fn create(code: &str, message: &str) -> KmsAeadError {
87        KmsAeadError::EncryptionError(KmsAeadEncryptionError::new(
88            KmsAeadErrorPublicGenericDetails::new(code.to_string()),
89            message.to_string(),
90        ))
91    }
92}
93
94impl Display for KmsAeadEncryptionError {
95    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
96        write!(
97            f,
98            "KmsAead encryption error: {:?} / {}",
99            self.public, self.message
100        )
101    }
102}
103
104impl std::error::Error for KmsAeadEncryptionError {}
105
106impl From<SystemTimeError> for KmsAeadError {
107    fn from(e: SystemTimeError) -> Self {
108        KmsAeadError::SystemError(
109            KmsAeadSystemError::new(
110                KmsAeadErrorPublicGenericDetails::new("SYSTEM_TIME_ERROR".to_string()),
111                format!("System time error: {e}"),
112            )
113            .with_root_cause(Box::new(e)),
114        )
115    }
116}
117
118#[cfg(feature = "gcp")]
119impl From<gcloud_sdk::error::Error> for KmsAeadError {
120    fn from(e: gcloud_sdk::error::Error) -> Self {
121        KmsAeadError::SystemError(
122            KmsAeadSystemError::new(
123                KmsAeadErrorPublicGenericDetails::new(format!("{:?}", e.kind())),
124                format!("GCloud system error: {e}"),
125            )
126            .with_root_cause(Box::new(e)),
127        )
128    }
129}
130
131#[cfg(feature = "gcp")]
132impl From<gcloud_sdk::tonic::Status> for KmsAeadError {
133    fn from(status: gcloud_sdk::tonic::Status) -> Self {
134        KmsAeadError::NetworkError(KmsAeadNetworkError::new(
135            KmsAeadErrorPublicGenericDetails::new(format!("{:?}", status.code())),
136            format!("{status}"),
137        ))
138    }
139}
140
141#[cfg(feature = "aws")]
142impl<E: Display + Error + Sync + Send + 'static, R: std::fmt::Debug + Sync + Send + 'static>
143    From<aws_sdk_kms::error::SdkError<E, R>> for KmsAeadError
144{
145    fn from(e: aws_sdk_kms::error::SdkError<E, R>) -> Self {
146        KmsAeadError::EncryptionError(
147            KmsAeadEncryptionError::new(
148                KmsAeadErrorPublicGenericDetails::new(format!("{e}")),
149                format!("AWS KMS error: {e}"),
150            )
151            .with_root_cause(Box::new(e)),
152        )
153    }
154}