Skip to content

Commit 28837f3

Browse files
committed
Prevent crash on upgrade errors
1 parent 5c64cef commit 28837f3

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

apps/webapp/server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ if (process.env.HTTP_SERVER_DISABLED !== "true") {
120120
)}`
121121
);
122122

123+
socket.on("error", (err) => {
124+
console.error("Connection upgrade error:", err);
125+
});
126+
123127
const url = new URL(req.url ?? "", "http://localhost");
124128

125129
// Upgrade socket.io connection
@@ -133,6 +137,7 @@ if (process.env.HTTP_SERVER_DISABLED !== "true") {
133137

134138
// Only upgrade the connecting if the path is `/ws`
135139
if (url.pathname !== "/ws") {
140+
// Setting the socket.destroy() error param causes an error event to be emitted which needs to be handled with socket.on("error") to prevent uncaught exceptions.
136141
socket.destroy(
137142
new Error(
138143
"Cannot connect because of invalid path: Please include `/ws` in the path of your upgrade request."

0 commit comments

Comments
 (0)