-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathCrashHandler.java
More file actions
337 lines (279 loc) · 11.3 KB
/
Copy pathCrashHandler.java
File metadata and controls
337 lines (279 loc) · 11.3 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* SPDX-FileCopyrightText: 2011-2024 Minicraft+ contributors
* SPDX-License-Identifier: GPL-3.0-only
*/
package minicraft.core;
import kong.unirest.Empty;
import kong.unirest.HttpResponse;
import minicraft.core.CrashHandler.ErrorInfo.ErrorType;
import minicraft.core.io.ClipboardHandler;
import minicraft.network.Analytics;
import minicraft.util.Logging;
import org.jetbrains.annotations.Nullable;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.Future;
public class CrashHandler {
public static void crashHandle(Thread thread, Throwable throwable) {
crashHandle(throwable);
}
public static void crashHandle(Throwable throwable) {
crashHandle(throwable, new ErrorInfo(true));
}
/**
* This handles application crashing errors by giving notification to the user clearly.<br>
* The user can only exit the program.
*/
public static void crashHandle(Throwable throwable, ErrorInfo info) {
Logging.CRASHHANDLER.error(throwable);
StringWriter string = new StringWriter();
PrintWriter printer = new PrintWriter(string);
throwable.printStackTrace(printer);
Future<HttpResponse<Empty>> ping = Analytics.Crashes.ping();
// Ensure ping finishes before program closes.
if (GraphicsEnvironment.isHeadless() && ping != null) {
try {
ping.get();
} catch (Exception ignored) {
}
return;
}
Logging.CRASHHANDLER.error("Crash: " + info.type.name + ": " + info.title + (info.message != null ? ": " + info.message : ""));
JDialog dialog = new JDialog(Initializer.frame, "Crash: " + info.type.name, true); // Displays the error type.
JLabel icon = new JLabel(UIManager.getIcon(info.serious ? "OptionPane.errorIcon" : "OptionPane.warningIcon"));
icon.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
JPanel boxPanel1 = new JPanel(); // Styling the icon.
boxPanel1.setLayout(new BoxLayout(boxPanel1, BoxLayout.Y_AXIS));
boxPanel1.add(icon);
dialog.getContentPane().add(boxPanel1, BorderLayout.WEST);
JLabel title = new JLabel(info.title);
title.setFont(title.getFont().deriveFont(20.0f));
title.setAlignmentX(JLabel.CENTER_ALIGNMENT);
JPanel gridPanel = new JPanel(new GridBagLayout()); // Centers the components.
JPanel boxPanel2 = new JPanel();
boxPanel2.setLayout(new BoxLayout(boxPanel2, BoxLayout.Y_AXIS));
gridPanel.add(boxPanel2);
boxPanel2.add(title);
if (info.message != null && info.message.length() > 0) { // Displays the error message when available.
JLabel msg = new JLabel(info.message);
msg.setFont(title.getFont().deriveFont(12.5f).deriveFont(Font.PLAIN));
msg.setAlignmentX(JLabel.CENTER_ALIGNMENT);
boxPanel2.add(msg);
}
// Add all components to the dialog.
gridPanel.setBorder(BorderFactory.createEmptyBorder(3, 0, 3, 0));
dialog.getContentPane().add(gridPanel, BorderLayout.NORTH);
dialog.getContentPane().add(getCrashPanel(info, getErrorScrollPane(string.toString()), dialog, string.toString()));
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); // Disables the close button.
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true); // Shows the dialog.
dialog.dispose();
// Ensure ping finishes before program closes.
if (ping != null) {
try {
ping.get();
} catch (Exception ignored) {
}
}
// Exits the program when the dialog closes.
Logging.CRASHHANDLER.error("Application closes due to the crash.");
System.exit(info.type.exitCode);
}
public static void errorHandle(Throwable throwable) {
errorHandle(throwable, new ErrorInfo());
}
public static void errorHandle(Throwable throwable, ErrorInfo info) {
errorHandle(throwable, info, null);
}
/**
* This handles application crashing errors by giving notification to the user clearly.<br>
* The user can ignore the error, continue handling the error or exit the program (only in serious errors or error reports).
* @param handling The handling function of the error.
*/
public static void errorHandle(Throwable throwable, ErrorInfo info, @Nullable Action handling) {
throwable.printStackTrace();
StringWriter string = new StringWriter();
PrintWriter printer = new PrintWriter(string);
throwable.printStackTrace(printer);
Future<HttpResponse<Empty>> ping = Analytics.Crashes.ping();
// Ensure ping finishes before program closes.
if (GraphicsEnvironment.isHeadless() && ping != null) {
try {
ping.get();
} catch (Exception ignored) {
}
return;
}
Logging.CRASHHANDLER.error(info.type.name + ": " + info.title + (info.message != null ? ": " + info.message : ""));
JDialog dialog = new JDialog(Initializer.frame, "Error: " + info.type.name, true); // Displays the error type.
// Sets the icon depends on the error type.
JLabel icon = new JLabel(info.serious ? UIManager.getIcon("OptionPane.errorIcon") : info.type == ErrorType.REPORT ? UIManager.getIcon("OptionPane.informationIcon") : UIManager.getIcon("OptionPane.warningIcon"));
icon.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
JPanel boxPanel1 = new JPanel(); // Styling the icon.
boxPanel1.setLayout(new BoxLayout(boxPanel1, BoxLayout.Y_AXIS));
boxPanel1.add(icon);
dialog.getContentPane().add(boxPanel1, BorderLayout.WEST);
JLabel title = new JLabel(info.title);
title.setFont(title.getFont().deriveFont(20.0f));
title.setAlignmentX(JLabel.CENTER_ALIGNMENT);
JPanel gridPanel = new JPanel(new GridBagLayout()); // Centers the components.
JPanel boxPanel2 = new JPanel();
boxPanel2.setLayout(new BoxLayout(boxPanel2, BoxLayout.Y_AXIS));
gridPanel.add(boxPanel2);
boxPanel2.add(title);
if (info.message != null && info.message.length() > 0) { // Displays the error message when available.
JLabel msg = new JLabel(info.message);
msg.setFont(title.getFont().deriveFont(12.5f).deriveFont(Font.PLAIN));
msg.setAlignmentX(JLabel.CENTER_ALIGNMENT);
boxPanel2.add(msg);
}
// Add all components to the dialog.
gridPanel.setBorder(BorderFactory.createEmptyBorder(3, 0, 3, 0));
dialog.getContentPane().add(gridPanel, BorderLayout.NORTH);
dialog.getContentPane().add(getErrorPanel(info, getErrorScrollPane(string.toString()), dialog, string.toString(), handling, ping));
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); // Disables the close button.
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true); // Shows the dialog.
}
/**
* Getting the stack trace display component.
*/
private static JScrollPane getErrorScrollPane(String stackTrace) {
JTextArea errorDisplay = new JTextArea(stackTrace);
errorDisplay.setEditable(false);
return new JScrollPane(errorDisplay);
}
/**
* Getting the panel for crashing handling dialog.
*/
private static JPanel getCrashPanel(ErrorInfo info, JScrollPane errorPane, JDialog dialog, String stackTrace) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(errorPane);
JPanel buttonPanel = new JPanel();
JButton copyButton = new JButton("Copy Error"); // Copies the information of the error.
ClipboardHandler clip = new ClipboardHandler();
copyButton.addActionListener(e -> clip.setClipboardContents(info.type.name + ": " + info.title + (info.message != null ? ": " + info.message : "") + "\n" + stackTrace));
buttonPanel.add(copyButton);
JButton exitButton = new JButton("Exit Program"); // Closes the dialog to exit program.
exitButton.addActionListener(e -> {
dialog.setVisible(false);
dialog.dispose();
});
dialog.getRootPane().setDefaultButton(exitButton);
buttonPanel.add(exitButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
return panel;
}
private static JPanel getErrorPanel(ErrorInfo info, JScrollPane errorPane, JDialog dialog, String stackTrace, Action callback, Future<HttpResponse<Empty>> ping) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(errorPane);
JPanel buttonPanel = new JPanel();
JButton copyButton = new JButton("Copy Error"); // Copies the information of the error.
ClipboardHandler clip = new ClipboardHandler();
copyButton.addActionListener(e -> clip.setClipboardContents(info.type.name + ": " + info.title + (info.message != null ? ": " + info.message : "") + "\n" + stackTrace));
buttonPanel.add(copyButton);
if (info.serious || info.type == ErrorType.REPORT) { // Ability to exit program when the error is serious or error report.
JButton exitButton = new JButton("Exit Program");
exitButton.addActionListener(e -> {
dialog.setVisible(false);
dialog.dispose();
// Ensure ping finishes before program closes.
if (ping != null) {
try {
ping.get();
} catch (Exception ignored) {
}
}
Logging.CRASHHANDLER.error("Application closes due to the error.");
System.exit(info.type.exitCode);
});
buttonPanel.add(exitButton);
}
if (callback != null) {
JButton ignoreButton = new JButton("Ignore"); // Continues the program without handling the error with the callback.
ignoreButton.addActionListener(e -> {
dialog.setVisible(false);
dialog.dispose();
});
buttonPanel.add(ignoreButton);
}
JButton continueButton = new JButton("Continue"); // Continues the program and also handles the error with the callback.
continueButton.addActionListener(e -> {
dialog.setVisible(false);
dialog.dispose();
if (callback != null) callback.act();
});
dialog.getRootPane().setDefaultButton(continueButton);
buttonPanel.add(continueButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
return panel;
}
public static class ErrorInfo {
public final String title;
public final ErrorType type;
public final String message;
public final boolean serious;
public ErrorInfo() {
this(false);
}
public ErrorInfo(boolean crashing) {
this(crashing ? "General Application Crash" : "General Application Error",
crashing ? ErrorType.DEFAULT : ErrorType.REPORT);
}
public ErrorInfo(String topic) {
this(topic, ErrorType.DEFAULT);
}
public ErrorInfo(String topic, ErrorType type) {
this(topic, type, type.exitCode != 0);
}
public ErrorInfo(String topic, ErrorType type, boolean serious) {
this(topic, type, serious, null);
}
public ErrorInfo(String topic, ErrorType type, String message) {
this(topic, type, type.exitCode < 0, message);
}
public ErrorInfo(String topic, ErrorType type, boolean serious, String message) {
this.title = topic;
this.type = type;
this.message = message;
this.serious = serious;
}
/**
* The error types. Add more types when needed.
*/
public static enum ErrorType {
DEFAULT(-1, "Unhandled error"),
UNEXPECTED(-2, "Unexpected error"),
UNHANDLEABLE(-3, "Unhandleable error"),
SERIOUS(1, "Serious error"),
HANDLED(0, "Handled error"),
REPORT(0, "Error report"),
;
/**
* The exit codes are referring to https://www.techiedelight.com/exit-codes-java-system-exit-method/
*/
public final int exitCode;
public final String name;
ErrorType(int exitCode, String message) {
this.exitCode = exitCode;
this.name = message;
}
}
}
}