Remove anyhow usage in tor-client.

This commit is contained in:
Jani Monoses 2021-10-13 10:25:53 +03:00
parent f683d4f3cd
commit 12da3f0771
4 changed files with 65 additions and 5 deletions

View File

@ -25,7 +25,6 @@ tor-persist = { path="../tor-persist", version="0.0.0" }
tor-proto = { path="../tor-proto", version="0.0.0" }
tor-rtcompat = { path="../tor-rtcompat", version="0.0.0" }
anyhow = "1.0.38"
futures = "0.3.13"
tracing = "0.1.26"
thiserror = "1.0.24"

View File

@ -19,7 +19,7 @@ use std::str::FromStr;
use std::sync::{Arc, Weak};
use std::time::Duration;
use anyhow::{anyhow, Context, Result};
use crate::{Error, Result};
use tracing::{debug, error, info, warn};
/// An active client session on the Tor network.
@ -195,7 +195,7 @@ impl<R: Runtime> TorClient<R> {
flags: Option<ConnectPrefs>,
) -> Result<DataStream> {
if addr.to_lowercase().ends_with(".onion") {
return Err(anyhow!("Rejecting .onion address as unsupported."));
return Err(Error::OnionAddressNotSupported);
}
let flags = flags.unwrap_or_default();
@ -224,7 +224,7 @@ impl<R: Runtime> TorClient<R> {
flags: Option<ConnectPrefs>,
) -> Result<Vec<IpAddr>> {
if hostname.to_lowercase().ends_with(".onion") {
return Err(anyhow!("Rejecting .onion address as unsupported."));
return Err(Error::OnionAddressNotSupported);
}
let flags = flags.unwrap_or_default();
@ -296,7 +296,7 @@ impl<R: Runtime> TorClient<R> {
.circmgr
.get_or_launch_exit(dir.as_ref().into(), exit_ports, flags.isolation_group())
.await
.context("Unable to launch circuit")?;
.map_err(|_| Error::Internal("Unable to launch circuit"))?;
drop(dir); // This decreases the refcount on the netdir.
Ok(circ)

View File

@ -0,0 +1,55 @@
//! Declare tor client specific errors.
use thiserror::Error;
use tor_rtcompat::TimeoutError;
/// An error originating from the tor-dirclient crate.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
/// Error while getting a circuit
#[error("Error while getting a circuit {0}")]
CircMgr(#[from] tor_circmgr::Error),
/// Error while getting a circuit
#[error("Directory state error {0}")]
DirMgr(#[from] tor_dirmgr::Error),
/// A protocol error while launching a stream
#[error("Protocol error while launching a stream: {0}")]
Proto(#[from] tor_proto::Error),
/// A protocol error while launching a stream
#[error("Persist error while launching a stream: {0}")]
Persist(#[from] tor_persist::Error),
/// The directory cache took too long to reply to us.
#[error("directory timed out")]
Timeout,
/// Onion services not supported.
#[error("Rejecting .onion address as unsupported.")]
OnionAddressNotSupported,
/// An internal error of some kind that should never occur.
#[error("Internal error: {0}")]
Internal(&'static str),
}
impl From<TimeoutError> for Error {
fn from(_: TimeoutError) -> Self {
Error::Timeout
}
}
impl From<futures::task::SpawnError> for Error {
fn from(_: futures::task::SpawnError) -> Error {
Error::Internal("Couldn't spawn channel reactor")
}
}
impl From<std::net::AddrParseError> for Error {
fn from(_: std::net::AddrParseError) -> Error {
Error::Internal("Couldn't parse IP address")
}
}

View File

@ -115,3 +115,9 @@ pub use tor_circmgr::IsolationToken;
/// This type is a re-export from [`tor_proto::stream::DataStream`];
/// see that crate for its documentation in a more low-level context.
pub use tor_proto::stream::DataStream;
mod err;
pub use err::Error;
/// Result type used by this crate
type Result<T> = std::result::Result<T, Error>;