Skip to content
Snippets Groups Projects

Refactor commands in handle_command for less redundancy in error handling

1 file
+ 31
26
Compare changes
  • Side-by-side
  • Inline
+ 31
26
@@ -607,32 +607,16 @@ where
block_path,
peerid,
sender,
} =>
// TODO go search the block on the disk
{
match PeerId::from_str(peerid.as_str()) {
Ok(peer) => {
if self
.swarm
.behaviour_mut()
.dragoon
.send_data_to_peer(block_hash, block_path, peer)
{
if sender.send(Ok(())).is_err() {
error!("could not send result");
}
} else {
let err = PeerNotFound;
if sender.send(Err(Box::new(err))).is_err() {
error!("Cannot send result");
}
}
}
Err(err) => {
if sender.send(Err(Box::new(err))).is_err() {
error!("Cannot send result");
}
}
} => {
if sender
.send(
self.dragoon_send(block_hash, block_path, peerid)
.await
.map_err(|err| err.into()),
)
.is_err()
{
error!("Could not send the result of the dragoon_send operation")
}
}
DragoonCommand::DecodeBlocks {
@@ -710,6 +694,27 @@ where
// Ok(id)
// }
async fn dragoon_send(
&mut self,
block_hash: String,
block_path: String,
peerid: String,
) -> Result<()> {
// TODO go search the block on the disk
let peer = PeerId::from_str(&peerid)?;
if self
.swarm
.behaviour_mut()
.dragoon
.send_data_to_peer(block_hash, block_path, peer)
{
Ok(())
} else {
Err(PeerNotFound.into())
}
}
async fn decode_blocks(
block_dir: String,
block_hashes: Vec<String>,
Loading