tor-bytes: Use InternalError.

This crate's Error type is too low-level to have an ErrorKind, but
it does make sense to use InternalError for the internal errors
here.
This commit is contained in:
Nick Mathewson 2022-02-09 14:39:04 -05:00
parent 8748a8b126
commit 7670a26d9c
4 changed files with 19 additions and 3 deletions

1
Cargo.lock generated
View File

@ -2885,6 +2885,7 @@ dependencies = [
"hex-literal", "hex-literal",
"signature", "signature",
"thiserror", "thiserror",
"tor-error",
"tor-llcrypto", "tor-llcrypto",
] ]

View File

@ -12,6 +12,7 @@ repository = "https://gitlab.torproject.org/tpo/core/arti.git/"
[dependencies] [dependencies]
tor-llcrypto = { path = "../tor-llcrypto", version = "0.0.3"} tor-llcrypto = { path = "../tor-llcrypto", version = "0.0.3"}
tor-error = { path="../tor-error", version = "0.0.1" }
arrayref = "0.3" arrayref = "0.3"
bytes = "1" bytes = "1"

View File

@ -3,7 +3,7 @@
use thiserror::Error; use thiserror::Error;
/// Error type for decoding Tor objects from bytes. /// Error type for decoding Tor objects from bytes.
#[derive(Error, Debug, PartialEq, Eq, Clone)] #[derive(Error, Debug, Clone)]
#[non_exhaustive] #[non_exhaustive]
pub enum Error { pub enum Error {
/// Tried to read something, but we didn't find enough bytes. /// Tried to read something, but we didn't find enough bytes.
@ -25,5 +25,18 @@ pub enum Error {
/// We use this one in lieu of calling assert() and expect() and /// We use this one in lieu of calling assert() and expect() and
/// unwrap() from within parsing code. /// unwrap() from within parsing code.
#[error("internal programming error")] #[error("internal programming error")]
Internal, Internal(#[from] tor_error::InternalError),
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
use Error::*;
match (self, other) {
(Truncated, Truncated) => true,
(ExtraneousBytes, ExtraneousBytes) => true,
(BadMessage(a), BadMessage(b)) => a == b,
// notably, this means that an internal error is equal to nothing, not even itself.
(_, _) => false,
}
}
} }

View File

@ -240,7 +240,8 @@ mod rsa_impls {
impl Readable for RsaIdentity { impl Readable for RsaIdentity {
fn take_from(b: &mut Reader<'_>) -> Result<Self> { fn take_from(b: &mut Reader<'_>) -> Result<Self> {
let m = b.take(RSA_ID_LEN)?; let m = b.take(RSA_ID_LEN)?;
Ok(RsaIdentity::from_bytes(m).expect("take gave wrong length")) RsaIdentity::from_bytes(m)
.ok_or_else(|| tor_error::internal!("wrong number of bytes from take").into())
} }
} }
} }