feat: add a new variant in Error

Add `Other` variant in Error to express any other error. The main
intention for this is to use it to hold errors throw from underlying
custom crypto provider or pki provider.

A new unit struct `OtherError` is added to properly implement
`PartialEq`.
This commit is contained in:
Yuxiang Cao 2023-11-06 11:16:38 -08:00 committed by Joe Birr-Pixton
parent 3355e06f97
commit 1f0e6ad626
1 changed files with 41 additions and 1 deletions

View File

@ -94,6 +94,15 @@ pub enum Error {
/// The `max_fragment_size` value supplied in configuration was too small,
/// or too large.
BadMaxFragmentSize,
/// Any other error.
///
/// This variant should only be used when the error is not better described by a more
/// specific variant. For example, if a custom crypto provider returns a
/// provider specific error.
///
/// Enums holding this variant will never compare equal to each other.
Other(OtherError),
}
/// A corrupt TLS message payload that resulted in an error.
@ -514,6 +523,7 @@ impl fmt::Display for Error {
write!(f, "the supplied max_fragment_size was too small or large")
}
Self::General(ref err) => write!(f, "unexpected error: {}", err),
Self::Other(ref err) => write!(f, "other error: {:?}", err),
}
}
}
@ -533,10 +543,31 @@ impl From<rand::GetRandomFailed> for Error {
}
}
/// Any other error that cannot be expressed by a more specific [`Error`] variant.
///
/// For example, an `OtherError` could be produced by a custom crypto provider
/// exposing a provider specific error.
///
/// Enums holding this type will never compare equal to each other.
#[derive(Debug, Clone)]
pub struct OtherError(pub Arc<dyn StdError + Send + Sync>);
impl PartialEq<Self> for OtherError {
fn eq(&self, _other: &Self) -> bool {
false
}
}
impl From<OtherError> for Error {
fn from(value: OtherError) -> Self {
Self::Other(value)
}
}
#[cfg(test)]
mod tests {
use super::{Error, InvalidMessage};
use crate::error::CertRevocationListError;
use crate::error::{CertRevocationListError, OtherError};
#[test]
fn certificate_error_equality() {
@ -580,6 +611,14 @@ mod tests {
assert_ne!(BadSignature, InvalidCrlNumber);
}
#[test]
fn other_error_equality() {
let other_error = OtherError(alloc::sync::Arc::from(Box::from("")));
assert_ne!(other_error, other_error);
let other: Error = other_error.into();
assert_ne!(other, other);
}
#[test]
fn smoke() {
use crate::enums::{AlertDescription, ContentType, HandshakeType};
@ -608,6 +647,7 @@ mod tests {
Error::NoApplicationProtocol,
Error::BadMaxFragmentSize,
Error::InvalidCertRevocationList(CertRevocationListError::BadSignature),
Error::Other(OtherError(alloc::sync::Arc::from(Box::from("")))),
];
for err in all {