Skip to content

Commit 67124ac

Browse files
jasnelldanielleadams
authored andcommitted
readline: propagate signal.reason in awaitable question
Signed-off-by: James M Snell <[email protected]> PR-URL: #41008 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent e795547 commit 67124ac

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

lib/readline.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,16 @@ Interface.prototype.question[promisify.custom] = function(query, options) {
157157
options = typeof options === 'object' && options !== null ? options : {};
158158

159159
if (options.signal && options.signal.aborted) {
160-
return PromiseReject(new AbortError());
160+
return PromiseReject(
161+
new AbortError(undefined, { cause: options.signal.reason }));
161162
}
162163

163164
return new Promise((resolve, reject) => {
164165
let cb = resolve;
165166

166167
if (options.signal) {
167168
const onAbort = () => {
168-
reject(new AbortError());
169+
reject(new AbortError(undefined, { cause: options.signal.reason }));
169170
};
170171
options.signal.addEventListener('abort', onAbort, { once: true });
171172
cb = (answer) => {

lib/readline/promises.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ class Interface extends _Interface {
3030
if (options?.signal) {
3131
validateAbortSignal(options.signal, 'options.signal');
3232
if (options.signal.aborted) {
33-
return reject(new AbortError());
33+
return reject(
34+
new AbortError(undefined, { cause: options.signal.reason }));
3435
}
3536

3637
const onAbort = () => {
3738
this[kQuestionCancel]();
38-
reject(new AbortError());
39+
reject(new AbortError(undefined, { cause: options.signal.reason }));
3940
};
4041
options.signal.addEventListener('abort', onAbort, { once: true });
4142
cb = (answer) => {

test/parallel/test-readline-promises-interface.js

+9
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,15 @@ for (let i = 0; i < 12; i++) {
910910
rli.close();
911911
}
912912

913+
(async () => {
914+
const [rli] = getInterface({ terminal });
915+
const signal = AbortSignal.abort('boom');
916+
await assert.rejects(rli.question('hello', { signal }), {
917+
cause: 'boom',
918+
});
919+
rli.close();
920+
})().then(common.mustCall());
921+
913922
// Throw an error when question is executed with an aborted signal
914923
{
915924
const ac = new AbortController();

0 commit comments

Comments
 (0)