fix: don't crash if completion function fails

This commit is contained in:
Zoltan Kochan
2020-01-27 23:18:58 +02:00
parent 1c8aef0eb4
commit b4e0714899
2 changed files with 31 additions and 1 deletions

View File

@@ -52,7 +52,11 @@ export default async function complete (
if (!input.cmd || input.currentTypedWordType === 'value' && !ctx.completionByCommandName[input.cmd]) {
completions = ctx.initialCompletion()
} else if (ctx.completionByCommandName[input.cmd]) {
completions = await ctx.completionByCommandName[input.cmd](input.args, input.options)
try {
completions = await ctx.completionByCommandName[input.cmd](input.args, input.options)
} catch (err) {
// Ignore
}
}
}
if (input.currentTypedWordType === 'value') {

View File

@@ -91,6 +91,32 @@ test('complete a command', async (t) => {
t.end()
})
test('if command completion fails, return empty array', async (t) => {
t.deepEqual(
await complete(
{
cliOptionsTypesByCommandName: {},
completionByCommandName: {
run: async () => { throw new Error('error') },
},
globalOptionTypes: {
filter: String,
},
initialCompletion: () => [],
},
{
args: [],
cmd: 'run',
currentTypedWordType: 'value',
lastOption: null,
options: {},
},
),
[],
)
t.end()
})
test('initial completion', async (t) => {
const ctx = {
cliOptionsTypesByCommandName: {},