Skip to content

Commit aa63ba9

Browse files
scriptypekornelski
authored andcommitted
Listen for FormData error events
- Add ENOENT case to tests
1 parent 94878a5 commit aa63ba9

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

lib/node/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,13 @@ for (var key in requestBase) {
177177
*/
178178

179179
Request.prototype.attach = function(field, file, filename){
180-
if (!this._formData) this._formData = new FormData();
180+
if (!this._formData) {
181+
this._formData = new FormData();
182+
this._formData.on('error', function(err) {
183+
this.emit('error', err);
184+
this.abort();
185+
}.bind(this));
186+
}
181187
if ('string' == typeof file) {
182188
if (!filename) filename = file;
183189
debug('creating `fs.ReadStream` instance for file: %s', file);

lib/node/part.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ Part.prototype._attach = function(){
125125
if (!this._name) throw new Error('must call `Part#name()` first!');
126126

127127
// add `this` Stream's readable side as a stream for this Part
128-
if (!this._req._formData) this._req._formData = new FormData();
128+
if (!this._req._formData) {
129+
this._req._formData = new FormData();
130+
this._req._formData.on('error', function(err) {
131+
this._req.emit('error', err);
132+
this._req.abort();
133+
}.bind(this));
134+
}
129135
this._req._formData.append(this._name, this, {
130136
contentType: this._type,
131137
filename: this._filename

lib/request-base.js

+4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ exports.field = function(name, val) {
164164
if (!this._formData) {
165165
var FormData = require('form-data'); // browserify compatible. May throw if FormData is not supported natively.
166166
this._formData = new FormData();
167+
this._formData.on('error', function(err) {
168+
this.emit('error', err);
169+
this.abort();
170+
}.bind(this));
167171
}
168172
this._formData.append(name, val);
169173
return this;

test/node/multipart.js

+15
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ describe('Request', function(){
177177
done();
178178
})
179179
})
180+
it('filesystem errors should be caught', function(done){
181+
var timestamp = Date.now()
182+
request
183+
.post(base + '/echo')
184+
.attach('filedata', 'test/node/fixtures/lorem-' + timestamp + '.ext')
185+
.on('error', function(err) {
186+
err.code.should.equal('ENOENT')
187+
err.path.should.equal('test/node/fixtures/lorem-' + timestamp + '.ext')
188+
done()
189+
})
190+
.end(function (err, res) {
191+
console.log('END')
192+
done(new Error("Request should have been aborted earlier!"))
193+
})
194+
})
180195
})
181196
})
182197

0 commit comments

Comments
 (0)