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

Added error callback for when we fail in onEnd #1265

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/demo/demos/transformerException.mjs
@@ -0,0 +1,25 @@
import { prompt } from '@inquirer';

const demo = async () => {
try {
await prompt([
{
type: 'input',
message: 'Foo quotient',
name: 'foo',
transformer: (x) => {
if (x === 1) {
throw new Error('failing because of the reason');
}
return x;
},
default: 1,
},
]);

console.log('Done');
} catch (e) {
console.error('An _expected_ error', e);
}
};
export default demo;
2 changes: 2 additions & 0 deletions packages/demo/index.mjs
Expand Up @@ -10,6 +10,7 @@ import inputDemo from './demos/input.mjs';
import passwordDemo from './demos/password.mjs';
import rawlistDemo from './demos/rawlist.mjs';
import selectDemo from './demos/select.mjs';
import transformerExceptionDemo from './demos/transformerException.mjs';

const demos = {
checkbox: checkboxDemo,
Expand All @@ -20,6 +21,7 @@ const demos = {
password: passwordDemo,
rawlist: rawlistDemo,
select: selectDemo,
transformerExceptionDemo,
};

function askNextDemo() {
Expand Down
23 changes: 23 additions & 0 deletions packages/inquirer/examples/transformer-exception.js
@@ -0,0 +1,23 @@
import inquirer from '../lib/inquirer.js';
(async () => {
try {
await inquirer.prompt([
{
type: 'input',
message: 'Foo quotient',
name: 'foo',
transformer(x) {
if (x === 1) {
throw new Error('failing because of the reason');
}
return x;
},
default: 1,
},
]);

console.log('Done');
} catch (e) {
console.error('An _expected_ error', e);
}
})();
21 changes: 13 additions & 8 deletions packages/inquirer/lib/prompts/confirm.js
Expand Up @@ -36,8 +36,9 @@ export default class ConfirmPrompt extends Base {
* @return {this}
*/

_run(cb) {
_run(cb, errorCb) {
this.done = cb;
this.error = errorCb;

// Once user confirm (enter key)
const events = observe(this.rl);
Expand Down Expand Up @@ -79,14 +80,18 @@ export default class ConfirmPrompt extends Base {
onEnd(input) {
this.status = 'answered';

let output = this.opt.filter(input);
if (this.opt.transformer) {
output = this.opt.transformer(output);
try {
let output = this.opt.filter(input);
if (this.opt.transformer) {
output = this.opt.transformer(output);
}
this.render(output);

this.screen.done();
this.done(output);
} catch (error) {
this.error(error);
}
this.render(output);

this.screen.done();
this.done(output);
}

/**
Expand Down
15 changes: 10 additions & 5 deletions packages/inquirer/lib/prompts/input.js
Expand Up @@ -14,8 +14,9 @@ export default class InputPrompt extends Base {
* @return {this}
*/

_run(cb) {
_run(cb, errorCb) {
this.done = cb;
this.error = errorCb;

// Once user confirm (enter key)
const events = observe(this.rl);
Expand Down Expand Up @@ -82,11 +83,15 @@ export default class InputPrompt extends Base {
this.answer = state.value;
this.status = 'answered';

// Re-render prompt
this.render();
try {
// Re-render prompt
this.render();

this.screen.done();
this.done(state.value);
this.screen.done();
this.done(state.value);
} catch (error) {
this.error(error);
}
}

onError({ value = '', isValid }) {
Expand Down
62 changes: 62 additions & 0 deletions packages/inquirer/test/specs/inquirer.test.js
Expand Up @@ -778,6 +778,68 @@ describe('inquirer.prompt', () => {
expect(answers.prefiled.nested).toEqual('newValue');
});
});

it('should not mask the original error thrown in the onEnd function for transformer (input)', async () => {
const expectedError = 'Foo';
const prompts = [
{
type: 'input',
name: 'q1',
message: 'message',
transformer(x) {
if (x === 1) {
throw new Error(expectedError);
}
return x;
},
default: 1,
},
];

const promise = prompt(prompts);

promise.ui.rl.emit('line');

return promise
.then(() => {
// Failure
expect(true).toEqual(false);
})
.catch((error) => {
expect(error.message).toEqual(expectedError);
});
});

it('should not mask the original error thrown in the onEnd function for transformer (confirm)', async () => {
const expectedError = 'Foo';
const prompts = [
{
type: 'confirm',
name: 'q1',
message: 'message',
transformer(x) {
if (x === false) {
throw new Error(expectedError);
}
return x;
},
default: false,
},
];

const promise = prompt(prompts);

promise.ui.rl.emit('line');

return promise
.then(() => {
// Failure
expect(true).toEqual(false);
})
.catch((error) => {
expect(error.message).toEqual(expectedError);
});
});
});

describe('#registerPrompt()', () => {
Expand Down