yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications
https://yew.rs
Apache License 2.0
30.52k stars 1.42k forks source link

WebSocketService connect panic on malformed URL #726

Closed lizhaoxian closed 4 years ago

lizhaoxian commented 4 years ago

Description

yew::services::websocket::WebSocketService::connect this function has an unwrap

    let ws = WebSocket::new(url).unwrap();

for a malformed URL, this will result in a panic.

Expected Results

    pub fn connect<OUT: 'static>(
        &mut self,
        url: &str,
        callback: Callback<OUT>,
        notification: Callback<WebSocketStatus>,
    ) -> Result<WebSocketTask>
    where
        OUT: From<Text> + From<Binary>,
    {
        let ws = WebSocket::new(url);
        if ws.is_err() {
            return Err("Failed to create WebSocket from given URL");
        }
        ...

Actual Results

    pub fn connect<OUT: 'static>(
        &mut self,
        url: &str,
        callback: Callback<OUT>,
        notification: Callback<WebSocketStatus>,
    ) -> WebSocketTask
    where
        OUT: From<Text> + From<Binary>,
    {
        let ws = WebSocket::new(url).unwrap();

when panic happen, there is no proper way to handle it, the best solution is to have an Err returned.

Context (Environment)

jstarry commented 4 years ago

Thanks for the report, I agree we should go with your solution! I can get a fix together this weekend, if you need it sooner, feel free to send a PR

lizhaoxian commented 4 years ago

727

hgzimmerman commented 4 years ago

With the associated PR merged, this can be closed now.