-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose error status codes #1892
Comments
I'm not very happy with it because it is an implementation detail and this is the reason why it is "hidden" behind a symbol. Not all errors have it. That info is already available in the |
It is not sent, it is simply used as the value of the |
Oh! That's very surprising behaviour, especially that it fires a close event without a close frame in that case. Why isn't this sent? The websockets spec doesn't strictly require you to always send that close frame, but it is a 'SHOULD' if the connection is established before you hit the error, so it's supposed to be sent unless there's a specific reason not to:
(from https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.7)
Ok, fair enough. If you're not happy exposing the close status codes, would you be open to creating some other kind of recognizable error codes, or using separate error classes, to allow applications to recognize different types of websocket error? Right now I'm looking at matching various error messages using regular expressions, which is super fragile and potentially open to abuse if external clients can inject any content into error messages anywhere. It would be great to have a better option! |
It is to allow the connection to be closed as soon as possible when an error occurs. There might be buffered data to be sent before the close frame so we should wait for the
Can't you already do that using the status code from the |
For the spec recommendation we could use a best effort approach and do something like this: diff --git a/lib/websocket.js b/lib/websocket.js
index 83b471d..040c41a 100644
--- a/lib/websocket.js
+++ b/lib/websocket.js
@@ -809,8 +809,13 @@ function receiverOnError(err) {
websocket._socket.removeListener('data', socketOnData);
- websocket._readyState = WebSocket.CLOSING;
websocket._closeCode = err[kStatusCode];
+
+ if (websocket._readyState !== WebSocket.CLOSING) {
+ websocket._readyState = WebSocket.CLOSING;
+ websocket._sender.close(websocket._closeCode, '', !websocket._isServer);
+ }
+
websocket.emit('error', err);
websocket._socket.destroy();
} but if the other peer receives the close frame, it will try to send back a close frame on an already closed connection. It's a waste of resources. |
The close event is unfortunately ambiguous in this case right now, because it fires for both local errors and for remotely received close messages. Because of that, you have to process the error events and close events together to tell the difference. It would be much easier to do this if there was some kind of error code directly attached to error events. My case is a bit unusual, since I'm doing some low-level networking logging and analysis of my websocket traffic, so I'm especially interested in exactly how and why these connections fail. For basic websocket applications that won't matter, but I do think it'd be useful to have the error code available anyway for the people who do care about this, and I imagine most advanced websocket deployments will eventually want to know this kind of data.
I've done some more digging into the error logic in the spec, and I think I've now run into three things that differ between how WS works in error cases and what the spec says should happen. I've pulled them out into #1898 to track this and separate the topics before everything gets too mixed up. In short, when there's an error:
|
Yes, they are emitted one after the other.
|
How would you like to procede here? Adding codes like |
Yes, something like that would work perfectly for me - an The more specific the better I guess? It seems easy and convenient to have a different code for distinct type of failure rather than grouping them together unnecessarily. I'd share codes if the same parsing failure can happen in different places, but I don't know if that's relevant. |
Can you help me to find code names for all the errors created by the
|
Sorry for commenting on this closed issue, but I think exposing error codes would be ideal for the WebSocket client too (in |
@felixputera feel free to open a PR. |
Description
When ws throws errors due to received content, it associates a websocket status code with them, which specifies the status code that will be sent when closing the connection.
It attaches this to the error behind a symbol called
kStatusCode
(used here, defined here), keeping that status code as internal private state that can't be easily or reliably accessed by application code.That is inconvenient, because in some cases this is useful information for applications too, e.g. to tracking metrics of websocket failure reasons, or to implement various advanced error handling scenarios that depend on type of protocol failure involved.
Would you be open to making this a non-symbol property, so it's available as a reliable part of the API for error handling?
The text was updated successfully, but these errors were encountered: