@@ -33,6 +33,9 @@ const PersistentFile = require('./PersistentFile');
33
33
const VolatileFile = require ( './VolatileFile' ) ;
34
34
const DummyParser = require ( './parsers/Dummy' ) ;
35
35
const MultipartParser = require ( './parsers/Multipart' ) ;
36
+ const errors = require ( './FormidableError.js' ) ;
37
+
38
+ const { FormidableError } = errors ;
36
39
37
40
function hasOwnProp ( obj , key ) {
38
41
return Object . prototype . hasOwnProperty . call ( obj , key ) ;
@@ -63,7 +66,6 @@ class IncomingForm extends EventEmitter {
63
66
64
67
this . _setUpRename ( ) ;
65
68
66
-
67
69
this . _flushing = 0 ;
68
70
this . _fieldsSize = 0 ;
69
71
this . _fileSize = 0 ;
@@ -75,8 +77,9 @@ class IncomingForm extends EventEmitter {
75
77
. filter ( Boolean ) ;
76
78
77
79
if ( this . options . enabledPlugins . length === 0 ) {
78
- throw new Error (
80
+ throw new FormidableError (
79
81
'expect at least 1 enabled builtin plugin, see options.enabledPlugins' ,
82
+ errors . missingPlugin ,
80
83
) ;
81
84
}
82
85
@@ -91,7 +94,10 @@ class IncomingForm extends EventEmitter {
91
94
92
95
use ( plugin ) {
93
96
if ( typeof plugin !== 'function' ) {
94
- throw new Error ( '.use: expect `plugin` to be a function' ) ;
97
+ throw new FormidableError (
98
+ '.use: expect `plugin` to be a function' ,
99
+ errors . pluginFunction ,
100
+ ) ;
95
101
}
96
102
this . _plugins . push ( plugin . bind ( this ) ) ;
97
103
return this ;
@@ -183,7 +189,7 @@ class IncomingForm extends EventEmitter {
183
189
} )
184
190
. on ( 'aborted' , ( ) => {
185
191
this . emit ( 'aborted' ) ;
186
- this . _error ( new Error ( 'Request aborted' ) ) ;
192
+ this . _error ( new FormidableError ( 'Request aborted' , errors . aborted ) ) ;
187
193
} )
188
194
. on ( 'data' , ( buffer ) => {
189
195
try {
@@ -211,7 +217,13 @@ class IncomingForm extends EventEmitter {
211
217
this . _parseContentType ( ) ;
212
218
213
219
if ( ! this . _parser ) {
214
- this . _error ( new Error ( 'no parser found' ) ) ;
220
+ this . _error (
221
+ new FormidableError (
222
+ 'no parser found' ,
223
+ errors . noParser ,
224
+ 415 , // Unsupported Media Type
225
+ ) ,
226
+ ) ;
215
227
return ;
216
228
}
217
229
@@ -225,7 +237,9 @@ class IncomingForm extends EventEmitter {
225
237
return null ;
226
238
}
227
239
if ( ! this . _parser ) {
228
- this . _error ( new Error ( 'uninitialized parser' ) ) ;
240
+ this . _error (
241
+ new FormidableError ( 'uninitialized parser' , errors . uninitializedParser ) ,
242
+ ) ;
229
243
return null ;
230
244
}
231
245
@@ -254,7 +268,12 @@ class IncomingForm extends EventEmitter {
254
268
255
269
_handlePart ( part ) {
256
270
if ( part . filename && typeof part . filename !== 'string' ) {
257
- this . _error ( new Error ( `the part.filename should be string when exists` ) ) ;
271
+ this . _error (
272
+ new FormidableError (
273
+ `the part.filename should be string when it exists` ,
274
+ errors . filenameNotString ,
275
+ ) ,
276
+ ) ;
258
277
return ;
259
278
}
260
279
@@ -278,8 +297,10 @@ class IncomingForm extends EventEmitter {
278
297
this . _fieldsSize += buffer . length ;
279
298
if ( this . _fieldsSize > this . options . maxFieldsSize ) {
280
299
this . _error (
281
- new Error (
300
+ new FormidableError (
282
301
`options.maxFieldsSize (${ this . options . maxFieldsSize } bytes) exceeded, received ${ this . _fieldsSize } bytes of field data` ,
302
+ errors . maxFieldsSizeExceeded ,
303
+ 413 , // Payload Too Large
283
304
) ,
284
305
) ;
285
306
return ;
@@ -312,16 +333,20 @@ class IncomingForm extends EventEmitter {
312
333
this . _fileSize += buffer . length ;
313
334
if ( this . _fileSize < this . options . minFileSize ) {
314
335
this . _error (
315
- new Error (
336
+ new FormidableError (
316
337
`options.minFileSize (${ this . options . minFileSize } bytes) inferior, received ${ this . _fileSize } bytes of file data` ,
338
+ errors . smallerThanMinFileSize ,
339
+ 400 ,
317
340
) ,
318
341
) ;
319
342
return ;
320
343
}
321
344
if ( this . _fileSize > this . options . maxFileSize ) {
322
345
this . _error (
323
- new Error (
346
+ new FormidableError (
324
347
`options.maxFileSize (${ this . options . maxFileSize } bytes) exceeded, received ${ this . _fileSize } bytes of file data` ,
348
+ errors . biggerThanMaxFileSize ,
349
+ 413 ,
325
350
) ,
326
351
) ;
327
352
return ;
@@ -338,8 +363,10 @@ class IncomingForm extends EventEmitter {
338
363
part . on ( 'end' , ( ) => {
339
364
if ( ! this . options . allowEmptyFiles && this . _fileSize === 0 ) {
340
365
this . _error (
341
- new Error (
366
+ new FormidableError (
342
367
`options.allowEmptyFiles is false, file size should be greather than 0` ,
368
+ errors . noEmptyFiles ,
369
+ 400 ,
343
370
) ,
344
371
) ;
345
372
return ;
@@ -361,7 +388,13 @@ class IncomingForm extends EventEmitter {
361
388
}
362
389
363
390
if ( ! this . headers [ 'content-type' ] ) {
364
- this . _error ( new Error ( 'bad content-type header, no content-type' ) ) ;
391
+ this . _error (
392
+ new FormidableError (
393
+ 'bad content-type header, no content-type' ,
394
+ errors . missingContentType ,
395
+ 400 ,
396
+ ) ,
397
+ ) ;
365
398
return ;
366
399
}
367
400
@@ -379,8 +412,10 @@ class IncomingForm extends EventEmitter {
379
412
} catch ( err ) {
380
413
// directly throw from the `form.parse` method;
381
414
// there is no other better way, except a handle through options
382
- const error = new Error (
415
+ const error = new FormidableError (
383
416
`plugin on index ${ idx } failed with: ${ err . message } ` ,
417
+ errors . pluginFailed ,
418
+ 500 ,
384
419
) ;
385
420
error . idx = idx ;
386
421
throw error ;
@@ -524,7 +559,11 @@ class IncomingForm extends EventEmitter {
524
559
fieldsCount += 1 ;
525
560
if ( fieldsCount > this . options . maxFields ) {
526
561
this . _error (
527
- new Error ( `options.maxFields (${ this . options . maxFields } ) exceeded` ) ,
562
+ new FormidableError (
563
+ `options.maxFields (${ this . options . maxFields } ) exceeded` ,
564
+ errors . maxFieldsExceeded ,
565
+ 413 ,
566
+ ) ,
528
567
) ;
529
568
}
530
569
} ) ;
0 commit comments