Skip to content

Commit a28d28f

Browse files
committed
refactor: replace panic statements with appropriate build error
1 parent 3ceff37 commit a28d28f

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

bindings/ldk_node.udl

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ interface Builder {
7878
void set_chain_source_esplora(string server_url, EsploraSyncConfig? config);
7979
void set_chain_source_electrum(string server_url, ElectrumSyncConfig? config);
8080
void set_chain_source_bitcoind_rpc(string rpc_host, u16 rpc_port, string rpc_user, string rpc_password);
81+
[Throws=BuildError]
8182
void set_chain_source_bitcoind_rest(string rest_host, u16 rest_port);
8283
void set_gossip_source_p2p();
8384
void set_gossip_source_rgs(string rgs_server_url);
@@ -338,6 +339,8 @@ enum BuildError {
338339
"WalletSetupFailed",
339340
"LoggerSetupFailed",
340341
"NetworkMismatch",
342+
"MissingBitcoindRpcConfig",
343+
"InvalidChainSourceConfig",
341344
};
342345

343346
[Trait]

src/builder.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ pub enum BuildError {
185185
LoggerSetupFailed,
186186
/// The given network does not match the node's previously configured network.
187187
NetworkMismatch,
188+
/// The RPC configuration for Bitcoin Core is incomplete or missing.
189+
///
190+
/// This error occurs in two scenarios:
191+
/// 1. When the RPC configuration fields (host, port, user, password) are empty or invalid.
192+
/// 2. When attempting to configure REST synchronization before setting up RPC configuration.
193+
MissingBitcoindRpcConfig,
194+
/// Invalid chain source configuration.
195+
///
196+
/// This error occurs when attempting to set Bitcoin Core REST configuration on a non-Bitcoind
197+
/// chain source (such as when the chain source is already configured for Esplora/Electrum).
198+
InvalidChainSourceConfig,
188199
}
189200

190201
impl fmt::Display for BuildError {
@@ -212,6 +223,12 @@ impl fmt::Display for BuildError {
212223
Self::NetworkMismatch => {
213224
write!(f, "Given network does not match the node's previously configured network.")
214225
},
226+
Self::MissingBitcoindRpcConfig => {
227+
write!(f, "Failed to configure RPC client for ChainSource::Bitcoind.")
228+
},
229+
Self::InvalidChainSourceConfig => {
230+
write!(f, "Given REST client configuration is invalid for the ChainSource variant.")
231+
},
215232
}
216233
}
217234
}
@@ -343,7 +360,7 @@ impl NodeBuilder {
343360
/// * `rest_host`, `rest_port` - Required parameters for the Bitcoin Core REST connection.
344361
pub fn set_chain_source_bitcoind_rest(
345362
&mut self, rest_host: String, rest_port: u16,
346-
) -> &mut Self {
363+
) -> Result<&mut Self, BuildError> {
347364
let rest_client_config = BitcoindRestSyncClientConfig { rest_host, rest_port };
348365

349366
match &mut self.chain_data_source_config {
@@ -359,20 +376,22 @@ impl NodeBuilder {
359376
|| rpc_user.is_empty()
360377
|| rpc_password.is_empty()
361378
{
362-
panic!("RPC configuration is incomplete. Must fully configure via RPC first.");
379+
return Err(BuildError::MissingBitcoindRpcConfig);
363380
}
364381

365382
*sync_client_config = Some(rest_client_config);
366383
},
367-
Some(cs) => {
368-
panic!("This option is only valid for ChainSource::Bitcoind. Current chain source config: {cs:?}")
384+
Some(_) => {
385+
// This option is only valid for ChainSource::Bitcoind.
386+
return Err(BuildError::InvalidChainSourceConfig);
369387
},
370388
None => {
371-
panic!("Must configure via RPC first.")
389+
// Must configure via RPC first.
390+
return Err(BuildError::MissingBitcoindRpcConfig);
372391
},
373392
}
374393

375-
self
394+
Ok(self)
376395
}
377396
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer
378397
/// network.
@@ -810,8 +829,10 @@ impl ArcedNodeBuilder {
810829
///
811830
/// ## Parameters:
812831
/// * `rest_host`, `rest_port` - Required parameters for the Bitcoin Core REST connection.
813-
pub fn set_chain_source_bitcoind_rest(&self, rest_host: String, rest_port: u16) {
814-
self.inner.write().unwrap().set_chain_source_bitcoind_rest(rest_host, rest_port);
832+
pub fn set_chain_source_bitcoind_rest(
833+
&self, rest_host: String, rest_port: u16,
834+
) -> Result<(), BuildError> {
835+
self.inner.write().unwrap().set_chain_source_bitcoind_rest(rest_host, rest_port).map(|_| ())
815836
}
816837

817838
/// Configures the [`Node`] instance to source its gossip data from the Lightning peer-to-peer

tests/common/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,8 @@ pub(crate) fn setup_node(
335335
let rpc_password = values.password;
336336
let rest_host = bitcoind.params.rpc_socket.ip().to_string();
337337
let rest_port = bitcoind.params.rpc_socket.port();
338-
builder
339-
.set_chain_source_bitcoind_rpc(rpc_host, rpc_port, rpc_user, rpc_password)
340-
.set_chain_source_bitcoind_rest(rest_host, rest_port);
338+
builder.set_chain_source_bitcoind_rpc(rpc_host, rpc_port, rpc_user, rpc_password);
339+
builder.set_chain_source_bitcoind_rest(rest_host, rest_port).unwrap();
341340
},
342341
}
343342

0 commit comments

Comments
 (0)