Catch/handle WebSocketStream connection reset error

How/where to catch this error? I am using WebSocketStream and catching on wss.opened and on wss.closed still this error is uncaught from somewhere -
error: Uncaught (in promise) WebSocketError: connection reset
at new WebSocketError (ext:deno_websocket/02_websocketstream.js:478:5)
at Object.pull (ext:deno_websocket/02_websocketstream.js:270:31)
at eventLoopTick (ext:core/01_core.js:168:7)
error: Uncaught (in promise) WebSocketError: connection reset
at new WebSocketError (ext:deno_websocket/02_websocketstream.js:478:5)
at Object.pull (ext:deno_websocket/02_websocketstream.js:270:31)
at eventLoopTick (ext:core/01_core.js:168:7)
19 Replies
√(sec²(冥)➖1)
@Deno AI Helper
Deno AI Helper
Deno AI Helper7mo ago
It seems that your message is incomplete. If you have a specific question or issue related to Deno, please provide more details so I can assist you. If there are any code snippets or error messages, make sure to include them in your message. Remember, while I strive to provide accurate advice, any suggestions I make based on your message are not directly from the Deno documentation, so please double-check the accuracy of the advice given.
√(sec²(冥)➖1)
@Deno AI Helper How/where to catch this error? I am using WebSocketStream and catching on wss.opened and on wss.closed still this error is uncaught from somewhere -
error: Uncaught (in promise) WebSocketError: connection reset
at new WebSocketError (ext:deno_websocket/02_websocketstream.js:478:5)
at Object.pull (ext:deno_websocket/02_websocketstream.js:270:31)
at eventLoopTick (ext:core/01_core.js:168:7)
error: Uncaught (in promise) WebSocketError: connection reset
at new WebSocketError (ext:deno_websocket/02_websocketstream.js:478:5)
at Object.pull (ext:deno_websocket/02_websocketstream.js:270:31)
at eventLoopTick (ext:core/01_core.js:168:7)
Deno AI Helper
Deno AI Helper7mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the error message you've provided, it seems like the WebSocketError: connection reset is an uncaught promise rejection that may be occurring outside of the wss.opened and wss.closed event handlers. In Deno, you can often catch errors related to WebSocketStream by attaching a listener to the error event or by wrapping your code in a try-catch block when you're working with async operations. Here's a general example of how you might catch errors with a WebSocketStream:
try {
const wss = new WebSocketStream("ws://example.com");
await wss.connection;
wss.addEventListener("open", () => {
console.log("WebSocket opened");
});
wss.addEventListener("close", () => {
console.log("WebSocket closed");
});
wss.addEventListener("error", (e) => {
console.error("WebSocket error:", e);
});
// ... your code to handle WebSocketStream
} catch (error) {
console.error("Caught an error:", error);
}
try {
const wss = new WebSocketStream("ws://example.com");
await wss.connection;
wss.addEventListener("open", () => {
console.log("WebSocket opened");
});
wss.addEventListener("close", () => {
console.log("WebSocket closed");
});
wss.addEventListener("error", (e) => {
console.error("WebSocket error:", e);
});
// ... your code to handle WebSocketStream
} catch (error) {
console.error("Caught an error:", error);
}
Make sure that you are also handling errors that might occur during the connection phase (await wss.connection) and any other async operations you perform with the WebSocketStream. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. If the issue persists, you may want to look for more specific examples or reach out to the community for further assistance.
√(sec²(冥)➖1)
Your solution gave me this error -
Property 'addEventListener' does not exist on type 'WebSocketStream'.deno-ts(2339)
Property 'addEventListener' does not exist on type 'WebSocketStream'.deno-ts(2339)
Deno AI Helper
Deno AI Helper7mo ago
I apologize for the confusion. It appears that I made an error in my previous message. The WebSocketStream API does not have an addEventListener method. Instead, you should use the wss object to handle the stream of messages directly. Here's a corrected example of how you might handle errors with a WebSocketStream:
try {
const wss = new WebSocketStream("ws://example.com");
const { readable, writable } = await wss.connection;

// Handle incoming messages
for await (const message of readable) {
console.log("Received message:", message);
}

// Handle outgoing messages
// const writer = writable.getWriter();
// writer.write(...);

} catch (error) {
console.error("Caught an error:", error);
}

// Listen for the close event
wss.closed.then(() => {
console.log("WebSocket closed");
}).catch((error) => {
console.error("WebSocket closed with error:", error);
});
try {
const wss = new WebSocketStream("ws://example.com");
const { readable, writable } = await wss.connection;

// Handle incoming messages
for await (const message of readable) {
console.log("Received message:", message);
}

// Handle outgoing messages
// const writer = writable.getWriter();
// writer.write(...);

} catch (error) {
console.error("Caught an error:", error);
}

// Listen for the close event
wss.closed.then(() => {
console.log("WebSocket closed");
}).catch((error) => {
console.error("WebSocket closed with error:", error);
});
In this example, we're using a try-catch block to handle errors that may occur when establishing the connection or when reading messages. Additionally, we're using the closed promise to handle the case when the WebSocket connection is closed, either normally or with an error. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. If you continue to experience issues, you may want to consult the Deno community or the official documentation for more detailed guidance.
√(sec²(冥)➖1)
I still get the uncaught error -
error: Uncaught (in promise) WebSocketError: connection reset
at new WebSocketError (ext:deno_websocket/02_websocketstream.js:478:5)
at Object.pull (ext:deno_websocket/02_websocketstream.js:270:31)
at eventLoopTick (ext:core/01_core.js:168:7)
error: Uncaught (in promise) WebSocketError: connection reset
at new WebSocketError (ext:deno_websocket/02_websocketstream.js:478:5)
at Object.pull (ext:deno_websocket/02_websocketstream.js:270:31)
at eventLoopTick (ext:core/01_core.js:168:7)
Deno AI Helper
Deno AI Helper7mo ago
I'm sorry to hear that the issue persists. The error you're encountering indicates that the WebSocket connection is being reset, which could be due to network issues, the server closing the connection unexpectedly, or other reasons outside of your control. The error message suggests that the promise rejection is not being caught. In Deno, uncaught promise rejections are reported as errors. To handle such errors, you should ensure that all promises have appropriate error handling. Here's a general approach to handling errors with WebSocketStream: 1. Make sure to handle the promise returned by the wss.closed properly. 2. Use try-catch blocks around any code that could throw an exception, including where you read from the readable stream. 3. Attach a .catch() handler to any promise that could be rejected. Here's an example that demonstrates handling the closed promise and reading from the stream with error handling:
const wss = new WebSocketStream("ws://example.com");

async function handleWebSocketStream(wss) {
try {
const { readable, writable } = await wss.connection;

// Process the readable stream
try {
for await (const message of readable) {
console.log("Received message:", message);
}
} catch (readError) {
console.error("Error while reading from the WebSocket:", readError);
}

// You can also handle the writable stream here
// ...

} catch (connError) {
console.error("WebSocket connection error:", connError);
}
}

// Call the function to handle the WebSocketStream
handleWebSocketStream(wss);

// Handle the closed promise
wss.closed.catch((closeError) => {
console.error("WebSocket closed with error:", closeError);
});
const wss = new WebSocketStream("ws://example.com");

async function handleWebSocketStream(wss) {
try {
const { readable, writable } = await wss.connection;

// Process the readable stream
try {
for await (const message of readable) {
console.log("Received message:", message);
}
} catch (readError) {
console.error("Error while reading from the WebSocket:", readError);
}

// You can also handle the writable stream here
// ...

} catch (connError) {
console.error("WebSocket connection error:", connError);
}
}

// Call the function to handle the WebSocketStream
handleWebSocketStream(wss);

// Handle the closed promise
wss.closed.catch((closeError) => {
console.error("WebSocket closed with error:", closeError);
});
In this example, we have a separate
Deno AI Helper
Deno AI Helper7mo ago
handleWebSocketStream function that handles the readable stream and any errors that might occur during processing. Additionally, we catch any errors that might occur when the closed promise is settled. Please note that while this advice is intended to help you debug the issue, it is not directly from the Deno documentation. If the problem continues, I recommend consulting the Deno documentation for the latest information on handling WebSocketStream errors, or reaching out to the Deno community for more specific assistance. 🦕
√(sec²(冥)➖1)
GitHub
deno/ext/websocket/02_websocketstream.js at main · denoland/deno
A modern runtime for JavaScript and TypeScript. Contribute to denoland/deno development by creating an account on GitHub.
Deno AI Helper
Deno AI Helper7mo ago
Thank you for providing the link to the Deno source code where the error originates. It appears that the error is thrown during the internal handling of the WebSocket stream when the connection is reset. Since the error is thrown from within the Deno runtime's internal JavaScript code, it's not something that can be caught directly with a try-catch block in your application code. However, you should still be able to handle it by attaching a .catch() handler to the promise that is being rejected. If you're already handling the closed promise with a .catch() and still seeing uncaught promise rejections, it's possible that there's another part of your code where a promise from the WebSocketStream is not being handled properly. Make sure that any other operations you perform on the WebSocketStream, such as sending messages, also have proper error handling. For example, if you're using the writable stream to send messages, you should also catch errors there:
const writer = writable.getWriter();
writer.write(someData).catch((error) => {
console.error("Error sending message:", error);
});
const writer = writable.getWriter();
writer.write(someData).catch((error) => {
console.error("Error sending message:", error);
});
If you've added error handling to all the places where you interact with the WebSocketStream and you're still encountering uncaught promise rejections, it might be a bug or an unhandled edge case within Deno itself. In such a case, I would recommend reporting the issue to the Deno GitHub repository with a detailed description and a minimal reproduction code snippet, so that the Deno maintainers can investigate the problem further. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. If you're unsure, reaching out to the Deno community or filing an issue on the Deno GitHub
√(sec²(冥)➖1)
What about readable? How do I handle that?
Deno AI Helper
Deno AI Helper7mo ago
The readable property of a WebSocketStream is a ReadableStream of incoming messages. According to the documentation, when working with ReadableStream, you typically use a loop to read through the stream's data. Here's an example of how you might handle incoming messages with proper error handling:
const wss = new WebSocketStream("ws://example.com");
const { readable, writable } = await wss.connection;

async function readFromReadableStream(readableStream) {
const reader = readableStream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
// Process the value (message) here
console.log("Received message:", value);
}
} catch (error) {
console.error("Error while reading from the WebSocket:", error);
} finally {
reader.releaseLock();
}
}

readFromReadableStream(readable).catch((error) => {
console.error("Caught an error from the readable stream:", error);
});

// Don't forget to handle the closed promise as well
wss.closed.catch((error) => {
console.error("WebSocket closed with error:", error);
});
const wss = new WebSocketStream("ws://example.com");
const { readable, writable } = await wss.connection;

async function readFromReadableStream(readableStream) {
const reader = readableStream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
// Process the value (message) here
console.log("Received message:", value);
}
} catch (error) {
console.error("Error while reading from the WebSocket:", error);
} finally {
reader.releaseLock();
}
}

readFromReadableStream(readable).catch((error) => {
console.error("Caught an error from the readable stream:", error);
});

// Don't forget to handle the closed promise as well
wss.closed.catch((error) => {
console.error("WebSocket closed with error:", error);
});
In this example, we're using an async function to read from the readableStream. We use a while loop to continuously read messages until the stream is done. If an error occurs while reading from the stream, it is caught in the catch block, and we log the error. We also make sure to release the reader lock in the finally block, which is important to free up resources. Please note that this code is a general example and not taken directly from the Deno documentation. You should adapt it to fit the context of your application and ensure that it aligns with the best practices for error handling in Deno. If you need more specific
√(sec²(冥)➖1)
Do it with for await (const message of readable) {}
Deno AI Helper
Deno AI Helper7mo ago
Certainly! You can use a for await...of loop to read from the readable stream of a WebSocketStream. Here's an example based on the documentation:
const wss = new WebSocketStream("ws://example.com");
const { readable, writable } = await wss.connection;

async function processMessages(readableStream) {
try {
for await (const message of readableStream) {
console.log("Received message:", message);
// Process each message here
}
} catch (error) {
console.error("Error while reading from the WebSocket:", error);
}
}

processMessages(readable).catch((error) => {
console.error("Caught an error from the readable stream:", error);
});

// Don't forget to handle the closed promise as well
wss.closed.catch((error) => {
console.error("WebSocket closed with error:", error);
});
const wss = new WebSocketStream("ws://example.com");
const { readable, writable } = await wss.connection;

async function processMessages(readableStream) {
try {
for await (const message of readableStream) {
console.log("Received message:", message);
// Process each message here
}
} catch (error) {
console.error("Error while reading from the WebSocket:", error);
}
}

processMessages(readable).catch((error) => {
console.error("Caught an error from the readable stream:", error);
});

// Don't forget to handle the closed promise as well
wss.closed.catch((error) => {
console.error("WebSocket closed with error:", error);
});
In this code, processMessages is an async function that reads messages from the readableStream using a for await...of loop. If an error occurs while processing the stream, it will be caught and logged. After defining the function, we call it and attach a .catch() to handle any potential rejections from the promise returned by processMessages. This approach is consistent with the documentation's guidance on using async iteration to process streams. Remember to adapt this example to fit the specific needs and error handling requirements of your application. 🦕
√(sec²(冥)➖1)
I still get the uncaught error, even after catching readable and writable -
error: Uncaught (in promise) WebSocketError: connection reset
at new WebSocketError (ext:deno_websocket/02_websocketstream.js:478:5)
at Object.pull (ext:deno_websocket/02_websocketstream.js:270:31)
at eventLoopTick (ext:core/01_core.js:168:7)
error: Uncaught (in promise) WebSocketError: connection reset
at new WebSocketError (ext:deno_websocket/02_websocketstream.js:478:5)
at Object.pull (ext:deno_websocket/02_websocketstream.js:270:31)
at eventLoopTick (ext:core/01_core.js:168:7)
Deno AI Helper
Deno AI Helper7mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the message you've provided, it seems like there's an uncaught promise rejection somewhere in your code or possibly within the Deno runtime itself. If you've already attached .catch() handlers to the readable, writable, and closed promises and are still seeing this error, it's possible that the error is being thrown asynchronously in a way that's not being captured by your current error handling. One thing you can do is to ensure that you're not missing any .catch() handlers on any other promises that you might be using in your code. Also, make sure that you're awaiting all asynchronous operations that could throw errors, and that you're catching those errors either with a try-catch block or a .catch() on the promise. If you've done all of this and the error persists, it might be an issue with Deno itself. In that case, you could consider opening an issue on the Deno GitHub repository with a minimal reproduction of the problem for further assistance from the Deno maintainers. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. If you're unsure, you may want to reach out to the Deno community for more specific assistance.

Did you find this page helpful?