Use failure for error handling.
This commit is contained in:
parent
ce9d5c7e42
commit
678f843f5e
@ -8,6 +8,7 @@ edition = "2018"
|
||||
cbor = "*"
|
||||
cid = "*"
|
||||
env_logger = "*"
|
||||
failure = "*"
|
||||
fnv = "*"
|
||||
futures-preview = { version = "0.3.0-alpha.13", features = ["compat"] }
|
||||
libp2p = { version = "*", git = "https://github.com/libp2p/rust-libp2p", rev = "5655624" }
|
||||
|
55
src/error.rs
55
src/error.rs
@ -1,54 +1 @@
|
||||
pub trait IpfsError: std::fmt::Display + std::fmt::Debug + std::error::Error + Send {}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error(Box<IpfsError>);
|
||||
|
||||
impl std::error::Error for Error {
|
||||
fn description(&self) -> &str {
|
||||
self.0.description()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NoneError(std::option::NoneError);
|
||||
|
||||
impl std::error::Error for NoneError {
|
||||
fn description(&self) -> &str {
|
||||
"none error"
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for NoneError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "None error")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::option::NoneError> for Error {
|
||||
fn from(err: std::option::NoneError) -> Self {
|
||||
NoneError(err).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl IpfsError for NoneError {}
|
||||
impl IpfsError for crate::bitswap::BitswapError {}
|
||||
impl IpfsError for crate::ipld::IpldError {}
|
||||
impl IpfsError for crate::path::IpfsPathError {}
|
||||
impl IpfsError for cbor::CborError {}
|
||||
impl IpfsError for cid::Error {}
|
||||
impl IpfsError for libp2p::core::upgrade::ReadOneError {}
|
||||
impl IpfsError for protobuf::ProtobufError {}
|
||||
impl IpfsError for rocksdb::Error {}
|
||||
impl IpfsError for std::io::Error {}
|
||||
|
||||
impl<T: IpfsError + 'static> From<T> for Error {
|
||||
fn from(err: T) -> Self {
|
||||
Error(Box::new(err))
|
||||
}
|
||||
}
|
||||
pub use failure::Error;
|
||||
|
@ -31,7 +31,10 @@ impl<Types: RepoTypes> IpldDag<Types> {
|
||||
pub fn get(&self, path: IpfsPath) -> impl Future<Output=Result<Ipld, Error>> {
|
||||
let repo = self.repo.clone();
|
||||
async move {
|
||||
let cid = path.root().cid()?;
|
||||
let cid = match path.root().cid() {
|
||||
Some(cid) => cid,
|
||||
None => bail!("expected cid"),
|
||||
};
|
||||
let mut ipld = Ipld::from(&await!(repo.get_block(&cid))?)?;
|
||||
for sub_path in path.iter() {
|
||||
if !can_resolve(&ipld, sub_path) {
|
||||
@ -41,7 +44,10 @@ impl<Types: RepoTypes> IpldDag<Types> {
|
||||
ipld = resolve(ipld, sub_path);
|
||||
ipld = match ipld {
|
||||
Ipld::Link(root) => {
|
||||
Ipld::from(&await!(repo.get_block(root.cid()?))?)?
|
||||
match root.cid() {
|
||||
Some(cid) => Ipld::from(&await!(repo.get_block(cid))?)?,
|
||||
None => bail!("expected cid"),
|
||||
}
|
||||
}
|
||||
ipld => ipld,
|
||||
};
|
||||
|
@ -21,7 +21,10 @@ pub(crate) fn decode(bytes: &Vec<u8>) -> Result<Ipld, Error> {
|
||||
}
|
||||
|
||||
pub(crate) fn encode(data: Ipld) -> Result<Vec<u8>, Error> {
|
||||
let pb_node: PbNode = data.to_owned().try_into()?;
|
||||
let pb_node: PbNode = match data.to_owned().try_into() {
|
||||
Ok(pb_node) => pb_node,
|
||||
Err(_) => bail!("ipld data is not compatible with dag_pb format"),
|
||||
};
|
||||
Ok(pb_node.into_bytes())
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
#![feature(drain_filter)]
|
||||
#![feature(try_trait)]
|
||||
|
||||
#[macro_use] extern crate failure;
|
||||
#[macro_use] extern crate log;
|
||||
use futures::prelude::*;
|
||||
use std::marker::PhantomData;
|
||||
|
@ -111,7 +111,10 @@ impl TryInto<Cid> for IpfsPath {
|
||||
type Error = Error;
|
||||
|
||||
fn try_into(self) -> Result<Cid, Self::Error> {
|
||||
Ok(self.root().cid()?.to_owned())
|
||||
match self.root().cid() {
|
||||
Some(cid) => Ok(cid.to_owned()),
|
||||
None => bail!("expected cid"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -119,7 +122,10 @@ impl TryInto<PeerId> for IpfsPath {
|
||||
type Error = Error;
|
||||
|
||||
fn try_into(self) -> Result<PeerId, Self::Error> {
|
||||
Ok(self.root().peer_id()?.to_owned())
|
||||
match self.root().peer_id() {
|
||||
Some(peer_id) => Ok(peer_id.to_owned()),
|
||||
None => bail!("expected peer id"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,10 @@ impl File {
|
||||
let future = dag.get(path);
|
||||
async move {
|
||||
let ipld = await!(future)?;
|
||||
let pb_node: PbNode = ipld.try_into()?;
|
||||
let pb_node: PbNode = match ipld.try_into() {
|
||||
Ok(pb_node) => pb_node,
|
||||
Err(_) => bail!("invalid dag_pb node"),
|
||||
};
|
||||
Ok(File {
|
||||
data: pb_node.data,
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user