Skip to content

Commit 96a7936

Browse files
committed
panic on failed API request
1 parent 95f5027 commit 96a7936

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

compute/src/node/diagnostic.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,19 @@ impl DriaComputeNode {
111111
"Could not dial to RPC at: {}: {err:?}\nWill get a new RPC node.",
112112
self.dria_rpc.addr,
113113
);
114-
self.dria_rpc = DriaRPC::new(self.dria_rpc.network).await;
115-
116-
// now dial this new RPC again
117-
if let Err(err) = self
118-
.dial_with_timeout(self.dria_rpc.peer_id, self.dria_rpc.addr.clone())
119-
.await
120-
{
121-
// worst-case we cant dial this one too, just leave it for the next diagnostic
122-
log::error!("Could not dial the new RPC: {err:?}\nWill try again in the next diagnostic refresh.");
114+
if let Ok(new_rpc) = DriaRPC::new(self.dria_rpc.network).await {
115+
self.dria_rpc = new_rpc;
116+
117+
// now dial this new RPC again
118+
if let Err(err) = self
119+
.dial_with_timeout(self.dria_rpc.peer_id, self.dria_rpc.addr.clone())
120+
.await
121+
{
122+
// worst-case we cant dial this one too, just leave it for the next diagnostic
123+
log::error!("Could not dial the new RPC: {err:?}\nWill try again in the next diagnostic refresh.");
124+
}
125+
} else {
126+
log::error!("Could not get a new RPC node!\nWill try again in the next diagnostic refresh.");
123127
}
124128
} else {
125129
log::info!("Successfully dialled to RPC at: {}", self.dria_rpc.addr);

compute/src/node/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ impl DriaComputeNode {
7171
let keypair = secret_to_keypair(&config.secret_key);
7272

7373
// get available rpc node
74-
let dria_nodes = DriaRPC::new(config.network_type).await;
74+
let dria_nodes = DriaRPC::new(config.network_type)
75+
.await
76+
.expect("could not get RPC to connect to");
7577

7678
// we are using the major.minor version as the P2P version
7779
// so that patch versions do not interfere with the protocol

compute/src/utils/rpc.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use dkn_p2p::libp2p::{multiaddr::Protocol, Multiaddr, PeerId};
22
use dkn_p2p::DriaNetworkType;
3-
use eyre::Result;
3+
use eyre::{Context, OptionExt, Result};
44
use std::fmt::Debug;
55

66
/// The connected RPC node, as per the Star network topology.
@@ -13,23 +13,23 @@ pub struct DriaRPC {
1313

1414
impl DriaRPC {
1515
/// Creates a new `AvailableNodes` struct for the given network type.
16-
pub async fn new(network: DriaNetworkType) -> Self {
17-
let addr = refresh_rpc_addr(&network)
18-
.await
19-
.expect("could not get RPC address");
16+
///
17+
/// Will panic if anything goes wrong.
18+
pub async fn new(network: DriaNetworkType) -> Result<Self> {
19+
let addr = refresh_rpc_addr(&network).await?;
2020
let peer_id = addr
2121
.iter()
2222
.find_map(|p| match p {
2323
Protocol::P2p(peer_id) => Some(peer_id),
2424
_ => None,
2525
})
26-
.expect("returned address does not contain a peer id");
26+
.ok_or_eyre("did not find peer ID within the returned RPC address")?;
2727

28-
Self {
28+
Ok(Self {
2929
addr,
3030
peer_id,
3131
network,
32-
}
32+
})
3333
}
3434
}
3535

@@ -51,7 +51,10 @@ async fn refresh_rpc_addr(network: &DriaNetworkType) -> Result<Multiaddr> {
5151

5252
// make the request
5353
let response = reqwest::get(url).await?;
54-
let response_body = response.json::<DriaNodesApiResponse>().await?;
54+
let response_body = response
55+
.json::<DriaNodesApiResponse>()
56+
.await
57+
.wrap_err("could not parse API response")?;
5558

5659
Ok(response_body.rpc)
5760
}

0 commit comments

Comments
 (0)