tor-cell: convert BytesErr to a struct variant

This commit is contained in:
Nick Mathewson 2022-06-22 15:11:18 -04:00
parent c3da82fc2d
commit 0369995364
4 changed files with 22 additions and 7 deletions

View File

@ -0,0 +1 @@
BREAKING: incompatible change to error variant

View File

@ -83,7 +83,10 @@ impl ChannelCodec {
pub fn decode_cell(&mut self, src: &mut BytesMut) -> crate::Result<Option<ChanCell>> {
/// Wrap `be` as an appropriate type.
fn wrap_err(be: tor_bytes::Error) -> crate::Error {
crate::Error::BytesErr(be, "channel cell")
crate::Error::BytesErr {
err: be,
parsed: "channel cell",
}
}
if src.len() < 7 {

View File

@ -919,8 +919,10 @@ impl Certs {
.cert_body(tp)
.ok_or_else(|| crate::Error::ChanProto(format!("Missing {} certificate", tp)))?;
let cert = tor_cert::Ed25519Cert::decode(body)
.map_err(|be| crate::Error::BytesErr(be, "ed25519 certificate"))?;
let cert = tor_cert::Ed25519Cert::decode(body).map_err(|be| crate::Error::BytesErr {
err: be,
parsed: "ed25519 certificate",
})?;
if cert.peek_cert_type() != tp {
return Err(crate::Error::ChanProto(format!(
"Found a {} certificate labeled as {}",

View File

@ -12,8 +12,14 @@ use tor_error::{ErrorKind, HasKind};
pub enum Error {
/// An error that occurred in the tor_bytes crate while decoding an
/// object.
#[error("Error while parsing {1}")]
BytesErr(#[source] tor_bytes::Error, &'static str),
#[error("Error while parsing {parsed}")]
BytesErr {
/// The error that occurred.
#[source]
err: tor_bytes::Error,
/// The thing that was being parsed.
parsed: &'static str,
},
/// There was a programming error somewhere in the code.
#[error("Internal programming error")]
Internal(tor_error::Bug),
@ -34,8 +40,11 @@ impl HasKind for Error {
use Error as E;
use ErrorKind as EK;
match self {
E::BytesErr(ByE::Truncated, _) => EK::Internal,
E::BytesErr(_, _) => EK::TorProtocolViolation,
E::BytesErr {
err: ByE::Truncated,
..
} => EK::Internal,
E::BytesErr { .. } => EK::TorProtocolViolation,
E::Internal(_) => EK::Internal,
E::ChanProto(_) => EK::TorProtocolViolation,
E::BadStreamAddress => EK::BadApiUsage,