Skip to content

Commit 2f1bb3d

Browse files
authored
Validation failed creating an issue
Fixes #1868
1 parent cc0701b commit 2f1bb3d

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

src/common/utils.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Event, Disposable } from 'vscode';
88
import { sep } from 'path';
99
import moment = require('moment');
1010
import { HookError } from '@octokit/rest';
11+
import { isArray } from 'util';
1112

1213
export function uniqBy<T>(arr: T[], fn: (el: T) => string): T[] {
1314
const seen = Object.create(null);
@@ -94,6 +95,25 @@ export function groupBy<T>(arr: T[], fn: (el: T) => string): { [key: string]: T[
9495
}, Object.create(null));
9596
}
9697

98+
function isHookError(e: Error): e is HookError {
99+
return !!(e as HookError).errors;
100+
}
101+
102+
function hasFieldErrors(e: any): e is (Error & { errors: { value: string, field: string, code: string }[] }) {
103+
let areFieldErrors = true;
104+
if (!!e.errors && isArray(e.errors)) {
105+
for (const error of e.errors) {
106+
if (!error.field && !error.value && !error.code) {
107+
areFieldErrors = false;
108+
break;
109+
}
110+
}
111+
} else {
112+
areFieldErrors = false;
113+
}
114+
return areFieldErrors;
115+
}
116+
97117
export function formatError(e: HookError | any): string {
98118
if (!(e instanceof Error)) {
99119
if (typeof e === 'string') {
@@ -108,13 +128,20 @@ export function formatError(e: HookError | any): string {
108128
}
109129

110130
let errorMessage = e.message;
111-
const furtherInfo = (e as HookError).errors && (e as HookError).errors!.map((error: any) => {
112-
if (typeof error === 'string') {
113-
return error;
114-
} else {
115-
return error.message;
116-
}
117-
}).join(', ');
131+
let furtherInfo: string | undefined;
132+
if (e.message === 'Validation Failed' && hasFieldErrors(e)) {
133+
furtherInfo = e.errors.map(error => {
134+
return `Value "${error.value}" cannot be set for field ${error.field} (code: ${error.code})`;
135+
}).join(', ');
136+
} else if (isHookError(e) && e.errors) {
137+
furtherInfo = e.errors.map((error: any) => {
138+
if (typeof error === 'string') {
139+
return error;
140+
} else {
141+
return error.message;
142+
}
143+
}).join(', ');
144+
}
118145
if (furtherInfo) {
119146
errorMessage = `${errorMessage}: ${furtherInfo}`;
120147
}

src/issues/issueFeatureRegistrar.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,11 @@ export class IssueFeatureRegistrar implements vscode.Disposable {
291291
if (!title || !body) {
292292
return;
293293
}
294-
await this.doCreateIssue(this.createIssueInfo?.document, this.createIssueInfo?.newIssue, title, body, assignees, labels, this.createIssueInfo?.lineNumber, this.createIssueInfo?.insertIndex);
294+
const createSucceeded = await this.doCreateIssue(this.createIssueInfo?.document, this.createIssueInfo?.newIssue, title, body, assignees, labels, this.createIssueInfo?.lineNumber, this.createIssueInfo?.insertIndex);
295295
this.createIssueInfo = undefined;
296-
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
296+
if (createSucceeded) {
297+
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
298+
}
297299
}
298300

299301
async editQuery(query: vscode.TreeItem) {
@@ -505,14 +507,15 @@ ${body ?? ''}\n
505507
createParams.labels = filteredLabels;
506508
}
507509

508-
private async doCreateIssue(document: vscode.TextDocument | undefined, newIssue: NewIssue | undefined, title: string, issueBody: string | undefined, assignees: string[] | undefined, labels: string[] | undefined, lineNumber: number | undefined, insertIndex: number | undefined) {
510+
private async doCreateIssue(document: vscode.TextDocument | undefined, newIssue: NewIssue | undefined, title: string, issueBody: string | undefined, assignees: string[] | undefined,
511+
labels: string[] | undefined, lineNumber: number | undefined, insertIndex: number | undefined): Promise<boolean> {
509512
let origin: PullRequestDefaults | undefined;
510513
try {
511514
origin = await this.manager.getPullRequestDefaults();
512515
} catch (e) {
513516
// There is no remote
514517
vscode.window.showErrorMessage('There is no remote. Can\'t create an issue.');
515-
return;
518+
return false;
516519
}
517520
const body: string | undefined = issueBody || newIssue?.document.isUntitled ? issueBody : (await createGithubPermalink(this.gitAPI, newIssue)).permalink;
518521
const createParams: Octokit.IssuesCreateParams = {
@@ -542,7 +545,9 @@ ${body ?? ''}\n
542545
});
543546
}
544547
this._stateManager.refreshCacheNeeded();
548+
return true;
545549
}
550+
return false;
546551
}
547552

548553
private async getPermalinkWithError(): Promise<string | undefined> {

0 commit comments

Comments
 (0)