-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: add error handling for input stream
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: #30831 PR-URL: #31603 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
- Loading branch information
1 parent
e40ed28
commit cb3020d
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const fs = require('fs'); | ||
const readline = require('readline'); | ||
const path = require('path'); | ||
|
||
async function processLineByLine_SymbolAsyncError(filename) { | ||
const fileStream = fs.createReadStream(filename); | ||
const rl = readline.createInterface({ | ||
input: fileStream, | ||
crlfDelay: Infinity | ||
}); | ||
// eslint-disable-next-line no-unused-vars | ||
for await (const line of rl) { | ||
/* check SymbolAsyncIterator `errorListener` */ | ||
} | ||
} | ||
|
||
const f = path.join(__dirname, 'file.txt'); | ||
|
||
// catch-able SymbolAsyncIterator `errorListener` error | ||
processLineByLine_SymbolAsyncError(f).catch(common.expectsError({ | ||
code: 'ENOENT', | ||
message: `ENOENT: no such file or directory, open '${f}'` | ||
})); | ||
|
||
async function processLineByLine_InterfaceErrorEvent(filename) { | ||
const fileStream = fs.createReadStream(filename); | ||
const rl = readline.createInterface({ | ||
input: fileStream, | ||
crlfDelay: Infinity | ||
}); | ||
rl.on('error', common.expectsError({ | ||
code: 'ENOENT', | ||
message: `ENOENT: no such file or directory, open '${f}'` | ||
})); | ||
} | ||
|
||
// check Interface 'error' event | ||
processLineByLine_InterfaceErrorEvent(f); |