forked from redhat-developer/vscode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientErrorHandler.ts
More file actions
57 lines (51 loc) · 1.71 KB
/
clientErrorHandler.ts
File metadata and controls
57 lines (51 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { window, commands } from "vscode";
import { ErrorHandler, Message, ErrorAction, CloseAction, ErrorHandlerResult, CloseHandlerResult } from "vscode-languageclient";
import { Commands } from "./commands";
import { logger } from "./log";
export class ClientErrorHandler implements ErrorHandler {
private restarts: number[];
constructor(private name: string) {
this.restarts = [];
}
public error(_error: Error, _message: Message, count: number): ErrorHandlerResult {
if (count && count <= 3) {
logger.error(`${this.name} server encountered error: ${_message}, ${_error && _error.toString()}`);
return {
action: ErrorAction.Continue
};
}
logger.error(`${this.name} server encountered error and will shut down: ${_message}, ${_error && _error.toString()}`);
return {
action: ErrorAction.Shutdown
};
}
public closed(): CloseHandlerResult {
this.restarts.push(Date.now());
if (this.restarts.length < 5) {
logger.error(`The ${this.name} server crashed and will restart.`);
return {
action: CloseAction.Restart
};
} else {
const diff = this.restarts[this.restarts.length - 1] - this.restarts[0];
if (diff <= 3 * 60 * 1000) {
const message = `The ${this.name} server crashed 5 times in the last 3 minutes. The server will not be restarted.`;
logger.error(message);
const action = "Show logs";
window.showErrorMessage(message, action).then(selection => {
if (selection === action) {
commands.executeCommand(Commands.OPEN_LOGS);
}
});
return {
action: CloseAction.DoNotRestart
};
}
logger.error(`The ${this.name} server crashed and will restart.`);
this.restarts.shift();
return {
action: CloseAction.Restart
};
}
}
}