From 7987448b911083f2b3332f78b664ad1ed4358f12 Mon Sep 17 00:00:00 2001 From: Franziska Hinkelmann Date: Tue, 11 Sep 2012 21:52:38 -0400 Subject: [PATCH 1/3] emit 'error' when writing or opening of WriteStream fails --- lib/file.js | 4 ++++ lib/incoming_form.js | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/file.js b/lib/file.js index dad8d5f7..f71066c4 100644 --- a/lib/file.js +++ b/lib/file.js @@ -46,6 +46,10 @@ File.prototype._backwardsCompatibility = function() { File.prototype.open = function() { this._writeStream = new WriteStream(this.path); + var self = this; + this._writeStream.on('error', function (err) { + self.emit('writeError', err); + }); }; File.prototype.write = function(buffer, cb) { diff --git a/lib/incoming_form.js b/lib/incoming_form.js index 8cea0b9b..f7f89895 100644 --- a/lib/incoming_form.js +++ b/lib/incoming_form.js @@ -202,6 +202,10 @@ IncomingForm.prototype.handlePart = function(part) { hash: self.hash }); + file.on('writeError', function(error) { + self._error(new Error(error)); + }); + this.emit('fileBegin', part.name, file); file.open(); From c3e6ca6a526b1279a6b438aa6acda79581a1ba1f Mon Sep 17 00:00:00 2001 From: Franziska Hinkelmann Date: Wed, 26 Jun 2013 15:27:10 -0400 Subject: [PATCH 2/3] emit previous error, not new Error --- lib/file.js | 6 +++--- lib/incoming_form.js | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/file.js b/lib/file.js index f71066c4..631f1020 100644 --- a/lib/file.js +++ b/lib/file.js @@ -45,10 +45,10 @@ File.prototype._backwardsCompatibility = function() { }; File.prototype.open = function() { - this._writeStream = new WriteStream(this.path); var self = this; - this._writeStream.on('error', function (err) { - self.emit('writeError', err); + self._writeStream = new WriteStream(this.path); + self._writeStream.on('error', function (err) { + self.emit('error', err); }); }; diff --git a/lib/incoming_form.js b/lib/incoming_form.js index f7f89895..26676cdf 100644 --- a/lib/incoming_form.js +++ b/lib/incoming_form.js @@ -202,15 +202,17 @@ IncomingForm.prototype.handlePart = function(part) { hash: self.hash }); - file.on('writeError', function(error) { - self._error(new Error(error)); - }); + this.emit('fileBegin', part.name, file); file.open(); this.openedFiles.push(file); - + + file.on('error', function(error) { + self._error(error); + }); + part.on('data', function(buffer) { self.pause(); file.write(buffer, function() { From 3fcc8e391b67f7009fab9b8dfb2057dad40852d9 Mon Sep 17 00:00:00 2001 From: Franziska Hinkelmann Date: Wed, 26 Jun 2013 17:27:52 -0400 Subject: [PATCH 3/3] catch exception if fs.unlink does not work --- lib/incoming_form.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/incoming_form.js b/lib/incoming_form.js index 26676cdf..5662df43 100644 --- a/lib/incoming_form.js +++ b/lib/incoming_form.js @@ -263,8 +263,15 @@ IncomingForm.prototype._error = function(err) { this.emit('error', err); this.openedFiles.forEach(function(file) { - file._writeStream.destroy(); - setTimeout(fs.unlink, 0, file.path); + file._writeStream.destroy(); + + setTimeout(function() { + try{ + fs.unlink(); + } catch (e) { + console.log("Error unlinking file: " + err); + } + }, 0, file.path); }); };