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.

Can't catch error from an erroneous uploadDir path with a trycatch block

See original GitHub issue

Support plan

  • which support plan is this issue covered by? (e.g. Community, Sponsor, or Enterprise):Community
  • is this issue currently blocking your project? (yes/no):no
  • is this issue affecting a production system? (yes/no):no

Context

  • node version:12.16.1
  • module (formidable) version:1.2.2
  • environment (e.g. node, browser, native, OS):node
  • used with (i.e. popular names of modules):express
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

Can’t catch error from an erroneous uploadDir path with a trycatch block

try{
  const form = formidable({ uploadDir: path.join(__dirname, "/sth-weird") })
}catch(err){
  console.log("err", err)
}

What was the result you got?

events.js:288 throw er; // Unhandled ‘error’ event ^

Error: ENOENT: no such file or directory, open ‘C:\Users\username\Documents\project\server\routers\sth-weird\a.jpg’ Emitted ‘error’ event on WriteStream instance at: at internal/fs/streams.js:320:12 at FSReqCallback.oncomplete (fs.js:154:23) { errno: -4058, code: ‘ENOENT’, syscall: ‘open’, path: ‘C:\Users\username\Documents\project\server\routers\sth-weird\a.jpg’ }

What result did you expect?

Error logged

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ljc-devcommented, Jun 24, 2020

Thanks for the detailed explanation!!! I’ll try this on my next project. 👍

1reaction
tunnckoCorecommented, Jun 7, 2020

There’s no point in using try catch in event driven API. Errors and everything is through events, in case of error on('error') is emitted so.

I don’t see how calling just formidable() will error, it’s just not possible cuz there’s nothing done in the constructor that can produce error. Error can happen some time after the parse is called, in which case it should emit error event and not throw. If you are saying that it’s throwing, than yes it’s a problem and should be handled internally, I agree on that. All this will change with v2/v3 with simplifying the API and codebase a LOT.

Good workaround is to wrap everything in a Promise constructor and then work with promises entirely.

const promise = new Promise((resolve, reject) => {
  const form = formidable();
  form.on('error', reject);
  form.parse(req, (err, files, fields) => 
	err ? reject(err) : resolve({ files, fields})
  )
});

try {
  await promise
} catch (err) {}

which is overkill compared to just using

Yep, definitely.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling, "try...catch" - The Modern JavaScript Tutorial
The try...catch construct has two main blocks: try , and then catch : ... That's because the engine can't understand the code.
Read more >
Try-Catch not handling errors with an https.get request in Node
The reason why try...catch.. fails is that it is meant for handling synchronous errors. https.get() is asynchronous and cannot be handled ...
Read more >
ssh2-sftp-client - npm
Path to remote file to be deleted. noErrorOK: boolean. If true, no error is raised when you try to delete a non-existent file....
Read more >
JavaScript's try-catch hid my bugs! - freeCodeCamp
So I put a few try-catch handlers inside the various methods being called, and added logging statements inside the catch block.
Read more >
How to Handle the FileNotFoundException in C# | Rollbar
The location or path the developer has passed might be wrong. ... Then within the main method, a try-catch block is placed to...
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