-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
readline.createInterface() is missing error handling for input stream #30831
Comments
To add a little more to this report, when using Here's a simple way to reproduce the problem - code taken from readline doc:
And, just pass a filename that doesn't exist. If this is your entire program, you will notice that the process just exits without logging either the |
This adds support for error handling in readline.createInterface() for cases where the input object is not supplied, the input stream is invalid, or the underlying buffer emits an error. Fixes: nodejs#30831
This adds support for error handling in readline.createInterface() for cases where the input object is not supplied, the input stream is invalid, or the underlying buffer emits an error. Now, the 'error' emissions by the readline module are thrown but in order to log those in the specific case of await for loops, we still need to fix silent rejections (TODO added there) inside async iterators for the thenables to work. Fixes: nodejs#30831
Great catch! I've added error listeners in my PR above, and will fix the silent rejections in a later one. Cheers! |
Here is a different variation on the same problem, which doesn't require the
|
It is true that the But the rest of the example is wrongly structured because you can't |
Checking
and
respectively, so point taken, but it wasn't obvious that fs.createReadStream or readline.createInterface couldn't have thrown a (synchronous) error upon trying to validate an invalid input. |
This adds support for error handling in readline.createInterface() for cases where the input object is not supplied, the input stream is invalid, or the underlying buffer emits an error. Now, the 'error' emissions by the readline module are thrown but in order to log those in the specific case of await for loops, we still need to fix silent rejections (TODO added there) inside async iterators for the thenables to work. Fixes: nodejs#30831
The iterator resulting from the
readline.createInterface()
should reject on the first iteration when the underlying input stream raises an error event. In fact, it is missing the error handling.We can observe this problem regarding the example of processLineByLine() when we try to invoke the
processLineByLine()
for an absent file.Even if we wrap the
for await (const line of rl) {…}
in atry/catch
block we are unable to handle the error and the program finishes with:A couple of workarounds has been discussed in the Stackoverflow question How to handle error from fs readline.Interface async iterator and jfriend00 proposes some alternatives to detour the problem: https://stackoverflow.com/a/59217504/1140754
The text was updated successfully, but these errors were encountered: