Skip to content

Commit cd29ea0

Browse files
Stop leaking hickory error types in litep2p public API (#643)
This is what happened in litep2p <= v0.14.3 and forces us now to go to v0.15 because of the changes in hickory error types. Stop leaking so we can tolerate future hickory error type changes. Also mark litep2p `Error` as non-exhaustive, so it can be safely extended in the future.
1 parent faeb7c8 commit cd29ea0

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

src/error.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use std::io::{self, ErrorKind};
3939
// Please note that this error is not propagated directly to the user.
4040
#[allow(clippy::large_enum_variant)]
4141
#[derive(Debug, thiserror::Error)]
42+
#[non_exhaustive]
4243
pub enum Error {
4344
#[error("Peer `{0}` does not exist")]
4445
PeerDoesntExist(PeerId),
@@ -127,9 +128,9 @@ pub enum Error {
127128
#[error("Failed to dial peer immediately")]
128129
ImmediateDialError(#[from] ImmediateDialError),
129130
#[error("Cannot read system DNS config: `{0}`")]
130-
CannotReadSystemDnsConfig(hickory_resolver::net::NetError),
131+
CannotReadSystemDnsConfig(DnsInitError),
131132
#[error("Failed to build DNS resolver: `{0}`")]
132-
DnsResolverInit(hickory_resolver::net::NetError),
133+
DnsResolverInit(DnsInitError),
133134
}
134135

135136
/// Error type for address parsing.
@@ -428,6 +429,22 @@ pub enum DnsError {
428429
IpVersionMismatch,
429430
}
430431

432+
/// Error during DNS resolver initialization.
433+
#[derive(Debug, thiserror::Error)]
434+
#[error("{0}")]
435+
pub struct DnsInitError(String);
436+
437+
impl DnsInitError {
438+
/// Erase a resolver error into an opaque [`DnsInitError`] to stop leaking hickory error types
439+
/// into public litep2p API.
440+
///
441+
/// Takes `impl Display` rather than a concrete type because `read_system_conf` returns a
442+
/// different error type per target OS.
443+
pub(crate) fn new(error: impl std::fmt::Display) -> Self {
444+
Self(error.to_string())
445+
}
446+
}
447+
431448
impl From<Multihash> for Error {
432449
fn from(hash: Multihash) -> Self {
433450
Error::AddressError(AddressError::InvalidPeerId(hash))

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
use crate::{
3333
addresses::PublicAddresses,
3434
config::Litep2pConfig,
35-
error::DialError,
35+
error::{DialError, DnsInitError},
3636
protocol::{
3737
libp2p::{bitswap::Bitswap, identify::Identify, kademlia::Kademlia, ping::Ping},
3838
mdns::Mdns,
@@ -164,7 +164,7 @@ impl Litep2p {
164164

165165
let (resolver_config, mut resolver_opts) = if litep2p_config.use_system_dns_config {
166166
hickory_resolver::system_conf::read_system_conf()
167-
.map_err(Error::CannotReadSystemDnsConfig)?
167+
.map_err(|error| Error::CannotReadSystemDnsConfig(DnsInitError::new(error)))?
168168
} else {
169169
(
170170
ResolverConfig::udp_and_tcp(&GOOGLE),
@@ -177,7 +177,7 @@ impl Litep2p {
177177
TokioResolver::builder_with_config(resolver_config, TokioRuntimeProvider::default())
178178
.with_options(resolver_opts)
179179
.build()
180-
.map_err(Error::DnsResolverInit)?,
180+
.map_err(|error| Error::DnsResolverInit(DnsInitError::new(error)))?,
181181
);
182182

183183
let supported_transports = Self::supported_transports(&litep2p_config);

0 commit comments

Comments
 (0)