Skip to content

Commit 0ae8a88

Browse files
authored
Merge pull request #55 from DeepCodeAI/fix_error_process
Improved error handling
2 parents 702b9a9 + f0eea3f commit 0ae8a88

9 files changed

Lines changed: 170 additions & 80 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: 21 additions & 7 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
@@ -132,7 +134,13 @@ abstract class BundlesModule extends LoginModule
132134

133135
// procesing filter list of files, acceptable for server
134136
public async createFilesFilterList(): Promise<void> {
135-
this.serverFilesFilterList = await http.getFilters(this.baseURL, this.token);
137+
try {
138+
this.serverFilesFilterList = await http.getFilters(this.baseURL, this.token);
139+
} catch (err) {
140+
this.processError(err, {
141+
message: errorsLogs.filtersFiles
142+
})
143+
}
136144
}
137145

138146
private terminateAnalysis(): void {
@@ -193,7 +201,11 @@ abstract class BundlesModule extends LoginModule
193201
this.onError(error);
194202
});
195203

196-
http.analyse(this.baseURL, this.token, path, this.files, removedFiles).catch((error) => this.onError(error));
204+
http.analyse(this.baseURL, this.token, path, this.files, removedFiles).catch(
205+
(error) => this.processError(error, {
206+
message: errorsLogs.analyse
207+
})
208+
);
197209
}
198210

199211
private async startCollectingFiles(
@@ -274,25 +286,27 @@ abstract class BundlesModule extends LoginModule
274286
try {
275287
const workspaceFolders: readonly vscode.WorkspaceFolder[] | undefined = vscode.workspace.workspaceFolders;
276288
if (!workspaceFolders || !workspaceFolders.length) {
277-
setContext(DEEPCODE_CONTEXT.ANALYZING, false);
289+
await setContext(DEEPCODE_CONTEXT.ANALYZING, false);
278290
return;
279291
}
280292

281293
this.createWorkspacesList(workspaceFolders);
282294

283295
if (this.workspacesPaths.length) {
284-
setContext(DEEPCODE_CONTEXT.ANALYZING, true);
296+
await setContext(DEEPCODE_CONTEXT.ANALYZING, true);
285297
this.updateCurrentWorkspacePath(this.workspacesPaths[0]);
286298

287299
await this.updateHashesBundles();
288300
for await (const path of this.workspacesPaths) {
289301
await this.performBundlesActions(path);
290302
}
291303
} else {
292-
setContext(DEEPCODE_CONTEXT.ANALYZING, false);
304+
await setContext(DEEPCODE_CONTEXT.ANALYZING, false);
293305
}
294306
} catch(err) {
295-
await this.processError(err);
307+
await this.processError(err, {
308+
message: errorsLogs.failedAnalysis,
309+
});
296310
}
297311
}
298312
}

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: 15 additions & 7 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
}
@@ -36,9 +38,15 @@ abstract class LoginModule extends ReportModule implements DeepCode.LoginModuleI
3638
async checkSession(): Promise<boolean> {
3739
let validSession = false;
3840
if (this.token) {
39-
validSession = !!(await http.checkSession(this.baseURL, this.token));
41+
try {
42+
validSession = !!(await http.checkSession(this.baseURL, this.token));
43+
} catch (err) {
44+
this.processError(err, {
45+
message: errorsLogs.loginStatus
46+
});
47+
}
4048
}
41-
setContext(DEEPCODE_CONTEXT.LOGGEDIN, validSession);
49+
await setContext(DEEPCODE_CONTEXT.LOGGEDIN, validSession);
4250
return validSession;
4351
}
4452

@@ -57,13 +65,13 @@ abstract class LoginModule extends ReportModule implements DeepCode.LoginModuleI
5765

5866
async checkApproval(): Promise<boolean> {
5967
const approved = this.uploadApproved;
60-
setContext(DEEPCODE_CONTEXT.APPROVED, approved);
68+
await setContext(DEEPCODE_CONTEXT.APPROVED, approved);
6169
return approved;
6270
}
6371

6472
async approveUpload(): Promise<void> {
65-
this.setUploadApproved(true);
66-
this.checkApproval();
73+
await this.setUploadApproved(true);
74+
await this.checkApproval();
6775
}
6876

6977
}

src/deepcode/lib/modules/ReportModule.ts

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,37 @@ abstract class ReportModule extends BaseDeepCodeModule implements DeepCode.Repor
2424

2525
async sendError(options: {[key: string]: any}): Promise<void> {
2626
if (!this.shouldReport || !this.shouldReportErrors) return;
27-
await http.sendError(this.baseURL, {
28-
source: this.source,
29-
...(this.token && { sessionToken: this.token }),
30-
...options
31-
});
27+
try {
28+
await http.sendError(this.baseURL, {
29+
source: this.source,
30+
...(this.token && { sessionToken: this.token }),
31+
...options
32+
});
33+
} catch(error) {
34+
console.error(error);
35+
}
3236
}
3337

3438
async sendEvent(event: string, options: {[key: string]: any}): Promise<void> {
3539
if (!this.shouldReport || !this.shouldReportEvents) return;
36-
await http.sendEvent(this.baseURL, {
37-
type: event,
38-
source: this.source,
39-
...(this.token && { sessionToken: this.token }),
40-
...options
41-
});
40+
try {
41+
await http.sendEvent(this.baseURL, {
42+
type: event,
43+
source: this.source,
44+
...(this.token && { sessionToken: this.token }),
45+
...options
46+
});
47+
} catch(error) {
48+
this.processError(error, {
49+
message: errorsLogs.sendEvent,
50+
data: {
51+
event,
52+
source: this.source,
53+
...(this.token && { sessionToken: this.token }),
54+
...options
55+
},
56+
});
57+
}
4258
}
4359

4460
async processError(
@@ -79,27 +95,29 @@ abstract class ReportModule extends BaseDeepCodeModule implements DeepCode.Repor
7995
case badGateway:
8096
case serviceUnavailable:
8197
case timeout:
82-
return this.connectionErrorHandler();
98+
await this.connectionErrorHandler();
8399
case unauthorizedUser:
84100
case notFound:
85101
default:
86102
await this.sendErrorToServer(error, options);
87-
return this.generalErrorHandler();
103+
await this.generalErrorHandler();
88104
}
89105
}
90106

91-
private generalErrorHandler(): void {
107+
private async generalErrorHandler(): Promise<void> {
92108
this.transientErrors = 0;
93-
setContext(DEEPCODE_CONTEXT.ERROR, DEEPCODE_ERROR_CODES.BLOCKING);
109+
await setContext(DEEPCODE_CONTEXT.ERROR, DEEPCODE_ERROR_CODES.BLOCKING);
94110
}
95111

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

99115
++this.transientErrors;
100-
setContext(DEEPCODE_CONTEXT.ERROR, DEEPCODE_ERROR_CODES.TRANSIENT);
101-
setTimeout(async () => {
102-
this.startExtension();
116+
await setContext(DEEPCODE_CONTEXT.ERROR, DEEPCODE_ERROR_CODES.TRANSIENT);
117+
setTimeout(() => {
118+
this.startExtension().catch((err) => this.processError(err, {
119+
message: errorsLogs.failedExecutionTransient,
120+
}));
103121
}, 5000);
104122
}
105123

@@ -131,6 +149,7 @@ abstract class ReportModule extends BaseDeepCodeModule implements DeepCode.Repor
131149
}
132150
});
133151
} catch (e) {
152+
console.error(errorsLogs.errorReportFail);
134153
console.error(e);
135154
}
136155
}

0 commit comments

Comments
 (0)