use crate::webrtc_socket::messages::PeerEvent;
use cfg_if::cfg_if;
#[derive(Debug, thiserror::Error)]
pub enum ChannelError {
#[error("This channel was never created")]
NotFound,
#[error("This channel has already been taken and is no longer on the socket")]
Taken,
#[error("This channel is closed.")]
Closed,
}
#[derive(Debug, thiserror::Error)]
pub enum SignalingError {
#[error("failed to send to signaling server: {0}")]
UndeliverableSignal(#[from] futures_channel::mpsc::TrySendError<PeerEvent>),
#[error("The stream is exhausted")]
StreamExhausted,
#[error("Message received in unknown format")]
UnknownFormat,
#[error("failed to establish initial connection: {0}")]
NegotiationFailed(#[from] Box<SignalingError>),
#[cfg(not(target_arch = "wasm32"))]
#[error("socket failure communicating with signaling server: {0}")]
WebSocket(#[from] async_tungstenite::tungstenite::Error),
#[cfg(target_arch = "wasm32")]
#[error("socket failure communicating with signaling server: {0}")]
WebSocket(#[from] ws_stream_wasm::WsErr),
#[error("User implementation error: {0}")]
UserImplementationError(String),
}
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use wasm_bindgen::{JsValue};
use derive_more::Display;
pub trait JsErrorExt<T> {
fn efix(self) -> Result<T, JsError>;
}
impl<T> JsErrorExt<T> for Result<T, JsValue> {
fn efix(self) -> Result<T, JsError> {
self.map_err(JsError)
}
}
#[derive(Debug, Display)]
#[display("{_0:?}")]
pub struct JsError(JsValue);
impl std::error::Error for JsError {}
}
}