Skip to content

Commit

Permalink
feat: add a new variant in Error
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Taowyoo authored and ctz committed Nov 7, 2023
1 parent 3355e06 commit 1f0e6ad
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion rustls/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
}
}
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1f0e6ad

Please sign in to comment.