joyent/node — pull request #2394 by interruptz (2011-12-21 05:37:18+00:00)
Title: net: Tell the user why socket.write() was failed when the connection is already closed.

When the connection is closed or reset by peer or not initialized, socket._handle is not existing. so If you try to write in that case, you would receive an error on current version:

TypeError: Cannot call method 'write' of undefined
at Socket._write (net.js:434:31)
at Socket.write (net.js:426:15)
at repl:1:4
at REPLServer.eval (repl.js:80:21)
at Interface. (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream. (readline.js:82:12)

In that case, As far as I know, on POSIX, If you attempt to write something to socket which is closed already, it should raise `EPIPE` exception.

So I changed `net.js` to raise the exception forcibly.

Error: write EPIPE
at errnoException (net.js:637:11)
at Socket._write (net.js:460:24)
at Socket.write (net.js:438:15)
at repl:1:4
at REPLServer.eval (repl.js:80:21)
at Interface. (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)

The test case is included for your convenience at `test/simple/test-net-epipe.js`.