use crate::event_loop::InternalMessage;
use polysig_protocol::{http::StatusCode, ResponseMessage};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("{0} {1}")]
ConnectError(StatusCode, String),
#[error("{0} {1}")]
ServerError(StatusCode, String),
#[error("server did not reply")]
NoReply,
#[error("peer already exists")]
PeerAlreadyExists,
#[error(
"peer already exists, maybe peers are racing to connect"
)]
PeerAlreadyExistsMaybeRace,
#[error(r#"peer "{0}" not found "#)]
PeerNotFound(String),
#[error("not handshake protocol state")]
NotHandshakeState,
#[error("not transport protocol state")]
NotTransportState,
#[error("invalid peer handshake message")]
InvalidPeerHandshakeMessage,
#[error("web socket failed to send")]
WebSocketSend,
#[error("meeting identifiers must be unique")]
MeetingIdentifiersNotUnique,
#[error("meeting initiator must exist in list of identifiers")]
MeetingInitiatorNotExist,
#[error("public key {0} is not a session participant")]
NotSessionParticipant(String),
#[cfg(feature = "cggmp")]
#[error("could not find an ACK for key init phase")]
NoKeyInitAck,
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[error("{0}")]
JsString(String),
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[error("{0}")]
JsValue(polysig_protocol::serde_json::Value),
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[error("unknown javascript error (type conversion failed)")]
JsError,
#[error("stream and sink reunite failed")]
StreamReunite,
#[error(transparent)]
Generic(
#[from] Box<dyn std::error::Error + Send + Sync + 'static>,
),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Protocol(#[from] polysig_protocol::Error),
#[error(transparent)]
Driver(#[from] polysig_driver::Error),
#[error(transparent)]
Snow(#[from] polysig_protocol::snow::error::Error),
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[error(transparent)]
Websocket(#[from] tokio_tungstenite::tungstenite::Error),
#[error(transparent)]
RequestMpscSend(
#[from] tokio::sync::mpsc::error::SendError<InternalMessage>,
),
#[error(transparent)]
ResponseMpscSend(
#[from] tokio::sync::mpsc::error::SendError<ResponseMessage>,
),
#[cfg(feature = "frost-ed25519")]
#[error(transparent)]
FrostEd25519Core(#[from] polysig_driver::frost_ed25519::Error),
#[cfg(feature = "cggmp")]
#[error(transparent)]
Cggmp(#[from] polysig_driver::cggmp::Error),
#[cfg(feature = "frost-ed25519")]
#[error(transparent)]
FrostEd25519(#[from] polysig_driver::frost::Error),
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
impl From<wasm_bindgen::JsValue> for Error {
fn from(value: wasm_bindgen::JsValue) -> Self {
if let Some(s) = value.as_string() {
Error::JsString(s)
} else {
match serde_wasm_bindgen::from_value::<
polysig_protocol::serde_json::Value,
>(value)
{
Ok(val) => Error::JsValue(val),
Err(_) => Error::JsError,
}
}
}
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
impl From<Error> for wasm_bindgen::JsValue {
fn from(value: Error) -> Self {
let s = value.to_string();
wasm_bindgen::JsValue::from_str(&s)
}
}