Skip to content

Commit 2a72daf

Browse files
committed
Replace api::Error::other with dedicated errors.
1 parent f1bbd6c commit 2a72daf

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

src/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl From<EncodeError> for Error {
199199
fn from(value: EncodeError) -> Self {
200200
match value {
201201
EncodeError::Io(cause) => Self::Io(cause),
202-
_ => Self::other(value),
202+
_ => Self::Io(io::Error::other(value)),
203203
}
204204
}
205205
}

src/api/blobs.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,13 @@ impl Blobs {
440440
reader
441441
.recv_exact(&mut size)
442442
.await
443-
.map_err(super::Error::other)?;
443+
.map_err(io::Error::from)?;
444444
let size = u64::from_le_bytes(size);
445445
let Some(size) = NonZeroU64::new(size) else {
446446
return if hash == Hash::EMPTY {
447447
Ok(reader)
448448
} else {
449-
Err(super::Error::other("invalid size for hash").into())
449+
Err(io::Error::other("invalid size for hash").into())
450450
};
451451
};
452452
let tree = BaoTree::new(size.get(), IROH_BLOCK_SIZE);
@@ -651,7 +651,7 @@ impl<'a> AddProgress<'a> {
651651
_ => {}
652652
}
653653
}
654-
Err(super::Error::other("unexpected end of stream").into())
654+
Err(io::Error::other("unexpected end of stream").into())
655655
}
656656

657657
pub async fn with_named_tag(self, name: impl AsRef<[u8]>) -> RequestResult<HashAndFormat> {
@@ -704,7 +704,7 @@ impl IntoFuture for ObserveProgress {
704704
let mut rx = self.inner.await?;
705705
match rx.recv().await? {
706706
Some(bitfield) => Ok(bitfield),
707-
None => Err(super::Error::other("unexpected end of stream").into()),
707+
None => Err(io::Error::other("unexpected end of stream").into()),
708708
}
709709
})
710710
}
@@ -726,7 +726,7 @@ impl ObserveProgress {
726726
return Ok(item);
727727
}
728728
}
729-
Err(super::Error::other("unexpected end of stream").into())
729+
Err(io::Error::other("unexpected end of stream").into())
730730
}
731731

732732
/// Returns an infinite stream of bitfields. The first bitfield is the
@@ -805,7 +805,7 @@ impl ExportProgress {
805805
if let Some(size) = size {
806806
Ok(size)
807807
} else {
808-
Err(super::Error::other("unexpected end of stream").into())
808+
Err(io::Error::other("unexpected end of stream").into())
809809
}
810810
}
811811
}

src/store/fs.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ async fn handle_batch(cmd: BatchMsg, id: Scope, scope: Arc<TempTagScope>, ctx: A
853853
async fn handle_batch_impl(cmd: BatchMsg, id: Scope, scope: &Arc<TempTagScope>) -> api::Result<()> {
854854
let BatchMsg { tx, mut rx, .. } = cmd;
855855
trace!("created scope {}", id);
856-
tx.send(id).await.map_err(api::Error::other)?;
856+
tx.send(id).await?;
857857
while let Some(msg) = rx.recv().await? {
858858
match msg {
859859
BatchResponse::Drop(msg) => scope.on_drop(&msg),
@@ -1266,9 +1266,7 @@ async fn export_path_impl(
12661266
MemOrFile::Mem(data) => data.len() as u64,
12671267
MemOrFile::File((_, size)) => *size,
12681268
};
1269-
tx.send(ExportProgressItem::Size(size))
1270-
.await
1271-
.map_err(api::Error::other)?;
1269+
tx.send(ExportProgressItem::Size(size)).await?;
12721270
match data {
12731271
MemOrFile::Mem(data) => {
12741272
let mut target = fs::File::create(&target)?;
@@ -1324,9 +1322,7 @@ async fn export_path_impl(
13241322
}
13251323
},
13261324
}
1327-
tx.send(ExportProgressItem::Done)
1328-
.await
1329-
.map_err(api::Error::other)?;
1325+
tx.send(ExportProgressItem::Done).await?;
13301326
Ok(())
13311327
}
13321328

src/store/fs/meta.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ async fn handle_list_tags(msg: ListTagsMsg, tables: &impl ReadableTables) -> Act
363363
res.push(crate::api::Result::Ok(info));
364364
}
365365
}
366-
Err(e) => res.push(Err(crate::api::Error::other(e))),
366+
Err(e) => res.push(Err(api_error_from_storage_error(e))),
367367
}
368368
}
369369
tx.send(res).await.ok();
@@ -629,8 +629,12 @@ impl Actor {
629629
tx,
630630
..
631631
} = cmd;
632-
let res = tables.tags.insert(tag, value).map(|_| ());
633-
tx.send(res.map_err(crate::api::Error::other)).await.ok();
632+
let res = tables
633+
.tags
634+
.insert(tag, value)
635+
.map_err(api_error_from_storage_error)
636+
.map(|_| ());
637+
tx.send(res).await.ok();
634638
Ok(())
635639
}
636640

@@ -852,6 +856,13 @@ impl Actor {
852856
}
853857
}
854858

859+
/// Convert a redb StorageError into an api::Error
860+
///
861+
/// This can't be a From instance because that would require exposing redb::StorageError in the public API.
862+
fn api_error_from_storage_error(e: redb::StorageError) -> api::Error {
863+
api::Error::Io(io::Error::other(e))
864+
}
865+
855866
#[derive(Debug)]
856867
struct DbWrapper(Option<Database>);
857868

@@ -953,8 +964,12 @@ async fn list_blobs_impl(
953964
_cmd: ListRequest,
954965
tx: &mut mpsc::Sender<api::Result<Hash>>,
955966
) -> api::Result<()> {
956-
for item in snapshot.blobs.iter().map_err(api::Error::other)? {
957-
let (k, _) = item.map_err(api::Error::other)?;
967+
for item in snapshot
968+
.blobs
969+
.iter()
970+
.map_err(api_error_from_storage_error)?
971+
{
972+
let (k, _) = item.map_err(api_error_from_storage_error)?;
958973
let k = k.value();
959974
tx.send(Ok(k)).await.ok();
960975
}

src/store/mem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ async fn handle_batch(cmd: BatchMsg, id: Scope, scope: Arc<TempTagScope>) -> Sco
542542
async fn handle_batch_impl(cmd: BatchMsg, id: Scope, scope: &Arc<TempTagScope>) -> api::Result<()> {
543543
let BatchMsg { tx, mut rx, .. } = cmd;
544544
trace!("created scope {}", id);
545-
tx.send(id).await.map_err(api::Error::other)?;
545+
tx.send(id).await?;
546546
while let Some(msg) = rx.recv().await? {
547547
match msg {
548548
BatchResponse::Drop(msg) => scope.on_drop(&msg),

0 commit comments

Comments
 (0)