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
79 lines (73 loc) · 2.24 KB
/
clientErrorHandler.ts
File metadata and controls
79 lines (73 loc) · 2.24 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { window, commands } from "vscode";
import { serverStatusBarProvider } from './serverStatusBarProvider';
import { ErrorHandler, Message, ErrorAction, CloseAction, ErrorHandlerResult, CloseHandlerResult } from "vscode-languageclient";
import { Commands } from "./commands";
import { logger } from "./log";
import { apiManager } from "./apiManager";
const CLIENT_ERROR = "java.client.error";
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,
handled: true
};
}
const errorMessage = `${this.name} server encountered error and will shut down: ${_message}, ${_error && _error.toString()}`;
apiManager.fireTraceEvent({
name: CLIENT_ERROR,
properties: {
message: errorMessage,
},
});
logger.error(errorMessage);
return {
action: ErrorAction.Shutdown,
handled: true
};
}
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,
handled: true
};
} 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.`;
apiManager.fireTraceEvent({
name: CLIENT_ERROR,
properties: {
message,
},
});
logger.error(message);
serverStatusBarProvider.setError();
const action = "Show logs";
window.showErrorMessage(message, action).then(selection => {
if (selection === action) {
commands.executeCommand(Commands.OPEN_LOGS);
}
});
return {
action: CloseAction.DoNotRestart,
handled: true
};
}
logger.error(`The ${this.name} server crashed and will restart.`);
this.restarts.shift();
return {
action: CloseAction.Restart,
handled: true
};
}
}
}