From 0369995364c04f9476dbedd55d50caf915017ea7 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 22 Jun 2022 15:11:18 -0400 Subject: [PATCH] tor-cell: convert BytesErr to a struct variant --- crates/tor-cell/semver.md | 1 + crates/tor-cell/src/chancell/codec.rs | 5 ++++- crates/tor-cell/src/chancell/msg.rs | 6 ++++-- crates/tor-cell/src/err.rs | 17 +++++++++++++---- 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 crates/tor-cell/semver.md diff --git a/crates/tor-cell/semver.md b/crates/tor-cell/semver.md new file mode 100644 index 000000000..975fe642a --- /dev/null +++ b/crates/tor-cell/semver.md @@ -0,0 +1 @@ +BREAKING: incompatible change to error variant diff --git a/crates/tor-cell/src/chancell/codec.rs b/crates/tor-cell/src/chancell/codec.rs index 862cb6827..fda5d1061 100644 --- a/crates/tor-cell/src/chancell/codec.rs +++ b/crates/tor-cell/src/chancell/codec.rs @@ -83,7 +83,10 @@ impl ChannelCodec { pub fn decode_cell(&mut self, src: &mut BytesMut) -> crate::Result> { /// 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 { diff --git a/crates/tor-cell/src/chancell/msg.rs b/crates/tor-cell/src/chancell/msg.rs index b3e21e2c7..8d8c0a77d 100644 --- a/crates/tor-cell/src/chancell/msg.rs +++ b/crates/tor-cell/src/chancell/msg.rs @@ -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 {}", diff --git a/crates/tor-cell/src/err.rs b/crates/tor-cell/src/err.rs index eaff4f7a8..8dab9db52 100644 --- a/crates/tor-cell/src/err.rs +++ b/crates/tor-cell/src/err.rs @@ -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,