title: How To Handle Error created by upload call in multer when the number of files limit exceeds?
body: <p>With my Error Handling, It throws the Error as expected, even though it uploads some files(till reaches the limit) and then the Error is thrown. how to throw this error before uploading some chunk files?</p>
  
  <p><a href="https://i.stack.imgur.com/SBIW8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SBIW8.png" alt="enter image description here"></a></p>
  
  <p>How to throw error before uploading, when it exceeds the limit no of files uploaded? Thanks in advance!</p>
  
  <p>Which I tried: </p>
  
  <pre><code>app.post('/upload',[
  multer({
  dest    : function(req, file, cb){
  let dest = 'uploads/1992-12-11/'
  let len = parseInt(req.files.length);
  if(len === 20){
  console.log('let us throw error');
  }
  cb(null, dest);
  }
  onError : function(err, next){
  console.log('error' + err);
  next(err);
  }
  }),
  function(req, res) {
  res.status(204).end();
  }
  });
  </code></pre>
  
  <p>Used express-fileupload to validate using multer specified technique</p>
  
  <pre><code>var multer = require('multer')
  var upload = multer().single('avatar')
  
  app.post('/profile', function (req, res) {
    upload(req, res, function (err) {
    if (err instanceof multer.MulterError) {
    // A Multer error occurred when uploading.
    } else if (err) {
    // An unknown error occurred when uploading.
    }
  
     // Everything went fine.
    })
  })
  </code></pre>
  
  <p>which returns null for console.log(req.files);</p>
  
  <p>Tried almost everything in this thread: <a href="https://stackoverflow.com/questions/23114374/file-uploading-with-express-4-0-req-files-undefined">File uploading with Express 4.0: req.files undefined</a></p>
  
  <p>Error Handling:</p>
  
  <pre><code>app.post('/upload',[
    multer({
      dest    : './uploads/1992-12-11/',
      onError : function(err, next) {
        console.log('error', err);
        next(err);
      }
   }),
   function(req, res) {
     res.status(204).end();
     }
   ]);
  </code></pre>