1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#![allow(missing_docs)]

pub type Result<T> = std::result::Result<T, Error>;

#[derive(thiserror::Error, Debug)]
pub enum IceServerError {
    #[error("Url parse error")]
    UrlParse(#[from] url::ParseError),

    #[error("Ice server scheme {0} has not supported yet")]
    SchemeNotSupported(String),

    #[error("Cannot extract host from url")]
    UrlMissHost,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[cfg(feature = "native-webrtc")]
    #[error("WebRTC error: {0}")]
    Webrtc(#[from] webrtc::error::Error),

    #[cfg(feature = "web-sys-webrtc")]
    #[error("WebSysWebRTC error: {}", dump_js_value(.0))]
    WebSysWebrtc(wasm_bindgen::JsValue),

    #[error("Bincode error: {0}")]
    Bincode(#[from] bincode::Error),

    #[error("IceServer error: {0}")]
    IceServer(#[from] IceServerError),

    #[error("Failed when waiting for data channel open: {0}")]
    DataChannelOpen(String),

    #[error("WebRTC local SDP generation error")]
    WebrtcLocalSdpGenerationError,

    #[error("Connection {0} already exists")]
    ConnectionAlreadyExists(String),

    #[error("Connection {0} not found, should handshake first")]
    ConnectionNotFound(String),

    #[error("Connection {0} is released")]
    ConnectionReleased(String),

    #[error("Failed on notifier, state is not succeeded")]
    NotifierFailed,

    #[error("Notifier timeout, state is not succeeded within {0} ms")]
    NotifierTimeout(u64),
}

#[cfg(feature = "web-sys-webrtc")]
fn dump_js_value(v: &wasm_bindgen::JsValue) -> String {
    let Ok(s) = js_sys::JSON::stringify(v) else {
        return "Failed to stringify Error(JsValue)".to_string();
    };
    s.into()
}