diff --git a/crates/client-api/src/lib.rs b/crates/client-api/src/lib.rs index 9cb494691ef..19c3cb79eea 100644 --- a/crates/client-api/src/lib.rs +++ b/crates/client-api/src/lib.rs @@ -283,7 +283,7 @@ pub trait ControlStateReadAccess { // Replicas async fn get_replica_by_id(&self, id: u64) -> anyhow::Result>; async fn get_replicas(&self) -> anyhow::Result>; - async fn get_leader_replica_by_database(&self, database_id: u64) -> Option; + async fn get_leader_replica_by_database(&self, database_id: u64) -> anyhow::Result>; // Energy async fn get_energy_balance(&self, identity: &Identity) -> anyhow::Result>; @@ -382,7 +382,7 @@ impl ControlStateReadAc (**self).get_replicas().await } - async fn get_leader_replica_by_database(&self, database_id: u64) -> Option { + async fn get_leader_replica_by_database(&self, database_id: u64) -> anyhow::Result> { (**self).get_leader_replica_by_database(database_id).await } diff --git a/crates/client-api/src/routes/database.rs b/crates/client-api/src/routes/database.rs index 20e6196c78b..1efb06005e7 100644 --- a/crates/client-api/src/routes/database.rs +++ b/crates/client-api/src/routes/database.rs @@ -461,7 +461,11 @@ where // even if we can't get hold of a running [ModuleHost]. Err(e) => { warn!("could not obtain leader host for module logs: {e:#}"); - let Some(replica) = worker_ctx.get_leader_replica_by_database(database.id).await else { + let Some(replica) = worker_ctx + .get_leader_replica_by_database(database.id) + .await + .map_err(log_and_500)? + else { return Err(MISDIRECTED.into()); }; let logs_dir = worker_ctx.module_logs_dir(replica.id); diff --git a/crates/standalone/src/control_db.rs b/crates/standalone/src/control_db.rs index 8333695a42a..b95e8acf91e 100644 --- a/crates/standalone/src/control_db.rs +++ b/crates/standalone/src/control_db.rs @@ -431,11 +431,11 @@ impl ControlDb { Ok(None) } - pub fn get_leader_replica_by_database(&self, database_id: u64) -> Option { - self.get_replicas() - .unwrap() + pub fn get_leader_replica_by_database(&self, database_id: u64) -> Result> { + Ok(self + .get_replicas()? .into_iter() - .find(|instance| instance.database_id == database_id && instance.leader) + .find(|instance| instance.database_id == database_id && instance.leader)) } pub fn get_replicas_by_database(&self, database_id: u64) -> Result> { @@ -463,7 +463,7 @@ impl ControlDb { let id = self.db.generate_id()?; replica.id = id; - let buf = bsatn::to_vec(&replica).unwrap(); + let buf = bsatn::to_vec(&replica)?; tree.insert(id.to_be_bytes(), buf)?; @@ -482,7 +482,7 @@ impl ControlDb { let scan_key: &[u8] = b""; for result in tree.range(scan_key..) { let (_key, value) = result?; - let node = bsatn::from_slice(&value[..]).unwrap(); + let node = bsatn::from_slice(&value[..])?; nodes.push(node); } Ok(nodes) @@ -506,7 +506,7 @@ impl ControlDb { let id = self.db.generate_id()?; node.id = id; - let buf = bsatn::to_vec(&node).unwrap(); + let buf = bsatn::to_vec(&node)?; tree.insert(id.to_be_bytes(), buf)?; diff --git a/crates/standalone/src/lib.rs b/crates/standalone/src/lib.rs index 6c4e61dc9c8..4776b2650a4 100644 --- a/crates/standalone/src/lib.rs +++ b/crates/standalone/src/lib.rs @@ -167,7 +167,7 @@ impl NodeDelegate for StandaloneEnv { } async fn leader(&self, database_id: u64) -> Result { - let Some(leader) = self.control_db.get_leader_replica_by_database(database_id) else { + let Some(leader) = self.control_db.get_leader_replica_by_database(database_id)? else { return Err(GetLeaderHostError::NoSuchReplica); }; @@ -233,8 +233,8 @@ impl spacetimedb_client_api::ControlStateReadAccess for StandaloneEnv { Ok(self.control_db.get_replicas()?) } - async fn get_leader_replica_by_database(&self, database_id: u64) -> Option { - self.control_db.get_leader_replica_by_database(database_id) + async fn get_leader_replica_by_database(&self, database_id: u64) -> anyhow::Result> { + Ok(self.control_db.get_leader_replica_by_database(database_id)?) } // Energy async fn get_energy_balance(&self, identity: &Identity) -> anyhow::Result> { diff --git a/crates/standalone/src/subcommands/start.rs b/crates/standalone/src/subcommands/start.rs index 3f5f62d358b..47da186cd7f 100644 --- a/crates/standalone/src/subcommands/start.rs +++ b/crates/standalone/src/subcommands/start.rs @@ -34,7 +34,7 @@ pub fn cli() -> clap::Command { .default_value("0.0.0.0:3000") .help( "The address and port where SpacetimeDB should listen for connections. \ - This defaults to to listen on all IP addresses on port 80.", + This defaults to listen on all IP addresses on port 3000.", ), ) .arg( diff --git a/crates/testing/src/modules.rs b/crates/testing/src/modules.rs index 629ee5c1577..935e89bb383 100644 --- a/crates/testing/src/modules.rs +++ b/crates/testing/src/modules.rs @@ -229,7 +229,7 @@ impl CompiledModule { .unwrap(); let database = env.get_database_by_identity(&db_identity).await.unwrap().unwrap(); - let instance = env.get_leader_replica_by_database(database.id).await.unwrap(); + let instance = env.get_leader_replica_by_database(database.id).await.unwrap().unwrap(); let client_id = ClientActorId { identity,