Can't catch error from an erroneous uploadDir path with a trycatch block
See original GitHub issueSupport 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:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for the detailed explanation!!! I’ll try this on my next project. 👍
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 theparse
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.
Yep, definitely.