Skip to content

Commit 684b16c

Browse files
committed
Fixed unhandled promise rejections and added message to all progressError.
1 parent 702b9a9 commit 684b16c

8 files changed

Lines changed: 105 additions & 58 deletions

File tree

src/deepcode/DeepCodeExtension.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
DEEPCODE_OPEN_LOCAL,
1414
} from "./constants/commands";
1515
import { openDeepcodeSettingsCommand } from "./utils/vscodeCommandsUtils";
16+
import { errorsLogs } from "./messages/errorsServerLogMessages";
1617

1718
import {
1819
DEEPCODE_VIEW_SUPPORT,
@@ -22,24 +23,42 @@ import { SupportProvider } from "./view/SupportProvider";
2223
import { IssueProvider } from "./view/IssueProvider";
2324

2425
class DeepCodeExtension extends DeepCodeLib implements DeepCode.ExtensionInterface {
26+
private async executeCommand(
27+
name: string,
28+
fn: (...args: any[]) => Promise<any>,
29+
...args: any[]
30+
): Promise<any> {
31+
try {
32+
await fn(...args);
33+
} catch (error) {
34+
this.processError(error, {
35+
message: errorsLogs.command(name),
36+
});
37+
}
38+
}
39+
2540
public activate(context: vscode.ExtensionContext): void {
2641
this.statusBarItem.show();
2742

2843
context.subscriptions.push(
2944
vscode.commands.registerCommand(
3045
DEEPCODE_OPEN_BROWSER,
31-
(url: string) => open(url)
46+
this.executeCommand.bind(
47+
this,
48+
DEEPCODE_OPEN_BROWSER,
49+
(url: string) => open(url)
50+
)
3251
)
3352
);
3453

3554
context.subscriptions.push(
3655
vscode.commands.registerCommand(
3756
DEEPCODE_OPEN_LOCAL,
3857
(path: vscode.Uri, range?: vscode.Range) => {
39-
console.log("DEEPCODE_OPEN_LOCAL",path.toString());
4058
vscode.window.showTextDocument(path, { selection: range }).then(
41-
(f) => console.log(f),
42-
(err) => console.error(err)
59+
() => {}, (err) => this.processError(err, {
60+
message: errorsLogs.command(DEEPCODE_OPEN_LOCAL),
61+
})
4362
);
4463
}
4564
)
@@ -48,28 +67,44 @@ class DeepCodeExtension extends DeepCodeLib implements DeepCode.ExtensionInterfa
4867
context.subscriptions.push(
4968
vscode.commands.registerCommand(
5069
DEEPCODE_LOGIN,
51-
this.initiateLogin.bind(this)
70+
this.executeCommand.bind(
71+
this,
72+
DEEPCODE_LOGIN,
73+
this.initiateLogin.bind(this)
74+
)
5275
)
5376
);
5477

5578
context.subscriptions.push(
5679
vscode.commands.registerCommand(
5780
DEEPCODE_APPROVE,
58-
this.approveUpload.bind(this)
81+
this.executeCommand.bind(
82+
this,
83+
DEEPCODE_APPROVE,
84+
this.approveUpload.bind(this)
85+
)
5986
)
6087
);
6188

6289
context.subscriptions.push(
6390
vscode.commands.registerCommand(
6491
DEEPCODE_START_COMMAND,
65-
this.startExtension.bind(this)
92+
this.executeCommand.bind(
93+
this,
94+
DEEPCODE_START_COMMAND,
95+
this.startExtension.bind(this)
96+
)
6697
)
6798
);
6899

69100
context.subscriptions.push(
70101
vscode.commands.registerCommand(
71102
DEEPCODE_SETTINGS_COMMAND,
72-
openDeepcodeSettingsCommand
103+
this.executeCommand.bind(
104+
this,
105+
DEEPCODE_SETTINGS_COMMAND,
106+
openDeepcodeSettingsCommand
107+
)
73108
)
74109
);
75110

@@ -84,7 +119,9 @@ class DeepCodeExtension extends DeepCodeLib implements DeepCode.ExtensionInterfa
84119
);
85120

86121
this.activateAll();
87-
this.startExtension();
122+
this.startExtension().catch((err) => this.processError(err, {
123+
message: errorsLogs.failedExecution,
124+
}));
88125
}
89126

90127
}

src/deepcode/lib/modules/BaseDeepCodeModule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ export default abstract class BaseDeepCodeModule implements DeepCode.BaseDeepCod
7474
}
7575

7676
get uploadApproved(): boolean {
77-
return this.staticUploadApproved || this.source !== IDE_NAME || !!(vscode.workspace.getConfiguration('deepcode').get('uploadApproved'));
77+
return this.staticUploadApproved || this.source !== IDE_NAME || !!(vscode.workspace.getConfiguration('deepcode').get<boolean>('uploadApproved'));
7878
}
7979

8080
async setUploadApproved(value = true): Promise<void> {
8181
await vscode.workspace.getConfiguration('deepcode').update('uploadApproved', value, true);
8282
}
8383

8484
get shouldReportErrors(): boolean {
85-
return !!vscode.workspace.getConfiguration('deepcode').get('yesCrashReport');
85+
return !!vscode.workspace.getConfiguration('deepcode').get<boolean>('yesCrashReport');
8686
}
8787

8888
get shouldReportEvents(): boolean {
89-
return !!vscode.workspace.getConfiguration('deepcode').get('yesTelemetry');
89+
return !!vscode.workspace.getConfiguration('deepcode').get<boolean>('yesTelemetry');
9090
}
9191

9292
// Avoid refreshing context/views too often:

src/deepcode/lib/modules/BundlesModule.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ abstract class BundlesModule extends LoginModule
102102

103103
onError(error: Error) {
104104
this.terminateAnalysis();
105-
this.processError(error);
105+
this.processError(error, {
106+
message: errorsLogs.failedServiceAI,
107+
});
106108
}
107109

108110
// processing workspaces
@@ -274,25 +276,27 @@ abstract class BundlesModule extends LoginModule
274276
try {
275277
const workspaceFolders: readonly vscode.WorkspaceFolder[] | undefined = vscode.workspace.workspaceFolders;
276278
if (!workspaceFolders || !workspaceFolders.length) {
277-
setContext(DEEPCODE_CONTEXT.ANALYZING, false);
279+
await setContext(DEEPCODE_CONTEXT.ANALYZING, false);
278280
return;
279281
}
280282

281283
this.createWorkspacesList(workspaceFolders);
282284

283285
if (this.workspacesPaths.length) {
284-
setContext(DEEPCODE_CONTEXT.ANALYZING, true);
286+
await setContext(DEEPCODE_CONTEXT.ANALYZING, true);
285287
this.updateCurrentWorkspacePath(this.workspacesPaths[0]);
286288

287289
await this.updateHashesBundles();
288290
for await (const path of this.workspacesPaths) {
289291
await this.performBundlesActions(path);
290292
}
291293
} else {
292-
setContext(DEEPCODE_CONTEXT.ANALYZING, false);
294+
await setContext(DEEPCODE_CONTEXT.ANALYZING, false);
293295
}
294296
} catch(err) {
295-
await this.processError(err);
297+
await this.processError(err, {
298+
message: errorsLogs.failedAnalysis,
299+
});
296300
}
297301
}
298302
}

src/deepcode/lib/modules/DeepCodeLib.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import BundlesModule from "./BundlesModule";
44
import { setContext } from "../../utils/vscodeCommandsUtils";
55
import { DEEPCODE_CONTEXT } from "../../constants/views";
66
import { EXECUTION_DEBOUNCE_INTERVAL } from "../../constants/general";
7+
import { errorsLogs } from "../../messages/errorsServerLogMessages";
78

89
export default class DeepCodeLib extends BundlesModule implements DeepCode.DeepCodeLibInterface {
9-
private executing = false;
10-
1110
activateAll(): void {
1211
// this.filesWatcher.activate(this);
1312
this.workspacesWatcher.activate(this);
@@ -18,7 +17,7 @@ export default class DeepCodeLib extends BundlesModule implements DeepCode.DeepC
1817

1918
private async executeExtensionPipeline(): Promise<void> {
2019
console.log("DeepCode: starting execution pipeline");
21-
setContext(DEEPCODE_CONTEXT.ERROR, false);
20+
await setContext(DEEPCODE_CONTEXT.ERROR, false);
2221

2322
const loggedIn = await this.checkSession();
2423
if (!loggedIn) return;
@@ -36,7 +35,9 @@ export default class DeepCodeLib extends BundlesModule implements DeepCode.DeepC
3635
try {
3736
await this.executeExtensionPipeline();
3837
} catch (err) {
39-
this.processError(err);
38+
this.processError(err, {
39+
message: errorsLogs.failedExecutionDebounce,
40+
});
4041
}
4142
},
4243
EXECUTION_DEBOUNCE_INTERVAL,

src/deepcode/lib/modules/LoginModule.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class LoginModule extends ReportModule implements DeepCode.LoginModuleI
1717

1818
this.pendingLogin = true;
1919
try {
20-
setContext(DEEPCODE_CONTEXT.LOGGEDIN, false);
20+
await setContext(DEEPCODE_CONTEXT.LOGGEDIN, false);
2121
const result = await http.login(this.baseURL, this.source);
2222
const { sessionToken, loginURL } = result;
2323
if (!sessionToken || !loginURL) {
@@ -27,7 +27,9 @@ abstract class LoginModule extends ReportModule implements DeepCode.LoginModuleI
2727
await viewInBrowser(loginURL);
2828
await this.waitLoginConfirmation();
2929
} catch (err) {
30-
await this.processError(err, { message: errorsLogs.login });
30+
await this.processError(err, {
31+
message: errorsLogs.login
32+
});
3133
} finally {
3234
this.pendingLogin = false;
3335
}
@@ -38,7 +40,7 @@ abstract class LoginModule extends ReportModule implements DeepCode.LoginModuleI
3840
if (this.token) {
3941
validSession = !!(await http.checkSession(this.baseURL, this.token));
4042
}
41-
setContext(DEEPCODE_CONTEXT.LOGGEDIN, validSession);
43+
await setContext(DEEPCODE_CONTEXT.LOGGEDIN, validSession);
4244
return validSession;
4345
}
4446

@@ -57,13 +59,13 @@ abstract class LoginModule extends ReportModule implements DeepCode.LoginModuleI
5759

5860
async checkApproval(): Promise<boolean> {
5961
const approved = this.uploadApproved;
60-
setContext(DEEPCODE_CONTEXT.APPROVED, approved);
62+
await setContext(DEEPCODE_CONTEXT.APPROVED, approved);
6163
return approved;
6264
}
6365

6466
async approveUpload(): Promise<void> {
65-
this.setUploadApproved(true);
66-
this.checkApproval();
67+
await this.setUploadApproved(true);
68+
await this.checkApproval();
6769
}
6870

6971
}

src/deepcode/lib/modules/ReportModule.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,29 @@ abstract class ReportModule extends BaseDeepCodeModule implements DeepCode.Repor
7979
case badGateway:
8080
case serviceUnavailable:
8181
case timeout:
82-
return this.connectionErrorHandler();
82+
await this.connectionErrorHandler();
8383
case unauthorizedUser:
8484
case notFound:
8585
default:
8686
await this.sendErrorToServer(error, options);
87-
return this.generalErrorHandler();
87+
await this.generalErrorHandler();
8888
}
8989
}
9090

91-
private generalErrorHandler(): void {
91+
private async generalErrorHandler(): Promise<void> {
9292
this.transientErrors = 0;
93-
setContext(DEEPCODE_CONTEXT.ERROR, DEEPCODE_ERROR_CODES.BLOCKING);
93+
await setContext(DEEPCODE_CONTEXT.ERROR, DEEPCODE_ERROR_CODES.BLOCKING);
9494
}
9595

96-
private connectionErrorHandler(): void {
96+
private async connectionErrorHandler(): Promise<void> {
9797
if (this.transientErrors > MAX_CONNECTION_RETRIES) return this.generalErrorHandler();
9898

9999
++this.transientErrors;
100-
setContext(DEEPCODE_CONTEXT.ERROR, DEEPCODE_ERROR_CODES.TRANSIENT);
101-
setTimeout(async () => {
102-
this.startExtension();
100+
await setContext(DEEPCODE_CONTEXT.ERROR, DEEPCODE_ERROR_CODES.TRANSIENT);
101+
setTimeout(() => {
102+
this.startExtension().catch((err) => this.processError(err, {
103+
message: errorsLogs.failedExecutionTransient,
104+
}));
103105
}, 5000);
104106
}
105107

@@ -131,6 +133,7 @@ abstract class ReportModule extends BaseDeepCodeModule implements DeepCode.Repor
131133
}
132134
});
133135
} catch (e) {
136+
console.error(errorsLogs.errorReportFail);
134137
console.error(e);
135138
}
136139
}
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
export const errorsLogs = {
22
login: "Login of new user has failed",
3-
loginStatus: "Check login status of user has failed",
3+
// loginStatus: "Check login status of user has failed",
44
filtersFiles: "Failed fetching filters files for bundles from server",
5-
createBundle: "Failed to create bundle on server",
6-
uploadFiles: "Failed to upload missing files in bundle to server",
7-
checkBundle: "Failed to check bundle status on server",
8-
checkBundleAfterAttempts: (attempts: number) =>
9-
`Failed fetching bundle status on server after ${attempts} attempts`,
10-
extendBundle: "Failed to extend bundle on server with files",
11-
failedStatusOfAnalysis: "Analysis results have status FAILED",
12-
analyze: "Analysis request failed",
13-
failedAnalysis: "Failed fetching analysis results",
5+
// createBundle: "Failed to create bundle on server",
6+
// uploadFiles: "Failed to upload missing files in bundle to server",
7+
// checkBundle: "Failed to check bundle status on server",
8+
// checkBundleAfterAttempts: (attempts: number) =>
9+
// `Failed fetching bundle status on server after ${attempts} attempts`,
10+
// extendBundle: "Failed to extend bundle on server with files",
11+
// failedStatusOfAnalysis: "Analysis results have status FAILED",
12+
// analyze: "Analysis request failed",
13+
failedAnalysis: "Failed executing analysis",
14+
failedServiceAI: "ServiceAI returned an error",
15+
failedExecution: "Failed extension pipeline execution",
16+
failedExecutionDebounce: "Failed extension pipeline execution after debounce",
17+
failedExecutionTransient: "Failed extension pipeline execution after transient error",
1418
undefinedError: "Unrecognized error",
1519
watchFileBeforeExtendBundle:
1620
"Failed on watching file changes before extending bundle",
1721
updateReviewPositions:
1822
"Failed to update review results positions while editing file",
1923
errorReportFail: "Failed to send error report",
20-
modifiedFile: (type: string) => `Error occured while file was ${type}`,
21-
vscodeFileChanges: "Failed to process vscode file content changes"
24+
modifiedFile: (type: string) => `Failed to handle file change of type ${type}`,
25+
// vscodeFileChanges: "Failed to process vscode file content changes",
26+
command: (type: string) => `Failed to execute ${type} command`,
2227
};
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import * as vscode from "vscode";
22
import { DEEPCODE_EXTENSION_NAME } from "../constants/general";
33
import {
4-
DEEPCODE_START_COMMAND,
54
VSCODE_GO_TO_SETTINGS_COMMAND,
65
DEEPCODE_CONTEXT_PREFIX,
76
DEEPCODE_OPEN_BROWSER,
87
} from "../constants/commands";
98

10-
export const openDeepcodeSettingsCommand = (): void => {
11-
vscode.commands.executeCommand(VSCODE_GO_TO_SETTINGS_COMMAND, DEEPCODE_EXTENSION_NAME);
9+
export const openDeepcodeSettingsCommand = async (): Promise<void> => {
10+
await vscode.commands.executeCommand(VSCODE_GO_TO_SETTINGS_COMMAND, DEEPCODE_EXTENSION_NAME);
1211
};
1312

14-
export const startDeepCodeCommand = (): void => {
15-
vscode.commands.executeCommand(DEEPCODE_START_COMMAND);
16-
};
17-
18-
export const setContext = (key: string, value: unknown): void => {
13+
export const setContext = async (key: string, value: unknown): Promise<void> => {
1914
console.log("DeepCode context",key, value);
20-
vscode.commands.executeCommand('setContext', `${DEEPCODE_CONTEXT_PREFIX}${key}`, value);
15+
await vscode.commands.executeCommand('setContext', `${DEEPCODE_CONTEXT_PREFIX}${key}`, value);
2116
};
2217

23-
export const viewInBrowser = (url: string): void => {
24-
vscode.commands.executeCommand(DEEPCODE_OPEN_BROWSER, url);
18+
export const viewInBrowser = async (url: string): Promise<void> => {
19+
await vscode.commands.executeCommand(DEEPCODE_OPEN_BROWSER, url);
2520
};

0 commit comments

Comments
 (0)