@@ -126,6 +126,13 @@ export async function activate(context: ExtensionContext) {
126126 async ( tel ) : Promise < ResultCode > => {
127127 return await emitCode ( context , uri , tel ) ;
128128 } ,
129+ undefined ,
130+ ( e ) => {
131+ logger . error ( "Unexpected error when emitting code from TypeSpec." , [ e ] , {
132+ showPopup : true ,
133+ } ) ;
134+ return ResultCode . Fail ;
135+ } ,
129136 ) ;
130137 } ,
131138 ) ;
@@ -165,6 +172,14 @@ export async function activate(context: ExtensionContext) {
165172 }
166173 } ,
167174 args ?. activityId ,
175+ ( e ) => {
176+ logger . error (
177+ "Unexpected error when restarting TypeSpec language server." ,
178+ [ e ] ,
179+ { showPopup : true } ,
180+ ) ;
181+ return { code : ResultCode . Fail } ;
182+ } ,
168183 ) ;
169184 } ,
170185 ) ;
@@ -198,7 +213,26 @@ export async function activate(context: ExtensionContext) {
198213 await telemetryClient . doOperationWithTelemetry (
199214 TelemetryEventName . PreviewOpenApi3 ,
200215 async ( tel ) : Promise < ResultCode > => {
201- return await showOpenApi3 ( uri , context , tspLanguageClient ! , tel ) ;
216+ if ( ! tspLanguageClient || tspLanguageClient . state !== State . Running ) {
217+ logger . error (
218+ "TypeSpec language server is not running. Please restart the server." ,
219+ [ ] ,
220+ { showPopup : true } ,
221+ ) ;
222+ telemetryClient . logOperationDetailTelemetry ( tel . activityId , {
223+ error : "LSP client is not running" ,
224+ } ) ;
225+ tel . lastStep = "Check LSP client" ;
226+ return ResultCode . Fail ;
227+ }
228+ return await showOpenApi3 ( uri , context , tspLanguageClient , tel ) ;
229+ } ,
230+ undefined ,
231+ ( e ) => {
232+ logger . error ( "Unexpected error when previewing OpenAPI3." , [ e ] , {
233+ showPopup : true ,
234+ } ) ;
235+ return ResultCode . Fail ;
202236 } ,
203237 ) ;
204238 } ) ,
@@ -213,6 +247,13 @@ export async function activate(context: ExtensionContext) {
213247 async ( tel ) => {
214248 return await recreateLSPClient ( context , tel . activityId ) ;
215249 } ,
250+ undefined ,
251+ ( e ) => {
252+ logger . error ( "Unexpected error when restarting server after path change." , [ e ] , {
253+ showPopup : true ,
254+ } ) ;
255+ return { code : ResultCode . Fail } ;
256+ } ,
216257 ) ;
217258 }
218259 } ) ,
@@ -318,14 +359,30 @@ export async function activate(context: ExtensionContext) {
318359 return installResult . code ;
319360 } ,
320361 tel . activityId ,
362+ ( e ) => {
363+ logger . error ( "Unexpected error when starting TypeSpec language server." , [ e ] , {
364+ showPopup : true ,
365+ } ) ;
366+ return ResultCode . Fail ;
367+ } ,
321368 ) ;
322369 } else {
323370 logger . info ( "No workspace opened, Skip starting TypeSpec language service." ) ;
371+ telemetryClient . logOperationDetailTelemetry ( tel . activityId , {
372+ compilerLocation : "skipped-no-workspace-or-tsp-file" ,
373+ } ) ;
324374 }
325375 showStartUpMessages ( stateManager ) ;
326376 telemetryClient . sendDelayedTelemetryEvents ( ) ;
327377 return ResultCode . Success ;
328378 } ,
379+ undefined ,
380+ ( e ) => {
381+ logger . error ( "Unexpected error when starting TypeSpec extension." , [ e ] , {
382+ showPopup : true ,
383+ } ) ;
384+ return ResultCode . Fail ;
385+ } ,
329386 ) ;
330387
331388 // Expose API for other extensions to consume
@@ -339,36 +396,52 @@ export async function activate(context: ExtensionContext) {
339396}
340397
341398export async function deactivate ( ) {
342- await tspLanguageClient ?. stop ( ) ;
343- await clearOpenApi3PreviewTempFolders ( ) ;
399+ try {
400+ await tspLanguageClient ?. stop ( ) ;
401+ await clearOpenApi3PreviewTempFolders ( ) ;
402+ } catch ( e ) {
403+ logger . error ( "Error during extension deactivation" , [ e ] ) ;
404+ }
344405}
345406
346407async function recreateLSPClient (
347408 context : ExtensionContext ,
348409 activityId : string ,
349410) : Promise < Result < TspLanguageClient > > {
350- logger . info ( "Recreating TypeSpec LSP server..." ) ;
351- const oldClient = tspLanguageClient ;
352- setTspLanguageClient ( await TspLanguageClient . create ( activityId , context , outputChannel ) ) ;
353- await oldClient ?. stop ( ) ;
354- if ( ! tspLanguageClient ) {
355- telemetryClient . logOperationDetailTelemetry ( activityId , {
356- error : "Failed to create TspLanguageClient" ,
357- } ) ;
358- return { code : ResultCode . Fail , details : "Failed to create TspLanguageClient." } ;
359- } else {
360- await tspLanguageClient . start ( activityId ) ;
361- if ( tspLanguageClient . state === State . Running ) {
411+ try {
412+ logger . info ( "Recreating TypeSpec LSP server..." ) ;
413+ const oldClient = tspLanguageClient ;
414+ setTspLanguageClient ( await TspLanguageClient . create ( activityId , context , outputChannel ) ) ;
415+ await oldClient ?. stop ( ) ;
416+ if ( ! tspLanguageClient ) {
362417 telemetryClient . logOperationDetailTelemetry ( activityId , {
363- compilerVersion : tspLanguageClient . initializeResult ?. serverInfo ?. version ?? "< 0.64.0 ",
418+ error : "Failed to create TspLanguageClient. Compiler could not be resolved. ",
364419 } ) ;
365- return { code : ResultCode . Success , value : tspLanguageClient } ;
420+ return { code : ResultCode . Fail , details : "Failed to create TspLanguageClient." } ;
366421 } else {
367- telemetryClient . logOperationDetailTelemetry ( activityId , {
368- error : `Failed to start TspLanguageClient.` ,
369- } ) ;
370- return { code : ResultCode . Fail , details : "TspLanguageClient is not running." } ;
422+ await tspLanguageClient . start ( activityId ) ;
423+ if ( tspLanguageClient . state === State . Running ) {
424+ telemetryClient . logOperationDetailTelemetry ( activityId , {
425+ compilerVersion : tspLanguageClient . initializeResult ?. serverInfo ?. version ?? "< 0.64.0" ,
426+ } ) ;
427+ return { code : ResultCode . Success , value : tspLanguageClient } ;
428+ } else {
429+ telemetryClient . logOperationDetailTelemetry ( activityId , {
430+ error : `Failed to start TspLanguageClient. State: ${ tspLanguageClient . state } ` ,
431+ } ) ;
432+ return { code : ResultCode . Fail , details : "TspLanguageClient is not running." } ;
433+ }
371434 }
435+ } catch ( e ) {
436+ logger . error (
437+ "TypeSpec language server is unavailable due to an unexpected error. Please restart the server." ,
438+ [ e ] ,
439+ { showPopup : true } ,
440+ ) ;
441+ telemetryClient . logOperationDetailTelemetry ( activityId , {
442+ error : `Unexpected error in recreateLSPClient: ${ e } ` ,
443+ } ) ;
444+ return { code : ResultCode . Fail , details : `Unexpected error: ${ e } ` } ;
372445 }
373446}
374447
0 commit comments