@@ -327,7 +327,7 @@ export const activate = (context: vscode.ExtensionContext) => {
327327 value as UpdateMessageContents ;
328328 const doc = get_document ( current_update . file_path ) ;
329329 if ( doc === undefined ) {
330- sendResult ( id , {
330+ await sendResult ( id , {
331331 NoOpenDocument : current_update . file_path ,
332332 } ) ;
333333 break ;
@@ -358,7 +358,7 @@ export const activate = (context: vscode.ExtensionContext) => {
358358 assert ( "Diff" in source ) ;
359359 // If this diff was not made against the text we currently have, reject it.
360360 if ( source . Diff . version !== version ) {
361- sendResult ( id , "OutOfSync" ) ;
361+ await sendResult ( id , "OutOfSync" ) ;
362362 // Send an `Update` with the full text to re-sync the Client.
363363 send_update ( true ) ;
364364 break ;
@@ -437,7 +437,7 @@ export const activate = (context: vscode.ExtensionContext) => {
437437 ) ,
438438 ] ;
439439 }
440- sendResult ( id ) ;
440+ await sendResult ( id ) ;
441441 break ;
442442 }
443443
@@ -452,7 +452,7 @@ export const activate = (context: vscode.ExtensionContext) => {
452452 current_file ,
453453 ) ;
454454 } catch ( e ) {
455- sendResult ( id , {
455+ await sendResult ( id , {
456456 OpenFileFailed : [
457457 current_file ,
458458 ( e as Error ) . toString ( ) ,
@@ -467,7 +467,7 @@ export const activate = (context: vscode.ExtensionContext) => {
467467 current_editor ?. viewColumn ,
468468 ) ;
469469 ignore_active_editor_change = false ;
470- sendResult ( id ) ;
470+ await sendResult ( id ) ;
471471 } else {
472472 // TODO: open using a custom document editor.
473473 // See
@@ -489,17 +489,17 @@ export const activate = (context: vscode.ExtensionContext) => {
489489 } ,
490490 )
491491 . then (
492- ( ) => sendResult ( id ) ,
493- ( reason ) =>
494- sendResult ( id , {
492+ async ( ) => await sendResult ( id ) ,
493+ async ( reason ) =>
494+ await sendResult ( id , {
495495 OpenFileFailed : [
496496 current_file ,
497497 reason ,
498498 ] ,
499499 } ) ,
500500 ) ;
501501 }
502- sendResult ( id ) ;
502+ await sendResult ( id ) ;
503503 }
504504 break ;
505505 }
@@ -530,7 +530,7 @@ export const activate = (context: vscode.ExtensionContext) => {
530530 console_log (
531531 `CodeChat Editor extension: Result(LoadFile(${ format_struct ( load_file_result ) } ))` ,
532532 ) ;
533- codeChatEditorServer . sendResultLoadfile (
533+ await codeChatEditorServer . sendResultLoadfile (
534534 id ,
535535 load_file_result ,
536536 ) ;
@@ -541,7 +541,7 @@ export const activate = (context: vscode.ExtensionContext) => {
541541 const client_html = value as string ;
542542 assert ( webview_panel !== undefined ) ;
543543 webview_panel . webview . html = client_html ;
544- sendResult ( id ) ;
544+ await sendResult ( id ) ;
545545 // Now that the Client is loaded, send the editor's
546546 // current file to the server.
547547 send_update ( false ) ;
@@ -576,19 +576,27 @@ export const deactivate = async () => {
576576// Format a complex data structure as a string when in debug mode.
577577const format_struct = ( complex_data_structure : any ) : string =>
578578 DEBUG_ENABLED
579- ? JSON . stringify ( complex_data_structure ) . substring (
579+ // If the struct is `undefined`, print an empty string.
580+ ? JSON . stringify ( complex_data_structure ?? "" ) . substring (
580581 0 ,
581582 MAX_MESSAGE_LENGTH ,
582583 )
583584 : "" ;
584585
585586// Send a result (a response to a message from the server) back to the server.
586- const sendResult = ( id : number , result : ResultErrTypes | null = null ) => {
587+ const sendResult = async ( id : number , result ? : ResultErrTypes ) => {
587588 assert ( codeChatEditorServer ) ;
588589 console_log (
589590 `CodeChat Editor extension: sending Result(id = ${ id } , ${ format_struct ( result ) } ).` ,
590591 ) ;
591- codeChatEditorServer . sendResult ( id , JSON . stringify ( result ) ) ;
592+ try {
593+ await codeChatEditorServer . sendResult (
594+ id ,
595+ result === undefined ? undefined : JSON . stringify ( result ) ,
596+ ) ;
597+ } catch ( e ) {
598+ show_error ( `Error in sendResult for id ${ id } : ${ e } .` ) ;
599+ }
592600} ;
593601
594602// This is called after an event such as an edit, when the CodeChat panel
@@ -614,9 +622,13 @@ const send_update = (this_is_dirty: boolean) => {
614622 console_log (
615623 `CodeChat Editor extension: sending CurrentFile(${ current_file } }).` ,
616624 ) ;
617- await codeChatEditorServer ! . sendMessageCurrentFile (
618- current_file ,
619- ) ;
625+ try {
626+ await codeChatEditorServer ! . sendMessageCurrentFile (
627+ current_file ,
628+ ) ;
629+ } catch ( e ) {
630+ show_error ( `Error sending CurrentFile message: ${ e } .` ) ;
631+ }
620632 // Since we just requested a new file, the contents are
621633 // clean by definition.
622634 is_dirty = false ;
0 commit comments