Skip to content

Commit 95a237c

Browse files
committed
Improve error handling
1 parent 237a22c commit 95a237c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub async fn main() -> anyhow::Result<()> {
229229
log::info!("IP address changed: IPv4={ipv4}, IPv6={ipv6}. Updating..");
230230
}
231231
(ipv4, ipv6) => {
232-
log::debug!("IP address no change: IPv4={ipv4:?}, IPv6={ipv6:?}");
232+
log::debug!("IP address no change: IPv4={ipv4:?}, IPv6={ipv6:?}");
233233
}
234234
}
235235

@@ -403,9 +403,11 @@ impl IpService {
403403
}
404404

405405
pub fn get_current_local_ips() -> Option<(Option<Ipv4Addr>, Option<Ipv6Addr>)> {
406+
use local_ip_address::Error;
406407
let (ipv4, ipv6) = match local_ip_address::local_ip() {
407408
Ok(IpAddr::V4(ip)) => (Some(ip), None),
408409
Ok(IpAddr::V6(ip)) => (None, Some(ip)),
410+
Err(Error::LocalIpAddressNotFound) => (None, None),
409411
Err(err) => {
410412
log::error!(
411413
"{:#}",
@@ -419,9 +421,12 @@ pub fn get_current_local_ips() -> Option<(Option<Ipv4Addr>, Option<Ipv6Addr>)> {
419421
} else {
420422
match local_ip_address::local_ipv6() {
421423
Ok(IpAddr::V6(ip)) => Some(ip),
422-
Ok(IpAddr::V4(_)) => None,
424+
Ok(IpAddr::V4(_)) | Err(Error::LocalIpAddressNotFound) => None,
423425
Err(err) => {
424-
log::error!("{err:?}");
426+
log::error!(
427+
"{:#}",
428+
anyhow!(err).context("failed to query the system IP address")
429+
);
425430
None
426431
}
427432
}

src/upnp.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,15 @@ impl UPnPIpService {
247247
let ipv6 = if ipv6.is_some() {
248248
ipv6
249249
} else {
250-
self.get_current_external_ipv6().await?
250+
match self.get_current_external_ipv6().await {
251+
Ok(v) => v,
252+
Err(err) if ipv4.is_none() && ipv6.is_none() => return Err(err),
253+
Err(err) => {
254+
// Do not return the error if we have obtained an IP already.
255+
log::debug!("{:#}", err.context("UPnP request failed"));
256+
None
257+
}
258+
}
251259
};
252260

253261
Ok((ipv4, ipv6))

0 commit comments

Comments
 (0)