Skip to content

Commit 805170c

Browse files
turakvladLinusU
authored andcommitted
Make Multer errors inherit from MulterError (#641)
* Add "isMulter" property set to true to an error instance * Remove "isMulter" property from an error instance * Add ./lib/multer-error.js file * Update ./lib/make-error.js file * Require and export MulterError class in ./index.js file * Fix typo in ./lib/make-error.js file * Add test case to ./test/error-handling.js file * Update README.md * Add `if` statement for the `field` property * Remove ./lib/make-error.js file and replace all the `makeError` invocations with `new MulterError` class
1 parent 2592abe commit 805170c

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,23 +266,25 @@ function fileFilter (req, file, cb) {
266266

267267
## Error handling
268268

269-
When encountering an error, multer will delegate the error to express. You can
269+
When encountering an error, Multer will delegate the error to Express. You can
270270
display a nice error page using [the standard express way](http://expressjs.com/guide/error-handling.html).
271271

272-
If you want to catch errors specifically from multer, you can call the
273-
middleware function by yourself.
272+
If you want to catch errors specifically from Multer, you can call the
273+
middleware function by yourself. Also, if you want to catch only [the Multer errors](https://github.com/expressjs/multer/blob/master/lib/make-error.js#L1-L9), you can use the `MulterError` class that is attached to the `multer` object itself (e.g. `err instanceof multer.MulterError`).
274274

275275
```javascript
276+
var multer = require('multer')
276277
var upload = multer().single('avatar')
277278

278279
app.post('/profile', function (req, res) {
279280
upload(req, res, function (err) {
280-
if (err) {
281-
// An error occurred when uploading
282-
return
281+
if (err instanceof multer.MulterError) {
282+
// A Multer error occurred when uploading.
283+
} else {
284+
// An unknown error occurred when uploading.
283285
}
284286

285-
// Everything went fine
287+
// Everything went fine.
286288
})
287289
})
288290
```

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var makeError = require('./lib/make-error')
21
var makeMiddleware = require('./lib/make-middleware')
32

43
var diskStorage = require('./storage/disk')
54
var memoryStorage = require('./storage/memory')
5+
var MulterError = require('./lib/multer-error')
66

77
function allowAll (req, file, cb) {
88
cb(null, true)
@@ -37,7 +37,7 @@ Multer.prototype._makeMiddleware = function (fields, fileStrategy) {
3737

3838
function wrappedFileFilter (req, file, cb) {
3939
if ((filesLeft[file.fieldname] || 0) <= 0) {
40-
return cb(makeError('LIMIT_UNEXPECTED_FILE', file.fieldname))
40+
return cb(new MulterError('LIMIT_UNEXPECTED_FILE', file.fieldname))
4141
}
4242

4343
filesLeft[file.fieldname] -= 1
@@ -101,3 +101,4 @@ function multer (options) {
101101
module.exports = multer
102102
module.exports.diskStorage = diskStorage
103103
module.exports.memoryStorage = memoryStorage
104+
module.exports.MulterError = MulterError

lib/make-middleware.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var onFinished = require('on-finished')
55
var appendField = require('append-field')
66

77
var Counter = require('./counter')
8-
var makeError = require('./make-error')
8+
var MulterError = require('./multer-error')
99
var FileAppender = require('./file-appender')
1010
var removeUploadedFiles = require('./remove-uploaded-files')
1111

@@ -76,7 +76,7 @@ function makeMiddleware (setup) {
7676
}
7777

7878
function abortWithCode (code, optionalField) {
79-
abortWithError(makeError(code, optionalField))
79+
abortWithError(new MulterError(code, optionalField))
8080
}
8181

8282
// handle text field data
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var util = require('util')
2+
13
var errorMessages = {
24
'LIMIT_PART_COUNT': 'Too many parts',
35
'LIMIT_FILE_SIZE': 'File too large',
@@ -8,11 +10,14 @@ var errorMessages = {
810
'LIMIT_UNEXPECTED_FILE': 'Unexpected field'
911
}
1012

11-
function makeError (code, optionalField) {
12-
var err = new Error(errorMessages[code])
13-
err.code = code
14-
if (optionalField) err.field = optionalField
15-
return err
13+
function MulterError (code, field) {
14+
Error.captureStackTrace(this, this.constructor)
15+
this.name = this.constructor.name
16+
this.message = errorMessages[code]
17+
this.code = code
18+
if (field) this.field = field
1619
}
1720

18-
module.exports = makeError
21+
util.inherits(MulterError, Error)
22+
23+
module.exports = MulterError

test/error-handling.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ function withLimits (limits, fields) {
1414
}
1515

1616
describe('Error Handling', function () {
17+
it('should be an instance of both `Error` and `MulterError` classes in case of the Multer\'s error', function (done) {
18+
var form = new FormData()
19+
var storage = multer.diskStorage({ destination: os.tmpdir() })
20+
var upload = multer({storage: storage}).fields([
21+
{ name: 'small0', maxCount: 1 }
22+
])
23+
24+
form.append('small0', util.file('small0.dat'))
25+
form.append('small0', util.file('small0.dat'))
26+
27+
util.submitForm(upload, form, function (err, req) {
28+
assert.equal(err instanceof Error, true)
29+
assert.equal(err instanceof multer.MulterError, true)
30+
done()
31+
})
32+
})
33+
1734
it('should respect parts limit', function (done) {
1835
var form = new FormData()
1936
var parser = withLimits({ parts: 1 }, [

0 commit comments

Comments
 (0)