Skip to content

Commit 2a7f255

Browse files
Adds error handling for file not found in workspace (AST-68623) (#1105)
* Adds error handling for file not found in workspace * move string to constant file. * Fix comment * Refactors error handling in WebViewCommand * Get GPT configurations func --------- Co-authored-by: galactica <[email protected]>
1 parent 933c0bd commit 2a7f255

File tree

4 files changed

+55
-31
lines changed

4 files changed

+55
-31
lines changed

src/commands/webViewCommand.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ export class WebViewCommand {
334334
await this.startSastGpt(
335335
"Start chat",
336336
username,
337-
detailsDetachedView.getAskKicsUserIcon(),
338337
detailsDetachedView.getAskKicsIcon(),
339338
gptResult
340339
);
@@ -429,6 +428,9 @@ export class WebViewCommand {
429428
result.vulnerabilityName
430429
)
431430
.then((messages) => {
431+
432+
this.handleSystemNotFindPathError(messages[0].responses[0], "Please ensure that you have opened the correct workspace or the relevant file.");
433+
432434
this.conversationId = messages[0].conversationId;
433435
// enable all the buttons and inputs
434436
this.detailsPanel?.webview.postMessage({
@@ -492,6 +494,8 @@ export class WebViewCommand {
492494
this.conversationId
493495
)
494496
.then((messages) => {
497+
this.handleSystemNotFindPathError(messages[0].responses[0], constants.systemNotFindPathError);
498+
495499
this.conversationId = messages[0].conversationId;
496500
// enable all the buttons and inputs
497501
this.detailsPanel?.webview.postMessage({
@@ -526,7 +530,6 @@ export class WebViewCommand {
526530
async startSastGpt(
527531
userMessage: string,
528532
user: string,
529-
userKicsIcon,
530533
kicsIcon,
531534
result: GptResult
532535
) {
@@ -554,6 +557,8 @@ export class WebViewCommand {
554557

555558
cx.runSastGpt(userMessage, result.filename, result.resultID, "")
556559
.then((messages) => {
560+
this.handleSystemNotFindPathError(messages[0].responses[0], constants.systemNotFindPathError);
561+
557562
this.conversationId = messages[0].conversationId;
558563
// enable all the buttons and inputs
559564
this.detailsPanel?.webview.postMessage({
@@ -580,11 +585,17 @@ export class WebViewCommand {
580585
user: `${constants.aiSecurityChampion}`,
581586
},
582587
thinkID: this.thinkID,
583-
icon: kicsIcon,
588+
icon: "https://" + kicsIcon.authority + kicsIcon.path,
584589
});
585590
});
586591
}
587592

593+
handleSystemNotFindPathError(response: string, errorMessage: string): void {
594+
if (response.includes(errorMessage)) {
595+
throw new Error(constants.gptFileNotInWorkspaceError);
596+
}
597+
}
598+
588599
sleep(ms) {
589600
return new Promise((resolve) => setTimeout(resolve, ms));
590601
}

src/cx/cx.ts

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,10 @@ export class Cx implements CxPlatform {
4747
) {
4848
const resultsFilePath = getResultsFilePath();
4949
const cx = new CxWrapper(await this.getAstConfiguration());
50-
const gptToken = vscode.workspace
51-
.getConfiguration(constants.gptCommandName)
52-
.get(constants.gptSettingsKey) as string;
53-
const gptEngine = vscode.workspace
54-
.getConfiguration(constants.gptCommandName)
55-
.get(constants.gptEngineKey) as string;
56-
const filePackageObjectList = vscode.workspace.workspaceFolders;
57-
if (filePackageObjectList.length > 0) {
50+
const { gptToken, gptEngine } = this.getGptConfig();
51+
52+
this.validateWorkspaceFolders();
53+
5854
const answer = await cx.sastChat(
5955
gptToken,
6056
filePath,
@@ -69,7 +65,7 @@ export class Cx implements CxPlatform {
6965
} else {
7066
throw new Error(answer.status);
7167
}
72-
}
68+
7369
}
7470

7571
async runGpt(
@@ -80,30 +76,45 @@ export class Cx implements CxPlatform {
8076
queryName: string
8177
) {
8278
const cx = new CxWrapper(await this.getAstConfiguration());
79+
const { gptToken, gptEngine } = this.getGptConfig();
80+
81+
this.validateWorkspaceFolders();
82+
83+
const answer = await cx.kicsChat(
84+
gptToken,
85+
filePath,
86+
line,
87+
severity,
88+
queryName,
89+
message,
90+
null,
91+
gptEngine
92+
);
93+
if (answer.payload && answer.exitCode === 0) {
94+
return answer.payload;
95+
} else {
96+
throw new Error(answer.status);
97+
}
98+
}
99+
100+
private validateWorkspaceFolders() {
101+
const filePackageObjectList = vscode.workspace.workspaceFolders;
102+
103+
if (!filePackageObjectList || filePackageObjectList.length <= 0) {
104+
throw new Error(constants.gptFileNotInWorkspaceError);
105+
}
106+
}
107+
108+
getGptConfig(): { gptToken: string; gptEngine: string } {
83109
const gptToken = vscode.workspace
84110
.getConfiguration(constants.gptCommandName)
85111
.get(constants.gptSettingsKey) as string;
112+
86113
const gptEngine = vscode.workspace
87114
.getConfiguration(constants.gptCommandName)
88115
.get(constants.gptEngineKey) as string;
89-
const filePackageObjectList = vscode.workspace.workspaceFolders;
90-
if (filePackageObjectList.length > 0) {
91-
const answer = await cx.kicsChat(
92-
gptToken,
93-
filePath,
94-
line,
95-
severity,
96-
queryName,
97-
message,
98-
null,
99-
gptEngine
100-
);
101-
if (answer.payload && answer.exitCode === 0) {
102-
return answer.payload;
103-
} else {
104-
throw new Error(answer.status);
105-
}
106-
}
116+
117+
return { gptToken, gptEngine };
107118
}
108119

109120
async mask(filePath: string) {

src/utils/common/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ export const constants = {
141141
gptCommandName: "CheckmarxSecurityChampion",
142142
gptSettingsKey: "key",
143143
gptEngineKey: "model",
144+
systemNotFindPathError: "The system cannot find the path specified.",
145+
gptFileNotInWorkspaceError: "AI Security Champion can't advise you about this vulnerability because the file where the vulnerability was identified isn't open in your VS Code workspace.",
144146
// Documentation & Feedback
145147
feedback: "Send us enhancement request or report a bug",
146148
documentation: "Documentation",

src/views/gptView/gptView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class GptView implements vscode.WebviewViewProvider {
144144
<div id="main_div">
145145
<div class="container-fluid">
146146
<div class="container" style="padding:0;width:100 !important;">
147-
<div class="card" style="border:none;margin-bottom:1em;background:transparent;">
147+
<div class="card" style="border:none;margin-bottom:1em;background:transparent;color:var(--vscode-editor-foreground);">
148148
<div class="card-body" style="padding:0">
149149
<div class="row">
150150
<div class="col">

0 commit comments

Comments
 (0)