question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multer does not throw an error when limits: fileSize is exceeded and hangs

See original GitHub issue

I have the following code to handle profile pic uploads. Everything works fine if under the limits: fileSize, but when fileSize is exceeded no error is thrown. Error handling based on https://github.com/expressjs/multer/issues/336. I have a custom disk storage engine. The problem with this is that I cannot send any error message or a response to react frontend.

var storage = ProfileStorage({
  square: true,
  responsive: true,
  greyscale: false,
  quality: 60
});

var limits = {
  //files: 1, // allow only 1 file per request
  fileSize: 100 * 1024
};

var fileFilter = function(req, file, cb) {
  var allowedMimes = ['image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'];

  if (_.includes(allowedMimes, file.mimetype)) {
    cb(null, true);
  } else {
    cb(new Error('Invalid file type. Only jpg, png and gif image files are allowed.'));
  }
};

var upload = multer({
  storage: storage,
  limits: limits,
  fileFilter: fileFilter
});

The post method:

var profileUpload = upload.single('Profiilikuva');

module.exports = app => {
  app.post('/api/profile/save', requireLogin, (req, res) => {
    console.log('Entered profile save');

    profileUpload(req, res, (err) => {
      console.log('Entered profile upload.');
      if (err) {
        console.log('Error from multer: ', err);
        return res.status(400).send(err);
      }

      if (req.file) {
        console.log('Stored file: ', req.file.filename); // Storage engine stores under random filename
      }

      const {firstName, lastName, email, address,
        postalCode, descriptionText, lat, lng } = req.body;

      req.user.profileCompleted = true;
      req.user.firstName = firstName;
      // some more of these...
      req.user.save().then((user) => { 
        console.log('User saved');
        return res.send(user);
      }).catch(err => {
        console.log('Error saving user. ', err);
        return res.status(400).send('Error while saving user information');
      });
    });
  });
};

Log output when uploading 75KB:

Entered profile save
_handleFile called. { fieldname: 'Profiilikuva',
   originalname: '75KB.jpg',
   encoding: '7bit',
   mimetype: 'image/jpeg' }
 Entered profile upload.
 Stored file:  0b3dc28730ecc387115a03b3f860af20_lg.jpg
 User saved

Log output when uploading 190KB:

 Entered profile save
 _handleFile called. { fieldname: 'Profiilikuva',
   originalname: '190KB.jpg',
   encoding: '7bit',
   mimetype: 'image/jpeg' }
_removeFile called

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:13
  • Comments:30 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
sainisagar310commented, Jul 29, 2019

I am also facing the same issue. It doesn’t throw an error when the file limit exceeded.

4reactions
beac0ncommented, Jul 28, 2018

“Though I think it is my custom storage engine that messes things up” I am using the google cloud storage engine for buckets.

“Seems the error handling is a bit tricky.” That’s a huge understatement.

I think the issue might have something to do with the file streams. As far as I know, the file streams for writing to the file system have more meta information than, e.g. streams to google storage buckets.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle Multer fileSize error when uploading large files
MulterError ) { console.log("file too large")} and using upload as middleware to handle the error but it doesn't seem to work and my...
Read more >
Changing the File Upload Size Limit - Jotform
When uploading a project file, a Maximum File Size Exceeded error displays and you are not able to submit your project. This happens...
Read more >
Fix "Unexpected field" Error From Multer - Maxim Orlov
The foremost reason for the "Unexpected field" error is when the file input name doesn't match the argument passed to the multer function....
Read more >
This version has been deprecated - multer - npm
Event handler triggered when the number of files exceed the specification in the limit object. No more files will be parsed after the...
Read more >
Node.js Multer File Upload Type Validation Filters and Limit ...
Node.js Multer File Upload Type Validation Filters and Limit File Size and Error Handling in ExpressDownload the full source code of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found