@@ -162,22 +162,29 @@ function killProcess() {
162162 catch ( ex ) { }
163163}
164164
165+ function handleError ( source : string , errorMessage : string ) {
166+ console . error ( `Error (${ source } ) ${ errorMessage } ` ) ;
167+ vscode . window . showErrorMessage ( `There was an error in the python extension. Error ${ errorMessage } ` ) ;
168+ }
169+
165170function spawnProcess ( dir : string ) {
166171 try {
167172 proc = child_process . spawn ( pythonSettings . pythonPath , [ "-u" , "completion.py" ] , {
168173 cwd : dir
169174 } ) ;
170175 }
171176 catch ( ex ) {
172- var x = "" ;
177+ return handleError ( "spawnProcess" , ex . message ) ;
173178 }
174179 proc . stderr . on ( "data" , ( data ) => {
175- console . error ( "Error " + data ) ;
180+ handleError ( "stderr" , data ) ;
176181 } ) ;
177-
178182 proc . on ( "end" , ( end ) => {
179183 console . error ( "End - " + end ) ;
180184 } ) ;
185+ proc . on ( "error" , error => {
186+ handleError ( "error" , error ) ;
187+ } ) ;
181188
182189 proc . stdout . on ( "data" , ( data ) => {
183190 var dataStr = previousData = previousData + data + ""
@@ -187,7 +194,7 @@ function spawnProcess(dir: string) {
187194 previousData = "" ;
188195 }
189196 catch ( ex ) {
190- console . log ( ex . message ) ;
197+ handleError ( "stdout" , ex . message ) ;
191198 return ;
192199 }
193200
@@ -309,13 +316,28 @@ function spawnProcess(dir: string) {
309316
310317function sendCommand < T extends ICommandResult > ( cmd : ICommand < T > ) : Promise < T > {
311318 return new Promise < ICommandResult > ( ( resolve , reject ) => {
319+ if ( ! proc ) {
320+ return reject ( "Python proc not initialized" ) ;
321+ }
312322 var exexcutionCmd = < IExecutionCommand < T > > cmd ;
313323 var payload = createPayload ( exexcutionCmd ) ;
314324 exexcutionCmd . resolve = resolve ;
315325 exexcutionCmd . reject = reject ;
316- proc . stdin . write ( JSON . stringify ( payload ) + "\n" ) ;
317- commands . set ( exexcutionCmd . id , exexcutionCmd ) ;
318- commandQueue . push ( exexcutionCmd . id ) ;
326+ try {
327+ proc . stdin . write ( JSON . stringify ( payload ) + "\n" ) ;
328+ commands . set ( exexcutionCmd . id , exexcutionCmd ) ;
329+ commandQueue . push ( exexcutionCmd . id ) ;
330+ }
331+ catch ( ex ) {
332+ //If 'This socket is closed.' that means process didn't start at all (at least not properly)
333+ if ( ex . message === "This socket is closed." ) {
334+ killProcess ( ) ;
335+ }
336+ else {
337+ handleError ( "sendCommand" , ex . message ) ;
338+ }
339+ reject ( ex . message ) ;
340+ }
319341 } ) ;
320342}
321343
@@ -447,7 +469,7 @@ export class JediProxyHandler<R extends ICommandResult, T> {
447469 this . cancellationTokenSource = new vscode . CancellationTokenSource ( ) ;
448470 executionCmd . token = this . cancellationTokenSource . token ;
449471
450- this . jediProxy . sendCommand < R > ( executionCmd ) . then ( data => this . onResolved ( data ) ) ;
472+ this . jediProxy . sendCommand < R > ( executionCmd ) . then ( data => this . onResolved ( data ) , ( ) => { } ) ;
451473 this . lastCommandId = executionCmd . id ;
452474 this . lastToken = token ;
453475 this . promiseResolve = resolve ;
0 commit comments