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

Handle case of empty command id with arguments #9223

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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