Skip to content

Commit a91eb01

Browse files
committed
Improves get draft error handling
1 parent d95acdd commit a91eb01

File tree

3 files changed

+100
-55
lines changed

3 files changed

+100
-55
lines changed

src/commands/patches.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { showPatchesView } from '../plus/drafts/actions';
99
import type { Change, CreateDraft } from '../plus/webviews/patchDetails/protocol';
1010
import { getRepositoryOrShowPicker } from '../quickpicks/repositoryPicker';
1111
import { command } from '../system/command';
12+
import { Logger } from '../system/logger';
1213
import type { CommandContext } from './base';
1314
import {
1415
ActiveEditorCommand,
@@ -178,13 +179,13 @@ export class OpenCloudPatchCommand extends Command {
178179
return;
179180
}
180181

181-
const draft = args?.draft ?? (await this.container.drafts.getDraft(args?.id));
182-
if (draft == null) {
183-
void window.showErrorMessage(`Cannot open Cloud Patch; patch '${args.id}' not found`);
184-
return;
182+
try {
183+
const draft = args?.draft ?? (await this.container.drafts.getDraft(args?.id));
184+
void showPatchesView({ mode: 'view', draft: draft });
185+
} catch (ex) {
186+
Logger.error(ex, 'OpenCloudPatchCommand');
187+
void window.showErrorMessage(`Unable to open Cloud Patch '${args.id}'`);
185188
}
186-
187-
void showPatchesView({ mode: 'view', draft: draft });
188189
}
189190
}
190191

src/plus/drafts/draftsService.ts

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,42 @@ export class DraftService implements Disposable {
307307

308308
@log()
309309
async getDraft(id: string): Promise<Draft> {
310+
const scope = getLogScope();
311+
310312
type Result = { data: DraftResponse };
311313

312314
const [rspResult, changesetsResult] = await Promise.allSettled([
313315
this.connection.fetchGkDevApi(`v1/drafts/${id}`, { method: 'GET' }),
314316
this.getChangesets(id),
315317
]);
316318

317-
const rsp = getSettledValue(rspResult);
319+
if (rspResult.status === 'rejected') {
320+
Logger.error(rspResult.reason, scope, `Unable to open draft '${id}': ${rspResult.reason}`);
321+
throw new Error(`Unable to open draft '${id}': ${rspResult.reason}`);
322+
}
323+
324+
if (changesetsResult.status === 'rejected') {
325+
Logger.error(
326+
changesetsResult.reason,
327+
scope,
328+
`Unable to open changeset for draft '${id}': ${changesetsResult.reason}`,
329+
);
330+
throw new Error(`Unable to open changesets for draft '${id}': ${changesetsResult.reason}`);
331+
}
318332

333+
const rsp = getSettledValue(rspResult)!;
319334
if (rsp?.ok === false) {
320-
Logger.error(undefined, `Getting draft failed: (${rsp.status}) ${rsp.statusText}`);
321-
throw new Error(rsp.statusText);
335+
const json = (await rsp.json()) as { error?: { message?: string } } | undefined;
336+
Logger.error(
337+
undefined,
338+
scope,
339+
`Unable to open draft '${id}': (${rsp.status}) ${json?.error?.message ?? rsp.statusText}`,
340+
);
341+
throw new Error(json?.error?.message ?? rsp.statusText);
322342
}
323343

324344
const draft = ((await rsp.json()) as Result).data;
325-
const changesets = getSettledValue(changesetsResult) ?? [];
345+
const changesets = getSettledValue(changesetsResult)!;
326346

327347
const author: Draft['author'] = {
328348
id: draft.createdBy,
@@ -394,62 +414,81 @@ export class DraftService implements Disposable {
394414

395415
@log()
396416
async getChangesets(id: string): Promise<DraftChangeset[]> {
397-
type Result = { data: DraftChangesetResponse[] };
398-
399-
const rsp = await this.connection.fetchGkDevApi(`/v1/drafts/${id}/changesets`, { method: 'GET' });
400-
401-
const changeset = ((await rsp.json()) as Result).data;
417+
const scope = getLogScope();
402418

403-
const changesets: DraftChangeset[] = [];
404-
for (const c of changeset) {
405-
const patches: DraftPatch[] = [];
419+
type Result = { data: DraftChangesetResponse[] };
406420

407-
// const repoPromises = Promise.allSettled(c.patches.map(p => this.getRepositoryForGkId(p.gitRepositoryId)));
421+
try {
422+
const rsp = await this.connection.fetchGkDevApi(`/v1/drafts/${id}/changesets`, { method: 'GET' });
423+
if (rsp?.ok === false) {
424+
const json = (await rsp.json()) as { error?: { message?: string } } | undefined;
425+
Logger.error(
426+
undefined,
427+
scope,
428+
`Unable to open changesets for draft '${id}': (${rsp.status}) ${
429+
json?.error?.message ?? rsp.statusText
430+
}`,
431+
);
432+
throw new Error(json?.error?.message ?? rsp.statusText);
433+
}
408434

409-
for (const p of c.patches) {
410-
// const repoData = await this.getRepositoryData(p.gitRepositoryId);
411-
// const repo = await this.container.git.findMatchingRepository({
412-
// firstSha: repoData.initialCommitSha,
413-
// remoteUrl: repoData.remote?.url,
414-
// });
435+
const changeset = ((await rsp.json()) as Result).data;
436+
437+
const changesets: DraftChangeset[] = [];
438+
for (const c of changeset) {
439+
const patches: DraftPatch[] = [];
440+
441+
// const repoPromises = Promise.allSettled(c.patches.map(p => this.getRepositoryForGkId(p.gitRepositoryId)));
442+
443+
for (const p of c.patches) {
444+
// const repoData = await this.getRepositoryData(p.gitRepositoryId);
445+
// const repo = await this.container.git.findMatchingRepository({
446+
// firstSha: repoData.initialCommitSha,
447+
// remoteUrl: repoData.remote?.url,
448+
// });
449+
450+
patches.push({
451+
type: 'cloud',
452+
id: p.id,
453+
createdAt: new Date(p.createdAt),
454+
updatedAt: new Date(p.updatedAt ?? p.createdAt),
455+
draftId: p.draftId,
456+
changesetId: p.changesetId,
457+
userId: c.userId,
458+
459+
baseBranchName: p.baseBranchName,
460+
baseRef: p.baseCommitSha,
461+
gkRepositoryId: p.gitRepositoryId,
462+
secureLink: p.secureDownloadData,
463+
464+
// // TODO@eamodio FIX THIS
465+
// repository: repo,
466+
// repoData: repoData,
467+
});
468+
}
415469

416-
patches.push({
417-
type: 'cloud',
418-
id: p.id,
419-
createdAt: new Date(p.createdAt),
420-
updatedAt: new Date(p.updatedAt ?? p.createdAt),
421-
draftId: p.draftId,
422-
changesetId: p.changesetId,
470+
changesets.push({
471+
id: c.id,
472+
createdAt: new Date(c.createdAt),
473+
updatedAt: new Date(c.updatedAt ?? c.createdAt),
474+
draftId: c.draftId,
475+
parentChangesetId: c.parentChangesetId,
423476
userId: c.userId,
424477

425-
baseBranchName: p.baseBranchName,
426-
baseRef: p.baseCommitSha,
427-
gkRepositoryId: p.gitRepositoryId,
428-
secureLink: p.secureDownloadData,
478+
gitUserName: c.gitUserName,
479+
gitUserEmail: c.gitUserEmail,
429480

430-
// // TODO@eamodio FIX THIS
431-
// repository: repo,
432-
// repoData: repoData,
481+
deepLinkUrl: c.deepLink,
482+
patches: patches,
433483
});
434484
}
435485

436-
changesets.push({
437-
id: c.id,
438-
createdAt: new Date(c.createdAt),
439-
updatedAt: new Date(c.updatedAt ?? c.createdAt),
440-
draftId: c.draftId,
441-
parentChangesetId: c.parentChangesetId,
442-
userId: c.userId,
443-
444-
gitUserName: c.gitUserName,
445-
gitUserEmail: c.gitUserEmail,
486+
return changesets;
487+
} catch (ex) {
488+
Logger.error(ex, scope);
446489

447-
deepLinkUrl: c.deepLink,
448-
patches: patches,
449-
});
490+
throw ex;
450491
}
451-
452-
return changesets;
453492
}
454493

455494
@log()

src/views/draftsView.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ export class DraftsView extends ViewBase<'drafts', DraftsViewNode, RepositoriesV
135135
async (node: DraftNode) => {
136136
let draft = node.draft;
137137
if (draft.changesets == null) {
138-
draft = await this.container.drafts.getDraft(node.draft.id);
138+
try {
139+
draft = await this.container.drafts.getDraft(node.draft.id);
140+
} catch (ex) {
141+
void window.showErrorMessage(`Unable to open Cloud Patch '${node.draft.id}'`);
142+
return;
143+
}
139144
}
140145
void showPatchesView({ mode: 'view', draft: draft });
141146
},

0 commit comments

Comments
 (0)