File tree 4 files changed +33
-2
lines changed
4 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -177,7 +177,13 @@ for (var key in requestBase) {
177
177
*/
178
178
179
179
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
+ }
181
187
if ( 'string' == typeof file ) {
182
188
if ( ! filename ) filename = file ;
183
189
debug ( 'creating `fs.ReadStream` instance for file: %s' , file ) ;
Original file line number Diff line number Diff line change @@ -125,7 +125,13 @@ Part.prototype._attach = function(){
125
125
if ( ! this . _name ) throw new Error ( 'must call `Part#name()` first!' ) ;
126
126
127
127
// 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
+ }
129
135
this . _req . _formData . append ( this . _name , this , {
130
136
contentType : this . _type ,
131
137
filename : this . _filename
Original file line number Diff line number Diff line change @@ -164,6 +164,10 @@ exports.field = function(name, val) {
164
164
if ( ! this . _formData ) {
165
165
var FormData = require ( 'form-data' ) ; // browserify compatible. May throw if FormData is not supported natively.
166
166
this . _formData = new FormData ( ) ;
167
+ this . _formData . on ( 'error' , function ( err ) {
168
+ this . emit ( 'error' , err ) ;
169
+ this . abort ( ) ;
170
+ } . bind ( this ) ) ;
167
171
}
168
172
this . _formData . append ( name , val ) ;
169
173
return this ;
Original file line number Diff line number Diff line change @@ -177,6 +177,21 @@ describe('Request', function(){
177
177
done ( ) ;
178
178
} )
179
179
} )
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
+ } )
180
195
} )
181
196
} )
182
197
You can’t perform that action at this time.
0 commit comments