Skip to content
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: add error handling for input stream #31603

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ function Interface(input, output, completer, terminal) {
this._ttyWrite = _ttyWriteDumb.bind(this);
}

function onerror(err) {
self.emit('error', err);
}

function ondata(data) {
self._normalWrite(data);
}
Expand Down Expand Up @@ -227,9 +231,12 @@ function Interface(input, output, completer, terminal) {

this[kLineObjectStream] = undefined;

input.on('error', onerror);

if (!this.terminal) {
function onSelfCloseWithoutTerminal() {
input.removeListener('data', ondata);
input.removeListener('error', onerror);
input.removeListener('end', onend);
}

Expand All @@ -240,6 +247,7 @@ function Interface(input, output, completer, terminal) {
} else {
function onSelfCloseWithTerminal() {
input.removeListener('keypress', onkeypress);
input.removeListener('error', onerror);
input.removeListener('end', ontermend);
if (output !== null && output !== undefined) {
output.removeListener('resize', onresize);
Expand Down Expand Up @@ -1098,12 +1106,17 @@ Interface.prototype[SymbolAsyncIterator] = function() {
});
const lineListener = (input) => {
if (!readable.push(input)) {
// TODO(rexagod): drain to resume flow
this.pause();
}
};
const closeListener = () => {
readable.push(null);
};
const errorListener = (err) => {
readable.destroy(err);
};
this.on('error', errorListener);
this.on('line', lineListener);
this.on('close', closeListener);
this[kLineObjectStream] = readable;
Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-readline-input-onerror.js
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);