Skip to content

Commit

Permalink
Updates CLI error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Mar 8, 2025
1 parent b1a9590 commit 3760823
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
41 changes: 21 additions & 20 deletions src/env/node/gk/cli/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/require-await */
import type { Disposable } from 'vscode';
import type { CompareWithCommandArgs } from '../../../../commands/compareWith';
import type { Container } from '../../../../container';
Expand Down Expand Up @@ -67,7 +68,7 @@ export class CliCommandHandlers implements Disposable {
_request: CliCommandRequest,
repo?: Repository | undefined,
): Promise<CliCommandResponse> {
return cherryPick(repo);
void cherryPick(repo);
}

@command('compare')
Expand All @@ -76,51 +77,48 @@ export class CliCommandHandlers implements Disposable {
repo?: Repository | undefined,
): Promise<CliCommandResponse> {
if (!repo || !_request.args?.length) {
await executeCommand('gitlens.compareWith');
void executeCommand('gitlens.compareWith');
return;
}

const [ref1, ref2] = _request.args;
if (!ref1 || !ref2) {
await executeCommand('gitlens.compareWith');
void executeCommand('gitlens.compareWith');
return;
}

if (ref1) {
if (!(await repo.git.refs().validateReference(ref1))) {
// TODO: Send an error back to the CLI?
await executeCommand('gitlens.compareWith');
return;
void executeCommand('gitlens.compareWith');
return { stderr: `${ref1} is an invalid reference` };
}
}

if (ref2) {
if (!(await repo.git.refs().validateReference(ref2))) {
// TODO: Send an error back to the CLI?
await executeCommand<CompareWithCommandArgs>('gitlens.compareWith', { ref1: ref1 });
return;
void executeCommand<CompareWithCommandArgs>('gitlens.compareWith', { ref1: ref1 });
return { stderr: `${ref2} is an invalid reference` };
}
}

await executeCommand<CompareWithCommandArgs>('gitlens.compareWith', { ref1: ref1, ref2: ref2 });
void executeCommand<CompareWithCommandArgs>('gitlens.compareWith', { ref1: ref1, ref2: ref2 });
}

@command('graph')
async handleGraphCommand(request: CliCommandRequest, repo?: Repository | undefined): Promise<CliCommandResponse> {
if (!repo || !request.args?.length) {
await executeCommand('gitlens.showGraphView');
void executeCommand('gitlens.showGraphView');
return;
}

const [ref] = request.args;
const reference = await repo.git.refs().getReference(ref);
if (ref && !reference) {
// TODO: Send an error back to the CLI?
await executeCommand('gitlens.showInCommitGraph', repo);
return;
void executeCommand('gitlens.showInCommitGraph', repo);
return { stderr: `${ref} is an invalid reference` };
}

await executeCommand('gitlens.showInCommitGraph', { ref: reference });
void executeCommand('gitlens.showInCommitGraph', { ref: reference });
}

@command('merge')
Expand All @@ -129,10 +127,12 @@ export class CliCommandHandlers implements Disposable {

const [ref] = request.args;
const reference = await repo.git.refs().getReference(ref);

void merge(repo, reference);

if (ref && !reference) {
// TODO: Send an error back to the CLI?
return { stderr: `${ref} is an invalid reference` };
}
return merge(repo, reference);
}

@command('rebase')
Expand All @@ -141,10 +141,11 @@ export class CliCommandHandlers implements Disposable {

const [ref] = request.args;
const reference = await repo.git.refs().getReference(ref);

void rebase(repo, reference);

if (ref && !reference) {
// TODO: Send an error back to the CLI?
return { stderr: `${ref} is an invalid reference` };
}

return rebase(repo, reference);
}
}
6 changes: 3 additions & 3 deletions src/env/node/gk/cli/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { Disposable } from 'vscode';
import type { Container } from '../../../../container';
import { configuration } from '../../../../system/-webview/configuration';
import { CliCommandHandlers } from './commands';
import type { IpcServer } from './server';
import { createIpcServer } from './server';
import type { IpcServer } from './ipcServer';
import { createIpcServer } from './ipcServer';

export interface CliCommandRequest {
cwd?: string;
args?: string[];
}
export type CliCommandResponse = string | void;
export type CliCommandResponse = { stdout?: string; stderr?: string } | void;
export type CliIpcServer = IpcServer<CliCommandRequest, CliCommandResponse>;

export class GkCliIntegrationProvider implements Disposable {
Expand Down
13 changes: 10 additions & 3 deletions src/env/node/gk/cli/server.ts → src/env/node/gk/cli/ipcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,18 @@ export class IpcServer<Request = unknown, Response = void> implements Disposable
const data = JSON.parse(Buffer.concat(chunks).toString('utf8'));
try {
const result = await handler(data);
res.writeHead(200);
if (result != null && typeof result === 'string') {
if (result == null) {
res.writeHead(200);
res.end();
return;
}

if (typeof result === 'string') {
res.writeHead(200);
res.end(result);
} else {
res.end();
res.writeHead(200);
res.end(JSON.stringify(result));
}
} catch (ex) {
Logger.error(ex, 'IPC handler error', data);
Expand Down

0 comments on commit 3760823

Please sign in to comment.