Skip to content

Commit

Permalink
Merge pull request #13 from noocene/error-handling
Browse files Browse the repository at this point in the history
Port from failure to anyhow/thiserror, add passthrough
  • Loading branch information
Izzy Swart committed Jan 8, 2020
2 parents 1d35fa6 + b45c947 commit 338bf6b
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 211 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -17,7 +17,6 @@ default = ["cbor", "json", "bincode"]
[dependencies]
futures = { version = "0.3.1", features = ["thread-pool"] }
serde = { version = "1.0.101", features = ["derive"] }
failure = "0.1.5"
erased-serde = "0.3.9"
serde_json = { version = "1.0.41", optional = true }
serde_cbor = "0.10.2"
Expand All @@ -27,6 +26,8 @@ void = "1.0.2"
downcast-rs = "1.1.1"
weak-table = "0.2.3"
url = "2.1.0"
thiserror = "1.0.9"
anyhow = "1.0.26"

[target.wasm32-unknown-unknown.dependencies]
wasm-bindgen = { version = "0.2.54", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion examples/function_stream.rs
Expand Up @@ -6,7 +6,7 @@ use vessels::{
log, OnTo,
};

use failure::Error;
use anyhow::Error;

use futures::{stream::iter, StreamExt};

Expand Down
35 changes: 4 additions & 31 deletions src/channel/id_channel/mod.rs
Expand Up @@ -12,7 +12,6 @@ use core::{
marker::PhantomData,
pin::Pin,
};
use failure::Fail;
use futures::{
channel::mpsc::{unbounded, SendError, UnboundedReceiver, UnboundedSender},
future::ok,
Expand All @@ -21,6 +20,7 @@ use futures::{
};
use serde::{de::DeserializeOwned, Serialize};
use std::{collections::HashMap, sync::Mutex};
use thiserror::Error;

use crate::{
channel::{Channel, Context as IContext, Fork as IFork, ForkHandle, Waiter},
Expand Down Expand Up @@ -84,41 +84,14 @@ impl Display for SinkStage {
}
}

#[derive(Debug)]
#[derive(Debug, Error)]
pub enum IdChannelError {
#[error("send on underlying channel `{1}` in `{0}` stage failed: `{2}`")]
Channel(SinkStage, ForkHandle, ChannelError),
#[error("underlying channel `{0}` does not exist")]
InvalidId(ForkHandle),
}

impl Fail for IdChannelError {
fn name(&self) -> Option<&str> {
Some("IdChannelError")
}
fn cause(&self) -> Option<&dyn Fail> {
if let IdChannelError::Channel(_, _, error) = self {
return Some(error.0.as_fail());
}
None
}
}

impl Display for IdChannelError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
use IdChannelError::{Channel, InvalidId};
write!(
formatter,
"{}",
match self {
Channel(stage, handle, error) => format!(
"send on underlying channel {} in {} stage failed: {}",
handle, stage, error
),
InvalidId(handle) => format!("underlying channel {} does not exist", handle),
}
)
}
}

impl Drop for IdChannel {
fn drop(&mut self) {
self.in_channels.lock().unwrap().remove(&ForkHandle(0));
Expand Down
12 changes: 5 additions & 7 deletions src/channel/mod.rs
Expand Up @@ -6,16 +6,14 @@ use crate::{
Kind,
};

use core::{
fmt::{self, Display, Formatter},
marker::Unpin,
};
use failure::{Error, Fail};
use anyhow::Error;
use core::fmt::{self, Display, Formatter};
use futures::{Sink, Stream};
use serde::{
de::{DeserializeOwned, DeserializeSeed},
Deserialize, Serialize,
};
use thiserror::Error;

#[derive(Serialize, Deserialize, Debug, PartialEq, Hash, Eq, Clone, Copy)]
#[repr(transparent)]
Expand All @@ -32,8 +30,8 @@ pub trait Fork: Sync + Send + 'static {
fn get_fork<K: Kind>(&self, fork_ref: ForkHandle) -> Fallible<K, K::ConstructError>;
}

#[derive(Debug, Fail)]
pub struct ChannelError(#[fail(cause)] pub(crate) Error);
#[derive(Debug, Error)]
pub struct ChannelError(#[source] pub(crate) Error);

impl Display for ChannelError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
Expand Down
15 changes: 8 additions & 7 deletions src/core/data/mod.rs
Expand Up @@ -9,9 +9,10 @@ use crate::{
Kind,
};

use anyhow::{anyhow, Error};
use core::fmt::{self, Debug, Formatter};
use failure::{format_err, Error, Fail};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use thiserror::Error;

#[derive(Hash, Kind, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Checksum(pub(crate) [u8; 32]);
Expand Down Expand Up @@ -48,17 +49,17 @@ pub struct Resource<T: Serialize + DeserializeOwned + Sync + Send + 'static> {
acquire: Option<Box<dyn FnOnce() -> Infallible<Serde<T>> + Sync + Send>>,
}

#[derive(Fail, Kind)]
#[fail(display = "reification failed: {}", cause)]
#[derive(Error, Kind)]
#[error("reification failed: {source}")]
pub struct ReifyError<T: Serialize + DeserializeOwned + Sync + Send + 'static> {
#[fail(cause)]
cause: Error,
#[source]
source: Error,
pub resource: Resource<T>,
}

impl<T: Serialize + DeserializeOwned + Sync + Send + 'static> Debug for ReifyError<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "ReifyError {{ cause: {:?} }}", self.cause)
write!(f, "ReifyError {{ source: {:?} }}", self.source)
}
}

Expand Down Expand Up @@ -92,7 +93,7 @@ impl<T: Serialize + DeserializeOwned + Sync + Send + 'static> Resource<T> {
} else {
// TODO reify from abstract acquisition methods
Err(ReifyError {
cause: format_err!("no suitable acquisition method"),
source: anyhow!("no suitable acquisition method"),
resource: self,
})
}
Expand Down
31 changes: 16 additions & 15 deletions src/core/hal/network/mod.rs
Expand Up @@ -6,22 +6,23 @@ use crate::{
object, Kind,
};

use failure::{Error, Fail};
use anyhow::Error;
use futures::{future::ready, lock::Mutex, FutureExt, Sink, StreamExt};
use std::{net::SocketAddr, sync::Arc};
use thiserror::Error;
use url::Url;

#[object]
pub trait Peer {}

#[derive(Fail, Debug, Kind)]
#[derive(Error, Debug, Kind)]
pub enum ConnectError {
#[fail(display = "connection failed: {}", _0)]
Connect(#[cause] Error),
#[fail(display = "construct failed: {}", _0)]
Construct(#[cause] Error),
#[fail(display = "underlying transport failed: {}", _0)]
Transport(#[cause] Error),
#[error("connection failed: `{0}`")]
Connect(#[source] Error),
#[error("construct failed: `{0}`")]
Construct(#[source] Error),
#[error("underlying transport failed: `{0}`")]
Transport(#[source] Error),
}

impl FromTransportError for ConnectError {
Expand All @@ -30,10 +31,10 @@ impl FromTransportError for ConnectError {
}
}

#[derive(Fail, Debug, Kind)]
#[fail(display = "listening failed: {}", cause)]
#[derive(Error, Debug, Kind)]
#[error("listening failed: {cause}")]
pub struct ListenError {
#[fail(cause)]
#[source]
cause: Error,
}

Expand All @@ -43,10 +44,10 @@ impl FromTransportError for ListenError {
}
}

#[derive(Fail, Debug, Kind)]
#[fail(display = "connection failed while open: {}", cause)]
#[derive(Error, Debug, Kind)]
#[error("connection failed while open: {cause}")]
pub struct ConnectionError {
#[fail(cause)]
#[source]
cause: Error,
}

Expand Down Expand Up @@ -123,7 +124,7 @@ impl Server {
) -> Future<Result<(), ListenError>>
where
T: ApplyEncode<'a>,
<T as Sink<<T as Context<'a>>::Item>>::Error: Fail,
<T as Sink<<T as Context<'a>>::Item>>::Error: std::error::Error + Sync + Send + 'static,
{
let handler = Arc::new(Mutex::new(handler));
self.0.listen(
Expand Down
6 changes: 3 additions & 3 deletions src/core/hal/network/web.rs
Expand Up @@ -2,7 +2,6 @@ use super::{ConnectError, ConnectionError, RawClient};

use crate::{core::spawn, kind::Future, kind::SinkStream, SyncSendAssert};

use failure::Fail;
use futures::{
channel::{
mpsc::{unbounded, UnboundedReceiver},
Expand All @@ -12,13 +11,14 @@ use futures::{
};
use js_sys::Uint8Array;
use url::Url;
use thiserror::Error;
use wasm_bindgen::{closure::Closure, JsCast};
use web_sys::{BinaryType, MessageEvent, WebSocket};

pub(crate) struct Client;

#[derive(Fail, Debug)]
#[fail(display = "the target port is being blocked")]
#[derive(Error, Debug)]
#[error("the target port is being blocked")]
pub struct SecurityError;

impl RawClient for Client {
Expand Down
38 changes: 13 additions & 25 deletions src/core/mod.rs
@@ -1,11 +1,11 @@
use alloc::sync::Arc;
#[cfg(any(feature = "core", target_arch = "wasm32"))]
use anyhow::Error;
#[cfg(any(target_arch = "wasm32", feature = "core"))]
use core::any::Any;
use core::fmt::{self, Display, Formatter};
use failure::{Error, Fail};
use futures::{lock, SinkExt, StreamExt};
use lazy_static::lazy_static;
use std::{collections::HashMap, sync::Mutex};
use thiserror::Error;

use crate::{
channel::IdChannel,
Expand All @@ -26,34 +26,22 @@ pub mod orchestrator;
#[doc(hidden)]
pub type Constructor<T> = Box<dyn FnOnce(Handle) -> Infallible<T> + Send + Sync>;

#[derive(Fail, Debug, Kind)]
#[fail(display = "{} is unimplemented on this target", feature)]
#[derive(Error, Debug, Kind)]
#[error("{feature} is unimplemented on this target")]
pub struct UnimplementedError {
feature: String,
}

#[derive(Fail, Debug, Kind)]
#[derive(Error, Debug, Kind)]
pub enum CoreError {
#[error("feature unavailable or unregistered")]
Unavailable,
Unimplemented(#[fail(cause)] UnimplementedError),
Construct(#[fail(cause)] Error),
Transport(#[fail(cause)] Error),
}

impl Display for CoreError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
use CoreError::{Construct, Transport, Unavailable, Unimplemented};
write!(
formatter,
"{}",
match self {
Unavailable => "this feature is unavailable or unregistered".to_owned(),
Unimplemented(feature) => format!("{}", feature),
Construct(e) => format!("handle transfer failed: {}", e),
Transport(e) => format!("underlying transport failed: {}", e),
}
)
}
#[error("`{0}`")]
Unimplemented(#[source] UnimplementedError),
#[error("`handle transfer failed: {0}`")]
Construct(#[source] Error),
#[error("`underlying transport failed: {0}`")]
Transport(#[source] Error),
}

impl FromTransportError for CoreError {
Expand Down
11 changes: 6 additions & 5 deletions src/core/orchestrator/mod.rs
Expand Up @@ -11,12 +11,13 @@ use crate::{
Kind,
};

use anyhow::Error;
use core::marker::PhantomData;
use failure::{Error, Fail};
use futures::SinkExt;
#[cfg(feature = "core")]
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[cfg(all(not(target_arch = "wasm32"), feature = "core"))]
mod native;
Expand All @@ -41,8 +42,8 @@ impl<T: Kind> Module<T> {
#[kind(using::Serde)]
pub(crate) struct LocalModule(pub(crate) Checksum);

#[derive(Fail, Debug, Kind)]
#[fail(display = "compile failed: {}", cause)]
#[derive(Error, Debug, Kind)]
#[error("compile failed: {cause}")]
pub struct CompileError {
cause: Error,
}
Expand All @@ -65,8 +66,8 @@ trait OrchestratorInner {
#[derive(Kind)]
pub struct Orchestrator(Shared<dyn OrchestratorInner>);

#[derive(Fail, Debug, Kind)]
#[fail(display = "instantiate failed: {}", cause)]
#[derive(Error, Debug, Kind)]
#[error("instantiate failed: {cause}")]
pub struct InstantiateError {
cause: Error,
}
Expand Down

0 comments on commit 338bf6b

Please sign in to comment.