|
| 1 | +use std::backtrace::Backtrace; |
| 2 | + |
| 3 | +#[derive(Clone, PartialEq, Eq, Debug)] |
| 4 | +#[non_exhaustive] |
| 5 | +/// Individual error types for ESDK |
| 6 | +pub enum ErrorKind { |
| 7 | + /// Higher level ESDK errors |
| 8 | + Esdk(String), |
| 9 | + /// Error in serializing or deserializing the data |
| 10 | + SerializationError(String), |
| 11 | + /// Low level cryptographic error from `aws_mpl_primitives` |
| 12 | + CryptographicError(String), |
| 13 | + /// Low level cryptographic error from `aws_mpl_rs::aws_cryptography_primitives` |
| 14 | + PrimitivesError(aws_mpl_rs::deps::aws_cryptography_primitives::types::error::Error), |
| 15 | + /// Mid level cryptographic error from `aws_mpl_rs` |
| 16 | + MplError(Box<aws_mpl_rs::types::error::Error>), |
| 17 | + /// Malformed input. No cryptography has been attempted. |
| 18 | + ValidationError(String), |
| 19 | +} |
| 20 | +#[derive(Debug)] |
| 21 | +#[non_exhaustive] |
| 22 | +/// Base error type for ESDK |
| 23 | +pub struct Error { |
| 24 | + /// Error type |
| 25 | + pub kind: ErrorKind, |
| 26 | + /// Backtrace captured when error was encountered. |
| 27 | + /// For `MplError` and `PrimitivesError`, the backtrace is not captured until the ESDK level |
| 28 | + pub backtrace: Backtrace, |
| 29 | + /// The Error causing the Error, if any. |
| 30 | + pub cause: Option<Box<dyn std::error::Error + Send + Sync + 'static>>, |
| 31 | +} |
| 32 | + |
| 33 | +impl std::fmt::Display for Error { |
| 34 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 35 | + match &self.kind { |
| 36 | + ErrorKind::Esdk(message) => write!(f, "Esdk Error {message}"), |
| 37 | + ErrorKind::SerializationError(message) => write!(f, "Serialization Error {message}"), |
| 38 | + ErrorKind::CryptographicError(message) => write!(f, "Cryptographic Error {message}"), |
| 39 | + ErrorKind::PrimitivesError(message) => write!(f, "Primitives Error {message}"), |
| 40 | + ErrorKind::MplError(message) => write!(f, "MPL Error {message}"), |
| 41 | + ErrorKind::ValidationError(message) => write!(f, "Validation Error {message}"), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +pub(crate) fn val_err(msg: impl Into<String>) -> Error { |
| 47 | + Error { |
| 48 | + kind: ErrorKind::ValidationError(msg.into()), |
| 49 | + backtrace: Backtrace::capture(), |
| 50 | + cause: None, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl std::error::Error for Error { |
| 55 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 56 | + match &self.cause { |
| 57 | + Some(cause) => Some(cause.as_ref()), |
| 58 | + None => None, |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl From<aws_mpl_rs::deps::aws_cryptography_primitives::types::error::Error> for Error { |
| 64 | + fn from(item: aws_mpl_rs::deps::aws_cryptography_primitives::types::error::Error) -> Self { |
| 65 | + Self { |
| 66 | + kind: ErrorKind::PrimitivesError(item), |
| 67 | + backtrace: Backtrace::capture(), |
| 68 | + cause: None, |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl From<aws_mpl_rs::types::error::Error> for Error { |
| 74 | + #[track_caller] |
| 75 | + fn from(item: aws_mpl_rs::types::error::Error) -> Self { |
| 76 | + Self { |
| 77 | + kind: ErrorKind::MplError(Box::new(item)), |
| 78 | + backtrace: Backtrace::capture(), |
| 79 | + cause: None, |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl From<aws_mpl_primitives::Error> for Error { |
| 85 | + #[track_caller] |
| 86 | + fn from(item: aws_mpl_primitives::Error) -> Self { |
| 87 | + Self { |
| 88 | + kind: ErrorKind::CryptographicError(item.msg), |
| 89 | + backtrace: item.backtrace, |
| 90 | + cause: None, |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl From<aws_smithy_types::error::operation::BuildError> for Error { |
| 96 | + fn from(item: aws_smithy_types::error::operation::BuildError) -> Self { |
| 97 | + Self { |
| 98 | + kind: ErrorKind::ValidationError(format!("{item:?}")), |
| 99 | + backtrace: Backtrace::capture(), |
| 100 | + cause: Some(Box::new(item)), |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl From<&str> for Error { |
| 106 | + fn from(item: &str) -> Self { |
| 107 | + Self { |
| 108 | + kind: ErrorKind::Esdk(item.to_string()), |
| 109 | + backtrace: Backtrace::capture(), |
| 110 | + cause: None, |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl From<String> for Error { |
| 116 | + fn from(item: String) -> Self { |
| 117 | + Self { |
| 118 | + kind: ErrorKind::Esdk(item), |
| 119 | + backtrace: Backtrace::capture(), |
| 120 | + cause: None, |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +impl From<derive_builder::UninitializedFieldError> for Error { |
| 126 | + fn from(item: derive_builder::UninitializedFieldError) -> Self { |
| 127 | + Self { |
| 128 | + kind: ErrorKind::ValidationError(format!("{item}")), |
| 129 | + backtrace: Backtrace::capture(), |
| 130 | + cause: None, |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments