2015-01-16 03:55:26 +01:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2015-01-16 03:55:26 +01:00
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu;
|
|
|
|
|
|
|
|
import java.awt.Cursor;
|
|
|
|
import java.awt.Desktop;
|
2015-08-08 19:04:15 +02:00
|
|
|
import java.io.IOException;
|
2015-01-16 03:55:26 +01:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
2015-08-08 19:04:15 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2015-03-06 21:55:15 +01:00
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.util.Properties;
|
2015-01-16 03:55:26 +01:00
|
|
|
|
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
import javax.swing.JScrollPane;
|
|
|
|
import javax.swing.JTextArea;
|
|
|
|
import javax.swing.UIManager;
|
|
|
|
|
2016-12-09 02:00:14 +01:00
|
|
|
import org.lwjgl.opengl.GL11;
|
2015-01-16 03:55:26 +01:00
|
|
|
import org.newdawn.slick.util.Log;
|
2015-03-06 21:55:15 +01:00
|
|
|
import org.newdawn.slick.util.ResourceLoader;
|
2015-01-16 03:55:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Error handler to log and display errors.
|
|
|
|
*/
|
|
|
|
public class ErrorHandler {
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Error popup title. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final String title = "Error";
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Error popup description text. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final String
|
2015-01-22 06:44:45 +01:00
|
|
|
desc = "opsu! has encountered an error.",
|
2015-08-08 19:04:15 +02:00
|
|
|
descReport = "opsu! has encountered an error. Please report this!";
|
2015-01-16 03:55:26 +01:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Error popup button options. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final String[]
|
2015-08-08 19:04:15 +02:00
|
|
|
optionsLog = {"View Error Log", "Close"},
|
|
|
|
optionsReport = {"Send Report", "Close"},
|
|
|
|
optionsLogReport = {"Send Report", "View Error Log", "Close"};
|
2015-01-16 03:55:26 +01:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Text area for Exception. */
|
2016-09-27 19:58:45 +02:00
|
|
|
private static final JTextArea textArea = new JTextArea(15, 100);
|
2015-01-16 03:55:26 +01:00
|
|
|
static {
|
|
|
|
textArea.setEditable(false);
|
|
|
|
textArea.setBackground(UIManager.getColor("Panel.background"));
|
|
|
|
textArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
|
|
|
|
textArea.setTabSize(2);
|
2016-09-27 19:58:45 +02:00
|
|
|
textArea.setLineWrap(false);
|
2015-08-13 04:46:10 +02:00
|
|
|
textArea.setWrapStyleWord(true);
|
2015-01-16 03:55:26 +01:00
|
|
|
}
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Scroll pane holding JTextArea. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final JScrollPane scroll = new JScrollPane(textArea);
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Error popup objects. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final Object[]
|
2015-01-22 06:44:45 +01:00
|
|
|
message = { desc, scroll },
|
2015-08-08 19:04:15 +02:00
|
|
|
messageReport = { descReport, scroll };
|
2015-01-16 03:55:26 +01:00
|
|
|
|
2016-12-09 02:00:14 +01:00
|
|
|
/** OpenGL string (if any). */
|
|
|
|
private static String glString = null;
|
|
|
|
|
2015-01-16 03:55:26 +01:00
|
|
|
// This class should not be instantiated.
|
|
|
|
private ErrorHandler() {}
|
|
|
|
|
2016-12-09 02:00:14 +01:00
|
|
|
/**
|
|
|
|
* Sets the OpenGL version string.
|
|
|
|
*/
|
|
|
|
public static void setGlString() {
|
|
|
|
try {
|
|
|
|
String glVersion = GL11.glGetString(GL11.GL_VERSION);
|
|
|
|
String glVendor = GL11.glGetString(GL11.GL_VENDOR);
|
|
|
|
glString = String.format("%s (%s)", glVersion, glVendor);
|
|
|
|
} catch (Exception e) {}
|
|
|
|
}
|
|
|
|
|
2015-01-16 03:55:26 +01:00
|
|
|
/**
|
|
|
|
* Displays an error popup and logs the given error.
|
2015-06-30 02:22:38 +02:00
|
|
|
* @param error a description of the error
|
2015-01-16 03:55:26 +01:00
|
|
|
* @param e the exception causing the error
|
|
|
|
* @param report whether to ask to report the error
|
|
|
|
*/
|
|
|
|
public static void error(String error, Throwable e, boolean report) {
|
|
|
|
if (error == null && e == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// log the error
|
|
|
|
if (error == null)
|
|
|
|
Log.error(e);
|
|
|
|
else if (e == null)
|
|
|
|
Log.error(error);
|
|
|
|
else
|
|
|
|
Log.error(error, e);
|
|
|
|
|
|
|
|
// set the textArea to the error message
|
|
|
|
textArea.setText(null);
|
|
|
|
if (error != null) {
|
|
|
|
textArea.append(error);
|
|
|
|
textArea.append("\n");
|
|
|
|
}
|
2015-03-06 21:55:15 +01:00
|
|
|
String trace = null;
|
2015-01-16 03:55:26 +01:00
|
|
|
if (e != null) {
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
e.printStackTrace(new PrintWriter(sw));
|
2015-03-06 21:55:15 +01:00
|
|
|
trace = sw.toString();
|
|
|
|
textArea.append(trace);
|
2015-01-16 03:55:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// display popup
|
|
|
|
try {
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
2015-08-08 19:04:15 +02:00
|
|
|
Desktop desktop = null;
|
|
|
|
boolean isBrowseSupported = false, isOpenSupported = false;
|
2015-01-16 03:55:26 +01:00
|
|
|
if (Desktop.isDesktopSupported()) {
|
2015-08-08 19:04:15 +02:00
|
|
|
desktop = Desktop.getDesktop();
|
|
|
|
isBrowseSupported = desktop.isSupported(Desktop.Action.BROWSE);
|
|
|
|
isOpenSupported = desktop.isSupported(Desktop.Action.OPEN);
|
|
|
|
}
|
|
|
|
if (desktop != null && (isOpenSupported || (report && isBrowseSupported))) { // try to open the log file and/or issues webpage
|
|
|
|
if (report && isBrowseSupported) { // ask to report the error
|
|
|
|
if (isOpenSupported) { // also ask to open the log
|
|
|
|
int n = JOptionPane.showOptionDialog(null, messageReport, title,
|
|
|
|
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
|
|
|
|
null, optionsLogReport, optionsLogReport[2]);
|
|
|
|
if (n == 0)
|
|
|
|
desktop.browse(getIssueURI(error, e, trace));
|
|
|
|
else if (n == 1)
|
|
|
|
desktop.open(Options.LOG_FILE);
|
|
|
|
} else { // only ask to report the error
|
|
|
|
int n = JOptionPane.showOptionDialog(null, message, title,
|
|
|
|
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
|
|
|
|
null, optionsReport, optionsReport[1]);
|
|
|
|
if (n == 0)
|
|
|
|
desktop.browse(getIssueURI(error, e, trace));
|
|
|
|
}
|
|
|
|
} else { // don't report the error
|
2015-01-16 03:55:26 +01:00
|
|
|
int n = JOptionPane.showOptionDialog(null, message, title,
|
|
|
|
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
|
2015-08-08 19:04:15 +02:00
|
|
|
null, optionsLog, optionsLog[1]);
|
2015-01-16 03:55:26 +01:00
|
|
|
if (n == 0)
|
2015-08-08 19:04:15 +02:00
|
|
|
desktop.open(Options.LOG_FILE);
|
2015-01-16 03:55:26 +01:00
|
|
|
}
|
2015-08-08 19:04:15 +02:00
|
|
|
} else { // display error only
|
|
|
|
JOptionPane.showMessageDialog(null, report ? messageReport : message,
|
2015-01-16 03:55:26 +01:00
|
|
|
title, JOptionPane.ERROR_MESSAGE);
|
|
|
|
}
|
|
|
|
} catch (Exception e1) {
|
2015-08-08 19:04:15 +02:00
|
|
|
Log.error("An error occurred in the crash popup.", e1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the issue reporting URI.
|
|
|
|
* This will auto-fill the report with the relevant information if possible.
|
|
|
|
* @param error a description of the error
|
|
|
|
* @param e the exception causing the error
|
|
|
|
* @param trace the stack trace
|
|
|
|
* @return the created URI
|
|
|
|
*/
|
|
|
|
private static URI getIssueURI(String error, Throwable e, String trace) {
|
|
|
|
// generate report information
|
|
|
|
String issueTitle = (error != null) ? error : e.getMessage();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
try {
|
|
|
|
// read version and build date from version file, if possible
|
|
|
|
Properties props = new Properties();
|
|
|
|
props.load(ResourceLoader.getResourceAsStream(Options.VERSION_FILE));
|
|
|
|
String version = props.getProperty("version");
|
|
|
|
if (version != null && !version.equals("${pom.version}")) {
|
|
|
|
sb.append("**Version:** ");
|
|
|
|
sb.append(version);
|
2015-09-01 00:54:32 +02:00
|
|
|
String hash = Utils.getGitHash();
|
|
|
|
if (hash != null) {
|
|
|
|
sb.append(" (");
|
|
|
|
sb.append(hash.substring(0, 12));
|
|
|
|
sb.append(')');
|
|
|
|
}
|
2015-08-08 19:04:15 +02:00
|
|
|
sb.append('\n');
|
|
|
|
}
|
|
|
|
String timestamp = props.getProperty("build.date");
|
|
|
|
if (timestamp != null &&
|
|
|
|
!timestamp.equals("${maven.build.timestamp}") && !timestamp.equals("${timestamp}")) {
|
|
|
|
sb.append("**Build date:** ");
|
|
|
|
sb.append(timestamp);
|
|
|
|
sb.append('\n');
|
|
|
|
}
|
|
|
|
} catch (IOException e1) {
|
|
|
|
Log.warn("Could not read version file.", e1);
|
|
|
|
}
|
|
|
|
sb.append("**OS:** ");
|
|
|
|
sb.append(System.getProperty("os.name"));
|
|
|
|
sb.append(" (");
|
|
|
|
sb.append(System.getProperty("os.arch"));
|
|
|
|
sb.append(")\n");
|
|
|
|
sb.append("**JRE:** ");
|
|
|
|
sb.append(System.getProperty("java.version"));
|
|
|
|
sb.append('\n');
|
2016-12-09 02:00:14 +01:00
|
|
|
if (glString != null) {
|
|
|
|
sb.append("**OpenGL Version:** ");
|
|
|
|
sb.append(glString);
|
|
|
|
sb.append('\n');
|
|
|
|
}
|
2015-08-08 19:04:15 +02:00
|
|
|
if (error != null) {
|
|
|
|
sb.append("**Error:** `");
|
|
|
|
sb.append(error);
|
|
|
|
sb.append("`\n");
|
|
|
|
}
|
|
|
|
if (trace != null) {
|
|
|
|
sb.append("**Stack trace:**");
|
|
|
|
sb.append("\n```\n");
|
|
|
|
sb.append(trace);
|
|
|
|
sb.append("```");
|
|
|
|
}
|
|
|
|
|
|
|
|
// return auto-filled URI
|
|
|
|
try {
|
|
|
|
return URI.create(String.format(Options.ISSUES_URL,
|
|
|
|
URLEncoder.encode(issueTitle, "UTF-8"),
|
|
|
|
URLEncoder.encode(sb.toString(), "UTF-8")));
|
|
|
|
} catch (UnsupportedEncodingException e1) {
|
|
|
|
Log.warn("URLEncoder failed to encode the auto-filled issue report URL.");
|
|
|
|
return URI.create(String.format(Options.ISSUES_URL, "", ""));
|
2015-01-16 03:55:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|