1
1
use dkn_p2p:: libp2p:: { multiaddr:: Protocol , Multiaddr , PeerId } ;
2
2
use dkn_p2p:: DriaNetworkType ;
3
- use eyre:: Result ;
3
+ use eyre:: { Context , OptionExt , Result } ;
4
4
use std:: fmt:: Debug ;
5
5
6
6
/// The connected RPC node, as per the Star network topology.
@@ -13,23 +13,23 @@ pub struct DriaRPC {
13
13
14
14
impl DriaRPC {
15
15
/// 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 ? ;
20
20
let peer_id = addr
21
21
. iter ( )
22
22
. find_map ( |p| match p {
23
23
Protocol :: P2p ( peer_id) => Some ( peer_id) ,
24
24
_ => None ,
25
25
} )
26
- . expect ( "returned address does not contain a peer id" ) ;
26
+ . ok_or_eyre ( "did not find peer ID within the returned RPC address" ) ? ;
27
27
28
- Self {
28
+ Ok ( Self {
29
29
addr,
30
30
peer_id,
31
31
network,
32
- }
32
+ } )
33
33
}
34
34
}
35
35
@@ -51,7 +51,10 @@ async fn refresh_rpc_addr(network: &DriaNetworkType) -> Result<Multiaddr> {
51
51
52
52
// make the request
53
53
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" ) ?;
55
58
56
59
Ok ( response_body. rpc )
57
60
}
0 commit comments