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