Skip to main content

ironcore_alloy/
errors.rs

1pub use crate::tenant_security_client::errors::{
2    KmsError, SecurityEventError, ServiceError, TenantSecretError, TenantSecurityProxyError,
3};
4use crate::vector::crypto::{
5    DecryptError as VectorDecryptError, EncryptError as VectorEncryptError,
6};
7
8/// Errors related to IronCore Alloy SDK
9#[derive(Debug, uniffi::Error, PartialEq, Eq, Clone)]
10pub enum AlloyError {
11    /// Error while loading configuration.
12    InvalidConfiguration { msg: String },
13    /// Error with key used
14    InvalidKey { msg: String },
15    /// Error with user input
16    InvalidInput { msg: String },
17    /// Errors while encrypting
18    EncryptError { msg: String },
19    /// Errors while decrypting
20    DecryptError { msg: String },
21    /// Error when parsing encryption headers/metadata
22    ProtobufError { msg: String },
23    /// Error when making a request to the TSP
24    RequestError { msg: String },
25    /// Error converting request data to JSON
26    SerdeJsonError { msg: String },
27    /// Error directly from the TSP. See https://ironcorelabs.com/docs/saas-shield/tenant-security-proxy/errors/
28    /// for details about these error codes.
29    TspError {
30        error: TenantSecurityProxyError,
31        http_code: u16,
32        tsp_code: u16,
33        msg: String,
34    },
35}
36
37impl std::fmt::Display for AlloyError {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            AlloyError::InvalidConfiguration { msg } => {
41                write!(f, "Invalid configuration: '{msg}'")
42            }
43            AlloyError::InvalidKey { msg } => write!(f, "Invalid key: '{msg}'"),
44            AlloyError::InvalidInput { msg } => write!(f, "Invalid input: '{msg}'"),
45            AlloyError::EncryptError { msg } => write!(f, "Encrypt error: '{msg}'"),
46            AlloyError::DecryptError { msg } => write!(f, "Decrypt error: '{msg}'"),
47            AlloyError::ProtobufError { msg } => write!(f, "Protobuf error: '{msg}'"),
48            AlloyError::RequestError { msg } => write!(f, "Request error: '{msg}'"),
49            AlloyError::SerdeJsonError { msg } => write!(f, "Serde JSON error: '{msg}'"),
50            AlloyError::TspError {
51                error,
52                tsp_code,
53                http_code,
54                msg,
55            } => write!(
56                f,
57                "TSP error variant: '{error}', HTTP code: {http_code}, TSP code: {tsp_code}, Message: {msg}"
58            ),
59        }
60    }
61}
62impl From<VectorEncryptError> for AlloyError {
63    fn from(value: VectorEncryptError) -> Self {
64        match value {
65            VectorEncryptError::InvalidKey(s) => Self::InvalidKey { msg: s },
66            VectorEncryptError::OverflowError => Self::InvalidInput {
67                msg: value.to_string(),
68            },
69        }
70    }
71}
72impl From<VectorDecryptError> for AlloyError {
73    fn from(value: VectorDecryptError) -> Self {
74        match value {
75            VectorDecryptError::InvalidKey(s) => Self::InvalidKey { msg: s },
76            VectorDecryptError::InvalidAuthHash => Self::InvalidInput {
77                msg: "Invalid authentication hash".to_string(),
78            },
79        }
80    }
81}
82impl From<ironcore_documents::Error> for AlloyError {
83    fn from(value: ironcore_documents::Error) -> Self {
84        match value {
85            ironcore_documents::Error::EdocTooShort(_)
86            | ironcore_documents::Error::HeaderParseErr(_)
87            | ironcore_documents::Error::InvalidVersion(_)
88            | ironcore_documents::Error::NoIronCoreMagic
89            | ironcore_documents::Error::SpecifiedLengthTooLong(_)
90            | ironcore_documents::Error::HeaderLengthOverflow(_)
91            | ironcore_documents::Error::EdekTypeError(_)
92            | ironcore_documents::Error::PayloadTypeError(_)
93            | ironcore_documents::Error::KeyIdHeaderTooShort(_)
94            | ironcore_documents::Error::KeyIdHeaderMalformed(_) => AlloyError::InvalidInput {
95                msg: value.to_string(),
96            },
97            ironcore_documents::Error::ProtoSerializationErr(msg) => {
98                AlloyError::ProtobufError { msg }
99            }
100            ironcore_documents::Error::EncryptError(msg) => AlloyError::EncryptError { msg },
101            ironcore_documents::Error::DecryptError(msg) => AlloyError::DecryptError { msg },
102        }
103    }
104}
105impl From<protobuf::Error> for AlloyError {
106    fn from(value: protobuf::Error) -> Self {
107        AlloyError::ProtobufError {
108            msg: value.to_string(),
109        }
110    }
111}
112impl From<reqwest::Error> for AlloyError {
113    fn from(e: reqwest::Error) -> Self {
114        Self::RequestError { msg: e.to_string() }
115    }
116}
117impl From<serde_json::Error> for AlloyError {
118    fn from(e: serde_json::Error) -> Self {
119        Self::SerdeJsonError { msg: e.to_string() }
120    }
121}
122impl std::error::Error for AlloyError {}