Skip to content
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

Can Connection#send() throw an error? #185

Open
ibc opened this issue Feb 17, 2015 · 3 comments
Open

Can Connection#send() throw an error? #185

ibc opened this issue Feb 17, 2015 · 3 comments

Comments

@ibc
Copy link
Collaborator

ibc commented Feb 17, 2015

If I call send() or sendUTF() or sendBytes() on an already closed Connection, or if sending fails due to connection error, would it throw an error?

It is not documented.

@theturtle32
Copy link
Owner

No. The behavior for this is inherited from the Socket implementation in Node core. If there is an error, such as when writing to a closed socket, it will emit an error event on nextTick. I believe earlier versions of Node did throw in some cases, so I used to normalize that behavior using try/catch, but that's not necessary anymore because Node never throws when calling write except when the user calls it with invalid parameters.

The main difference here is that if nobody is listening for an error event on the WebSocketConnection instance, it will silently swallow the error event instead of letting it bubble up and crash the application. My reasoning for that was that it will actually be pretty common to run into these errors in production, and it will immediately trigger a close event, and there's no other handling that needs to be done by the user for these errors. I do expose the error event for anyone who finds it useful in their particular situation, but if no listeners are added for the error event, then any error events emitted by the underlying Socket will be silently discarded.

See io.js/net.js - writeAfterFIN

See also WebSocketConnection.prototype.handleSocketError

@theturtle32
Copy link
Owner

Should I add a comment to the documentation to this effect?

@ibc
Copy link
Collaborator Author

ibc commented Feb 18, 2015

Thanks for the explanation! Yes, I would find some text about this subject really useful in the API doc :)

@theturtle32 theturtle32 changed the title Can Connection#send() thrown an error? Can Connection#send() throw an error? Feb 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants