Repository navigation
NetworkError caused by WebSocketStream cannot be caught? #22165
Copy link
Copy link
Closed
Description
Version: Deno 1.40.1
const listener = Deno.listen({ port: 9000, transport: "tcp" });
try {
for await (const conn of listener) {
(async () => {
const ws = new WebSocketStream("ws://loca99999999999lhost:8000", {
headers: {
"x-Protocol": "Transmission Control",
"X-Destination-Address": "www.ba99999999999idu.com",
"X-Destination-Port": "80",
},
});
// console.log(ws);
try {
const webConn = await ws.opened; //.catch(console.error);
// console.log(webConn);
conn.readable.pipeTo(webConn.writable).catch(function (e) {
try {
ws.close();
conn.close();
} catch (e) {
console.error(e);
}
return console.error(e);
});
webConn.readable.pipeTo(conn.writable).catch(function (e) {
try {
ws.close();
conn.close();
} catch (e) {
console.error(e);
}
return console.error(e);
});
} catch (error) {
console.error(error);
try {
ws.close();
conn.close();
} catch (e) {
console.error(e);
}
}
})().catch(console.error);
}
} catch (error) {
console.error(error);
}deno run --unstable -A ./client.ts
⚠️ The `--unstable` flag is deprecated and will be removed in Deno 2.0. Use granular `--unstable-*` flags instead.
Learn more at: https://docs.deno.com/runtime/manual/tools/unstable_flags
DOMException: failed to connect to WebSocket: 不知道这样的主机。 (os error 11001)
at new DOMException (ext:deno_web/01_dom_exception.js:116:20)
at DOMExceptionNetworkError (ext:runtime_main/js/99_main.js:391:12)
at ext:core/00_infra.js:200:34
at eventLoopTick (ext:core/01_core.js:59:7)
error: Uncaught (in promise) NetworkError: failed to connect to WebSocket: 不知道这样的主机。 (os error 11001)
curl -I -v localhost:9000 -H host:www.baidu.com
* Trying [::1]:9000...
* Trying 127.0.0.1:9000...
* Connected to localhost (127.0.0.1) port 9000
> HEAD / HTTP/1.1
> Host:www.baidu.com
> User-Agent: curl/8.4.0
> Accept: */*
>
* Empty reply from server
* Closing connection
curl: (52) Empty reply from server
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
kt3k commentedon Jan 29, 2024
It looks like you also need to await
ws.closedto avoid uncaught error:const listener = Deno.listen({ port: 9000, transport: "tcp" }); try { for await (const conn of listener) { (async () => { const ws = new WebSocketStream("ws://loca99999999999lhost:8000", { headers: { "x-Protocol": "Transmission Control", "X-Destination-Address": "www.ba99999999999idu.com", "X-Destination-Port": "80", }, }); // console.log(ws); try { const webConn = await ws.opened; //.catch(console.error); // console.log(webConn); conn.readable.pipeTo(webConn.writable).catch(function (e) { try { ws.close(); conn.close(); } catch (e) { console.error(e); } return console.error(e); }); webConn.readable.pipeTo(conn.writable).catch(function (e) { try { ws.close(); conn.close(); } catch (e) { console.error(e); } return console.error(e); }); } catch (error) { console.error(error); try { ws.close(); conn.close(); + await ws.closed; } catch (e) { console.error(e); } } })().catch(console.error); } } catch (error) { console.error(error); }masx200 commentedon Jan 29, 2024
While this can be solved, can we design a better WebSocketStream api?
It's too hard to use
masx200 commentedon Jan 29, 2024
kt3k commentedon Jan 29, 2024
WebSocketStreamis designed based on Web Standard proposal whatwg/websockets#48 It's not a good idea to design it in our own way.ref: #8831
kt3k commentedon Jan 30, 2024
Closing as the original issue is solved and nothing is actionable further.
You are encouraged to create 3rd party module to address those pain points