diff options
author | isaacs <i@izs.me> | 2013-03-02 10:20:33 -0800 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-03-02 11:26:39 -0800 |
commit | 2106ef000c7d891385d5480a61d95327caf8e277 (patch) | |
tree | cd77e0c4e60727557947003737786a1ad7f7f88c /lib/net.js | |
parent | 47e115063b24e83fe72acb36f76667ba2216cdd5 (diff) | |
download | android-node-v8-2106ef000c7d891385d5480a61d95327caf8e277.tar.gz android-node-v8-2106ef000c7d891385d5480a61d95327caf8e277.tar.bz2 android-node-v8-2106ef000c7d891385d5480a61d95327caf8e277.zip |
net: Provide better error when writing after FIN
The stock writable stream "write after end" message is overly vague, if
you have clearly not called end() yourself yet.
When we receive a FIN from the other side, and call destroySoon() as a
result, then generate an EPIPE error (which is what would happen if you
did actually write to the socket), with a message explaining what
actually happened.
Diffstat (limited to 'lib/net.js')
-rw-r--r-- | lib/net.js | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/net.js b/lib/net.js index 934f9f2ea2..023d30b173 100644 --- a/lib/net.js +++ b/lib/net.js @@ -240,8 +240,31 @@ function onSocketEnd() { this.read(0); } - if (!this.allowHalfOpen) + if (!this.allowHalfOpen) { + this.write = writeAfterFIN; this.destroySoon(); + } +} + +// Provide a better error message when we call end() as a result +// of the other side sending a FIN. The standard 'write after end' +// is overly vague, and makes it seem like the user's code is to blame. +function writeAfterFIN(chunk, encoding, cb) { + if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + var er = new Error('This socket has been closed by the other party'); + er.code = 'EPIPE'; + var self = this; + // TODO: defer error events consistently everywhere, not just the cb + self.emit('error', er); + if (typeof cb === 'function') { + process.nextTick(function() { + cb(er); + }); + } } exports.Socket = Socket; @@ -708,6 +731,9 @@ function connect(self, address, port, addressType, localAddress) { Socket.prototype.connect = function(options, cb) { + if (this.write !== Socket.prototype.write) + this.write = Socket.prototype.write; + if (typeof options !== 'object') { // Old API: // connect(port, [host], [cb]) |