Skip to content

Commit

Permalink
Handle case of empty command id with arguments
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Mäder <[email protected]>
  • Loading branch information
tsmaeder committed Mar 26, 2021
1 parent d9c0fab commit b2b2a01
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/plugin-ext/src/common/rpc-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@ function safeStringify(obj: any, replacer?: JSONStringifyReplacer): string {
try {
return JSON.stringify(obj, replacer);
} catch (err) {
console.warn('error stringifying response: ' + err);
if (err && err.stack) {
console.error(err.stack);
}
return 'null';
}
}
19 changes: 10 additions & 9 deletions packages/plugin-ext/src/plugin/command-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class CommandRegistryImpl implements CommandRegistryExt {
if (handler) {
return handler<T>(...args.map(arg => this.argumentProcessors.reduce((r, p) => p.processArgument(r), arg)));
} else {
throw new Error(`Command ${id} doesn't exist`);
throw new Error(`No handler exists for command '${id}'`);
}
}

Expand Down Expand Up @@ -171,6 +171,7 @@ export class CommandsConverter {
if (!command) {
return undefined;
}

const result = this.toInternalCommand(command);
if (KnownCommands.mapped(result.id)) {
return result;
Expand All @@ -181,7 +182,7 @@ export class CommandsConverter {
this.isSafeCommandRegistered = true;
}

if (command.command && command.arguments && command.arguments.length > 0) {
if (command.arguments && command.arguments.length > 0) {
const id = this.handle++;
this.commandsMap.set(id, command);
disposables.push(new Disposable(() => this.commandsMap.delete(id)));
Expand All @@ -197,19 +198,19 @@ export class CommandsConverter {
// Existing code will have compiled against a non - optional version of the field, so asserting it to exist is ok
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return KnownCommands.map((external.command || external.id)!, external.arguments, (mappedId: string, mappedArgs: any[]) =>
({
id: mappedId,
title: external.title || external.label || ' ',
tooltip: external.tooltip,
arguments: mappedArgs
}));
({
id: mappedId,
title: external.title || external.label || ' ',
tooltip: external.tooltip,
arguments: mappedArgs
}));
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private executeSafeCommand<R>(...args: any[]): PromiseLike<R | undefined> {
const command = this.commandsMap.get(args[0]);
if (!command || !command.command) {
return Promise.reject('command NOT FOUND');
return Promise.reject(`command ${args[0]} not found`);
}
return this.commands.executeCommand(command.command, ...(command.arguments || []));
}
Expand Down

0 comments on commit b2b2a01

Please sign in to comment.