Closes GH-85 Emit error rather than throwing.
authorisaacs <i@izs.me>
Wed, 16 Feb 2011 05:34:30 +0000 (21:34 -0800)
committerRyan Dahl <ry@tinyclouds.org>
Mon, 28 Feb 2011 01:08:14 +0000 (17:08 -0800)
Since "error" events will throw when unhandled anyhow, it makes no sense
to throw from an EventEmitter's method, especially for such a minor
misdemeanor as attempting to write to a non-writable stream.

lib/fs.js
lib/tty_win32.js

index 3eb31a9..8350c61 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -1027,7 +1027,8 @@ WriteStream.prototype.flush = function() {
 
 WriteStream.prototype.write = function(data) {
   if (!this.writable) {
-    throw new Error('stream not writable');
+    this.emit("error", new Error('stream not writable'));
+    return false;
   }
 
   this.drainable = true;
index 6ca011a..7aa137a 100644 (file)
@@ -99,7 +99,8 @@ WriteStream.prototype.isTTY = true;
 
 WriteStream.prototype.write = function(data, encoding) {
   if (!this.writable) {
-    throw new Error('stream not writable');
+    this.emit("error", new Error('stream not writable'));
+    return false;
   }
 
   if (Buffer.isBuffer(data)) {