Skip to content

Commit

Permalink
distinct transport error
Browse files Browse the repository at this point in the history
  • Loading branch information
syntacticsugarglider committed Jan 8, 2020
1 parent 0f5fba3 commit bdf18b7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 51 deletions.
24 changes: 10 additions & 14 deletions src/core/hal/network/mod.rs
Expand Up @@ -2,7 +2,7 @@ use crate::{
channel::{Context, OnTo, Target},
core::{spawn, UnimplementedError},
format::{ApplyDecode, ApplyEncode, Format},
kind::{Fallible, FromTransportError, Future, Infallible, SinkStream},
kind::{Fallible, Future, Infallible, SinkStream, TransportError},
object, Kind,
};

Expand All @@ -22,13 +22,7 @@ pub enum ConnectError {
#[error("construct failed: `{0}`")]
Construct(#[source] Error),
#[error("underlying transport failed: `{0}`")]
Transport(#[source] Error),
}

impl FromTransportError for ConnectError {
fn from_transport_error(error: Error) -> Self {
ConnectError::Transport(error)
}
Transport(#[from] TransportError),
}

#[derive(Error, Debug, Kind)]
Expand All @@ -38,9 +32,11 @@ pub struct ListenError {
cause: Error,
}

impl FromTransportError for ListenError {
fn from_transport_error(cause: Error) -> Self {
ListenError { cause }
impl From<TransportError> for ListenError {
fn from(error: TransportError) -> Self {
ListenError {
cause: error.into(),
}
}
}

Expand All @@ -51,9 +47,9 @@ pub struct ConnectionError {
cause: Error,
}

impl FromTransportError for ConnectionError {
fn from_transport_error(cause: Error) -> Self {
ConnectionError { cause }
impl From<TransportError> for ConnectionError {
fn from(error: TransportError) -> Self {
ConnectionError { cause: error.into() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/hal/network/web.rs
Expand Up @@ -10,8 +10,8 @@ use futures::{
SinkExt, StreamExt,
};
use js_sys::Uint8Array;
use url::Url;
use thiserror::Error;
use url::Url;
use wasm_bindgen::{closure::Closure, JsCast};
use web_sys::{BinaryType, MessageEvent, WebSocket};

Expand Down
10 changes: 2 additions & 8 deletions src/core/mod.rs
Expand Up @@ -10,7 +10,7 @@ use thiserror::Error;
use crate::{
channel::IdChannel,
format::{ApplyDecode, ApplyEncode, Cbor},
kind::{Fallible, FromTransportError, Future, Infallible, SinkStream},
kind::{Fallible, Future, Infallible, SinkStream, TransportError},
object,
replicate::Share,
Kind, OnTo,
Expand Down Expand Up @@ -41,13 +41,7 @@ pub enum CoreError {
#[error("`handle transfer failed: {0}`")]
Construct(#[source] Error),
#[error("`underlying transport failed: {0}`")]
Transport(#[source] Error),
}

impl FromTransportError for CoreError {
fn from_transport_error(error: Error) -> Self {
CoreError::Transport(error)
}
Transport(#[from] TransportError),
}

#[doc(hidden)]
Expand Down
22 changes: 9 additions & 13 deletions src/core/orchestrator/mod.rs
Expand Up @@ -5,7 +5,7 @@ use crate::{
Constructor, Handle, UnimplementedError,
},
format::{ApplyDecode, Cbor},
kind::{using, Fallible, FromTransportError, SinkStream},
kind::{using, Fallible, SinkStream, TransportError},
object,
replicate::{Share, Shared},
Kind,
Expand Down Expand Up @@ -45,12 +45,13 @@ pub(crate) struct LocalModule(pub(crate) Checksum);
#[derive(Error, Debug, Kind)]
#[error("compile failed: {cause}")]
pub struct CompileError {
#[source]
cause: Error,
}

impl FromTransportError for CompileError {
fn from_transport_error(cause: Error) -> Self {
CompileError { cause }
impl From<TransportError> for CompileError {
fn from(error: TransportError) -> Self {
CompileError { cause: error.into() }
}
}

Expand All @@ -69,18 +70,13 @@ pub struct Orchestrator(Shared<dyn OrchestratorInner>);
#[derive(Error, Debug, Kind)]
#[error("instantiate failed: {cause}")]
pub struct InstantiateError {
#[source]
cause: Error,
}

impl FromTransportError for InstantiateError {
fn from_transport_error(cause: Error) -> Self {
InstantiateError { cause }
}
}

impl From<Error> for InstantiateError {
fn from(cause: Error) -> Self {
InstantiateError { cause }
impl From<TransportError> for InstantiateError {
fn from(error: TransportError) -> Self {
InstantiateError { cause: error.into() }
}
}

Expand Down
35 changes: 20 additions & 15 deletions src/kind/mod.rs
Expand Up @@ -33,27 +33,32 @@ use thiserror::Error;

use crate::{channel::ChannelError, Kind};

#[derive(Error, Kind, Debug)]
#[error("transport error: {cause}")]
pub struct TransportError {
#[source]
cause: Error,
}

impl TransportError {
fn new(cause: Error) -> Self {
TransportError {
cause
}
}
}

pub type Future<T> = Pin<Box<dyn IFuture<Output = T> + Sync + Send>>;
pub type Fallible<T, E> = Future<Result<T, E>>;
pub type Stream<T> = Pin<Box<dyn IStream<Item = T> + Sync + Send>>;
pub type Infallible<T> = Fallible<T, Error>;
pub type Infallible<T> = Fallible<T, TransportError>;
pub type Sink<T, E> = Pin<Box<dyn ISink<T, Error = E> + Sync + Send>>;

/// The result of reconstructing a Kind.
pub type ConstructResult<K> = Result<K, <K as Kind>::ConstructError>;
/// The result of deconstructing a Kind.
pub type DeconstructResult<K> = Result<(), <K as Kind>::DeconstructError>;

pub trait FromTransportError: Send + Sync {
fn from_transport_error(error: Error) -> Self;
}

impl FromTransportError for Error {
fn from_transport_error(error: Error) -> Self {
error
}
}

pub trait Flatten: Sized {
fn flatten<
E: 'static + Sync + Send + Into<Error>,
Expand All @@ -63,7 +68,7 @@ pub trait Flatten: Sized {
) -> Self;
}

impl<U: FromTransportError, T> Flatten for Fallible<T, U> {
impl<U: From<TransportError> + Sync + Send, T> Flatten for Fallible<T, U> {
fn flatten<
E: 'static + Sync + Send + Into<Error>,
F: IFuture<Output = Result<Self, E>> + Sync + Send + 'static,
Expand All @@ -72,13 +77,13 @@ impl<U: FromTransportError, T> Flatten for Fallible<T, U> {
) -> Self {
Box::pin(async move {
fut.await
.map_err(|e| U::from_transport_error(e.into()))?
.map_err(|e| U::from(TransportError::new(e.into())))?
.await
})
}
}

impl<U: FromTransportError, T> Flatten for Stream<Result<T, U>> {
impl<U: From<TransportError>, T> Flatten for Stream<Result<T, U>> {
fn flatten<
E: 'static + Sync + Send + Into<Error>,
F: IFuture<Output = Result<Self, E>> + Sync + Send + 'static,
Expand All @@ -89,7 +94,7 @@ impl<U: FromTransportError, T> Flatten for Stream<Result<T, U>> {
async move {
let r = fut.await;
match r {
Err(e) => Box::pin(once(async move { Err(U::from_transport_error(e.into())) }))
Err(e) => Box::pin(once(async move { Err(U::from(TransportError::new(e.into()))) }))
as Stream<Result<T, U>>,
Ok(s) => Box::pin(s),
}
Expand Down

0 comments on commit bdf18b7

Please sign in to comment.