@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License").
55You may not use this file except in compliance with the License.
66A copy of the License is located at
77
8- http://www.apache.org/licenses/LICENSE-2.0
8+ http://www.apache.org/licenses/LICENSE-2.0
99
1010or in the "license" file accompanying this file. This file is distributed
1111on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
@@ -31,6 +31,12 @@ import {
3131 LanguageClientOptions ,
3232 ServerOptions ,
3333 TransportKind ,
34+ ErrorHandler ,
35+ Message ,
36+ ErrorAction ,
37+ CloseAction ,
38+ CloseHandlerResult ,
39+ ErrorHandlerResult ,
3440} from "vscode-languageclient/node" ;
3541
3642let previews : { [ index : string ] : WebviewPanel } = { } ;
@@ -63,14 +69,14 @@ export async function activate(context: ExtensionContext) {
6369 { scheme : "file" , language : "json" } ,
6470 ] ,
6571 synchronize : {
66- // Synchronize the setting section 'languageServerExample' to the server
6772 configurationSection : "cfnLint" ,
6873 // Notify the server about file changes to '.clientrc files contain in the workspace
6974 fileEvents : [
7075 workspace . createFileSystemWatcher ( "**/.clientrc" ) ,
7176 workspace . createFileSystemWatcher ( "**/*.?(e)y?(a)ml" ) ,
7277 ] ,
7378 } ,
79+ errorHandler : new ClientErrorHandler ( 4 ) ,
7480 } ;
7581
7682 // Create the language client and start the client.
@@ -100,6 +106,14 @@ export async function activate(context: ExtensionContext) {
100106 }
101107 } ) ;
102108
109+ languageClient . onNotification ( "cfn/cfnLintUpgradeNeeded" , ( params ) => {
110+ window . showInformationMessage (
111+ `You are using an outdated version of cfn-lint (${ params [ "cli_version" ] } ). The latest version is ${ params [ "latest_version" ] } .`
112+ ) ;
113+ } ) ;
114+
115+ languageClient . sendNotification ( "cfn/validateCfnLintVersion" ) ;
116+
103117 let previewDisposable = commands . registerCommand (
104118 "extension.sidePreview" ,
105119 ( ) => {
@@ -177,3 +191,52 @@ export function deactivate(): Thenable<void> | undefined {
177191 }
178192 return languageClient . stop ( ) ;
179193}
194+
195+ export class ClientErrorHandler implements ErrorHandler {
196+ private restarts : number [ ] = [ ] ;
197+ constructor ( private readonly maxRestartCount : number ) { }
198+
199+ //@ts -ignore
200+ error ( error : Error , message : Message , count : number ) : ErrorHandlerResult {
201+ if ( count && count <= 3 ) {
202+ return {
203+ action : ErrorAction . Continue ,
204+ message : error . message ,
205+ handled : true ,
206+ } ;
207+ }
208+ return {
209+ action : ErrorAction . Shutdown ,
210+ message : error . message ,
211+ handled : true ,
212+ } ;
213+ }
214+
215+ closed ( ) : CloseHandlerResult {
216+ this . restarts . push ( Date . now ( ) ) ;
217+ if ( this . restarts . length <= this . maxRestartCount ) {
218+ return {
219+ action : CloseAction . Restart ,
220+ message : "Restarting cfn-lint extension" ,
221+ } ;
222+ } else {
223+ const diff = this . restarts [ this . restarts . length - 1 ] - this . restarts [ 0 ] ;
224+ if ( diff <= 3 * 60 * 1000 ) {
225+ const message = `The cfn-lint server crashed ${
226+ this . maxRestartCount + 1
227+ } times in the last 3 minutes. The server will not be restarted.`;
228+ window . showErrorMessage ( message ) ;
229+ return {
230+ action : CloseAction . DoNotRestart ,
231+ message : message ,
232+ } ;
233+ } else {
234+ this . restarts . shift ( ) ;
235+ return {
236+ action : CloseAction . Restart ,
237+ message : "Restart cfn-lint extension" ,
238+ } ;
239+ }
240+ }
241+ }
242+ }
0 commit comments