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.

No simple way to tell the client if an error occured

See original GitHub issue

All of the error handling functions, onFileSizeLimit, onError etc… have no way of either telling the client that an error has happened or passing the fact an error happened to the next middle ware:

  router.use('/uploads', multer({
    dest: './uploads',
    limits: { files: 2 },
    onFilesLimit: function () {
      // No way to add to the request that an error occurred, or respond to the client
   }
  });

  router.post('/uploads', function(req, res) {
    // Cannot tell that any of the limits has been reached so cannot inform the client
  });

You probably don’t want to respond to the client directly when a limit has been hit, but you should at least be able to tell that an error occurred later on.

Something along the lines of:

  router.use('/uploads', multer({
    dest: './uploads',
    limits: { files: 2 },
    onFilesLimit: function (file_data) {
      file_data.files_limit_reached = true;
   }
  });

  router.post('/uploads', function(req, res) {
    if (file_data.files_limit_reached) {
        res.json({ error: "file limit reached" });
    }
  });

So that rather then removing the files that have hit an error or exceeded a limit they are flagged with an error message.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jasson15commented, Mar 13, 2015

hi @qqilihq, I’d like to share my solution. please take a look:

app.use('/route', multer({
  dest: './upload/',
  limits: {
    fileSize: 1024*1024, // at most 1MB
  },
  // setting attributes of `file` is useful for passing info to the
  // next middleware
  onFileSizeLimit: function (file) {
    fs.unlink('./' + file.path); // delete the partially written file
    file.failed = true;
  }
}));

After the middleware above, we can do the trick as below:

app.use('/route', function(req, res, next){
  // take the 1st file for example, of course you can do a for-loop instead
  var files = req.files.yourfilename; // yourfilename is the field name defined in front-end
  var file = files[0];
  if (!file.failed) {
    // gotcha!
  }
}

I think this might help you. One more thing might be helpful: onFileUploadComplete does not mean success, any single file will fire this event one time, even if it runs over filesize limit.

1reaction
jwhitmarshcommented, Feb 5, 2016

Why has onFileSizeLimit been removed? Is there an alternative to it that we could use?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for REST API Error Handling - Baeldung
The simplest way we handle errors is to respond with an appropriate status code. Here are some common response codes: ... While basic,...
Read more >
Handling operation errors - Apollo GraphQL Docs
Apollo Client helps you handle these errors according to their type, enabling you to show appropriate information to the user when an error...
Read more >
How To Troubleshoot Common HTTP Error Codes
This guide focuses on identifying and troubleshooting the most commonly encountered HTTP error codes, i.e. 4xx and 5xx status codes, ...
Read more >
How to implement error handling in SQL Server - SQLShack
First, generally you shouldn't be "swallowing" (suppressing) errors. Inside a CATCH block, after handling the error, you should rethrow it so ...
Read more >
"Cannot connect the computer to the server" error message ...
Note This issue occurs on the client computer when connecting to the server in the ... If you're not on the computer that...
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