Skip to content

Commit 3760823

Browse files
committed
Updates CLI error handling
1 parent b1a9590 commit 3760823

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

src/env/node/gk/cli/commands.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/require-await */
12
import type { Disposable } from 'vscode';
23
import type { CompareWithCommandArgs } from '../../../../commands/compareWith';
34
import type { Container } from '../../../../container';
@@ -67,7 +68,7 @@ export class CliCommandHandlers implements Disposable {
6768
_request: CliCommandRequest,
6869
repo?: Repository | undefined,
6970
): Promise<CliCommandResponse> {
70-
return cherryPick(repo);
71+
void cherryPick(repo);
7172
}
7273

7374
@command('compare')
@@ -76,51 +77,48 @@ export class CliCommandHandlers implements Disposable {
7677
repo?: Repository | undefined,
7778
): Promise<CliCommandResponse> {
7879
if (!repo || !_request.args?.length) {
79-
await executeCommand('gitlens.compareWith');
80+
void executeCommand('gitlens.compareWith');
8081
return;
8182
}
8283

8384
const [ref1, ref2] = _request.args;
8485
if (!ref1 || !ref2) {
85-
await executeCommand('gitlens.compareWith');
86+
void executeCommand('gitlens.compareWith');
8687
return;
8788
}
8889

8990
if (ref1) {
9091
if (!(await repo.git.refs().validateReference(ref1))) {
91-
// TODO: Send an error back to the CLI?
92-
await executeCommand('gitlens.compareWith');
93-
return;
92+
void executeCommand('gitlens.compareWith');
93+
return { stderr: `${ref1} is an invalid reference` };
9494
}
9595
}
9696

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

105-
await executeCommand<CompareWithCommandArgs>('gitlens.compareWith', { ref1: ref1, ref2: ref2 });
104+
void executeCommand<CompareWithCommandArgs>('gitlens.compareWith', { ref1: ref1, ref2: ref2 });
106105
}
107106

108107
@command('graph')
109108
async handleGraphCommand(request: CliCommandRequest, repo?: Repository | undefined): Promise<CliCommandResponse> {
110109
if (!repo || !request.args?.length) {
111-
await executeCommand('gitlens.showGraphView');
110+
void executeCommand('gitlens.showGraphView');
112111
return;
113112
}
114113

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

123-
await executeCommand('gitlens.showInCommitGraph', { ref: reference });
121+
void executeCommand('gitlens.showInCommitGraph', { ref: reference });
124122
}
125123

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

130128
const [ref] = request.args;
131129
const reference = await repo.git.refs().getReference(ref);
130+
131+
void merge(repo, reference);
132+
132133
if (ref && !reference) {
133-
// TODO: Send an error back to the CLI?
134+
return { stderr: `${ref} is an invalid reference` };
134135
}
135-
return merge(repo, reference);
136136
}
137137

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

142142
const [ref] = request.args;
143143
const reference = await repo.git.refs().getReference(ref);
144+
145+
void rebase(repo, reference);
146+
144147
if (ref && !reference) {
145-
// TODO: Send an error back to the CLI?
148+
return { stderr: `${ref} is an invalid reference` };
146149
}
147-
148-
return rebase(repo, reference);
149150
}
150151
}

src/env/node/gk/cli/integration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { Disposable } from 'vscode';
33
import type { Container } from '../../../../container';
44
import { configuration } from '../../../../system/-webview/configuration';
55
import { CliCommandHandlers } from './commands';
6-
import type { IpcServer } from './server';
7-
import { createIpcServer } from './server';
6+
import type { IpcServer } from './ipcServer';
7+
import { createIpcServer } from './ipcServer';
88

99
export interface CliCommandRequest {
1010
cwd?: string;
1111
args?: string[];
1212
}
13-
export type CliCommandResponse = string | void;
13+
export type CliCommandResponse = { stdout?: string; stderr?: string } | void;
1414
export type CliIpcServer = IpcServer<CliCommandRequest, CliCommandResponse>;
1515

1616
export class GkCliIntegrationProvider implements Disposable {

src/env/node/gk/cli/server.ts renamed to src/env/node/gk/cli/ipcServer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,18 @@ export class IpcServer<Request = unknown, Response = void> implements Disposable
8181
const data = JSON.parse(Buffer.concat(chunks).toString('utf8'));
8282
try {
8383
const result = await handler(data);
84-
res.writeHead(200);
85-
if (result != null && typeof result === 'string') {
84+
if (result == null) {
85+
res.writeHead(200);
86+
res.end();
87+
return;
88+
}
89+
90+
if (typeof result === 'string') {
91+
res.writeHead(200);
8692
res.end(result);
8793
} else {
88-
res.end();
94+
res.writeHead(200);
95+
res.end(JSON.stringify(result));
8996
}
9097
} catch (ex) {
9198
Logger.error(ex, 'IPC handler error', data);

0 commit comments

Comments
 (0)