Skip to content

Commit 7e854e5

Browse files
committed
better erorr handling #55
1 parent 2d7f2bb commit 7e854e5

3 files changed

Lines changed: 37 additions & 46 deletions

File tree

src/client/debugger/DebugClients/LocalDebugClient.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ export class LocalDebugClient extends DebugClient {
7878
});
7979
});
8080
}
81-
private displayError(error) {
81+
private displayError(error: any, context: string = "") {
8282
if (!error) { return; }
8383
let errorMsg = typeof error === "string" ? error : ((error.message && error.message.length > 0) ? error.message : "");
8484
if (errorMsg.length > 0) {
85-
this.debugSession.sendEvent(new OutputEvent(errorMsg + "\n", "stderr"));
85+
this.debugSession.sendEvent(new OutputEvent(context + (context.length > 0 ? ": " : "") + errorMsg + "\n", "stderr"));
8686
}
8787
}
8888
private getShebangLines(program: string): Promise<string[]> {
@@ -155,9 +155,6 @@ export class LocalDebugClient extends DebugClient {
155155

156156
this.getPTVSToolsFilePath().then((ptVSToolsFilePath) => {
157157
return this.prependShebangToPTVSFile(ptVSToolsFilePath, this.args.program);
158-
}, error => {
159-
this.displayError(error);
160-
reject(error);
161158
}).then((ptVSToolsFilePath) => {
162159
let launcherArgs = this.buildLauncherArguments();
163160

@@ -167,10 +164,10 @@ export class LocalDebugClient extends DebugClient {
167164
this.pyProc = proc;
168165
resolve();
169166
}, error => {
167+
// TODO: This condition makes no sense (refactor)
170168
if (!this.debugServer && this.debugServer.IsRunning) {
171169
return;
172170
}
173-
this.displayError(error);
174171
reject(error);
175172
});
176173

@@ -179,21 +176,22 @@ export class LocalDebugClient extends DebugClient {
179176

180177
this.pyProc = child_process.spawn(pythonPath, args, { cwd: processCwd, env: environmentVariables });
181178
this.pyProc.on("error", error => {
179+
// TODO: This condition makes no sense (refactor)
182180
if (!this.debugServer && this.debugServer.IsRunning) {
183181
return;
184182
}
185-
this.displayError(error);
183+
this.displayError(error, "pyProc.error");
186184
});
187185
this.pyProc.on("stderr", error => {
186+
// TODO: This condition makes no sense (refactor)
188187
if (!this.debugServer && this.debugServer.IsRunning) {
189188
return;
190189
}
191-
this.displayError(error);
190+
this.displayError(error, "pyProc.stderr");
192191
});
193192

194193
resolve();
195-
}, error => {
196-
this.displayError(error);
194+
}).catch(error => {
197195
reject(error);
198196
});
199197
});

src/client/debugger/Main.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ export class PythonDebugger extends DebugSession {
197197
let that = this;
198198

199199
this.canStartDebugger().then(() => {
200-
return this.startDebugServer().then(dbgServer => {
201-
return that.debugClient.LaunchApplicationToDebug(dbgServer);
202-
});
200+
return this.startDebugServer();
201+
}).then(dbgServer => {
202+
return that.debugClient.LaunchApplicationToDebug(dbgServer);
203203
}).catch(error => {
204204
this.sendEvent(new OutputEvent(error + "\n", "stderr"));
205205
this.sendErrorResponse(that.entryResponse, 2000, error);
@@ -313,7 +313,7 @@ export class PythonDebugger extends DebugSession {
313313
this.configurationDonePromiseResolve();
314314
this.configurationDonePromiseResolve = null;
315315
}
316-
});
316+
}).catch(error => this.sendErrorResponse(response, 2000, error));
317317
});
318318
}
319319

@@ -389,8 +389,9 @@ export class PythonDebugger extends DebugSession {
389389
this.pythonProcess.SendStepOut(this.pythonProcess.LastExecutedThread.Id);
390390
}
391391
protected continueRequest(response: DebugProtocol.ContinueResponse, args: DebugProtocol.ContinueArguments): void {
392-
this.sendResponse(response);
393-
this.pythonProcess.SendContinue();
392+
this.pythonProcess.SendContinue().then(() => {
393+
this.sendResponse(response);
394+
}).catch(error => this.sendErrorResponse(response, 2000, error));
394395
}
395396
protected nextRequest(response: DebugProtocol.NextResponse, args: DebugProtocol.NextArguments): void {
396397
this.sendResponse(response);
@@ -423,11 +424,7 @@ export class PythonDebugger extends DebugSession {
423424
variablesReference: variablesReference
424425
};
425426
this.sendResponse(response);
426-
},
427-
error => {
428-
this.sendErrorResponse(response, 2000, error);
429-
}
430-
);
427+
}).catch(error => this.sendErrorResponse(response, 2000, error));
431428
});
432429
}
433430
protected scopesRequest(response: DebugProtocol.ScopesResponse, args: DebugProtocol.ScopesArguments): void {
@@ -504,8 +501,6 @@ export class PythonDebugger extends DebugSession {
504501
variablesReference: variablesReference
505502
});
506503
});
507-
}, error => {
508-
this.sendErrorResponse(response, 2001, error);
509504
});
510505
});
511506

@@ -515,7 +510,7 @@ export class PythonDebugger extends DebugSession {
515510
};
516511

517512
return this.sendResponse(response);
518-
});
513+
}).catch(error => this.sendErrorResponse(response, 2001, error));
519514
}
520515
protected pauseRequest(response: DebugProtocol.PauseResponse): void {
521516
this.pythonProcess.Break();

src/client/debugger/PythonProcessCallbackHandler.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -221,31 +221,29 @@ export class PythonProcessCallbackHandler extends EventEmitter {
221221
});
222222
}
223223
private GetHandledExceptionRanges(fileName: string): Promise<{ startLine: number, endLine: number, expressions: string[] }[]> {
224-
return new Promise<{ startLine: number, endLine: number, expressions: string[] }[]>(resolve => {
225-
ExtractTryStatements(fileName).then(statements => {
226-
let exceptionRanges: { startLine: number, endLine: number, expressions: string[] }[] = [];
227-
statements.forEach(statement => {
228-
let expressions = [];
229-
if (statement.Exceptions.length === 0 || statement.Exceptions.indexOf("*") >= 0) {
230-
expressions = ["*"];
231-
}
232-
else {
233-
statement.Exceptions.forEach(ex => {
234-
if (expressions.indexOf(ex) === -1) {
235-
expressions.push(ex);
236-
}
237-
});
238-
}
239-
240-
exceptionRanges.push({
241-
endLine: statement.EndLineNumber,
242-
startLine: statement.StartLineNumber,
243-
expressions: expressions
224+
return ExtractTryStatements(fileName).then(statements => {
225+
let exceptionRanges: { startLine: number, endLine: number, expressions: string[] }[] = [];
226+
statements.forEach(statement => {
227+
let expressions = [];
228+
if (statement.Exceptions.length === 0 || statement.Exceptions.indexOf("*") >= 0) {
229+
expressions = ["*"];
230+
}
231+
else {
232+
statement.Exceptions.forEach(ex => {
233+
if (expressions.indexOf(ex) === -1) {
234+
expressions.push(ex);
235+
}
244236
});
245-
});
237+
}
246238

247-
resolve(exceptionRanges);
239+
exceptionRanges.push({
240+
endLine: statement.EndLineNumber,
241+
startLine: statement.StartLineNumber,
242+
expressions: expressions
243+
});
248244
});
245+
246+
return exceptionRanges;
249247
});
250248
}
251249

0 commit comments

Comments
 (0)