Skip to content

Commit

Permalink
Improve Error Handler and remove useless mirrors
Browse files Browse the repository at this point in the history
  • Loading branch information
CloneWith committed Apr 23, 2024
1 parent 88bfaf7 commit 58d765b
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 884 deletions.
98 changes: 72 additions & 26 deletions src/itdelatrisu/opsu/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package itdelatrisu.opsu;

import itdelatrisu.opsu.options.Options;
import itdelatrisu.opsu.ui.UI;

import java.awt.Cursor;
import java.awt.Desktop;
Expand All @@ -36,6 +37,7 @@
import javax.swing.UIManager;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.util.Log;
import org.newdawn.slick.util.ResourceLoader;

Expand All @@ -47,15 +49,13 @@ public class ErrorHandler {
private static final String title = "Error";

/** Error popup description text. */
private static final String
desc = "An error occurred. :(",
descReport = "Something bad happened. Please report this!";
private static final String desc = "An error occurred. :(",
descReport = "Something bad happened. Please report this!";

/** Error popup button options. */
private static final String[]
optionsLog = {"View Error Log", "Close"},
optionsReport = {"Send Report", "Close"},
optionsLogReport = {"Send Report", "View Error Log", "Close"};
private static final String[] optionsLog = { "View Error Log", "Close" },
optionsReport = { "Send Report", "Close" },
optionsLogReport = { "Send Report", "View Error Log", "Close" };

/** Text area for Exception. */
private static final JTextArea textArea = new JTextArea(7, 30);
Expand All @@ -72,15 +72,15 @@ public class ErrorHandler {
private static final JScrollPane scroll = new JScrollPane(textArea);

/** Error popup objects. */
private static final Object[]
message = { desc, scroll },
messageReport = { descReport, scroll };
private static final Object[] message = { desc, scroll },
messageReport = { descReport, scroll };

/** OpenGL string (if any). */
private static String glString = null;

// This class should not be instantiated.
private ErrorHandler() {}
private ErrorHandler() {
}

/**
* Sets the OpenGL version string.
Expand All @@ -90,13 +90,57 @@ public static void setGlString() {
String glVersion = GL11.glGetString(GL11.GL_VERSION);
String glVendor = GL11.glGetString(GL11.GL_VENDOR);
glString = String.format("%s (%s)", glVersion, glVendor);
} catch (Exception e) {}
} catch (Exception e) {
}
}

/**
* Displays an error bar notification and logs the given error.
*
* @author CloneWith
* @param error a description of the error
* @param e the exception causing the error
*/
public static void bar(String des, Throwable e) {
if (des == null && e == null)
return;
if (des == null)
Log.error(e);
else if (e == null) {
Log.error(des);
UI.getNotificationManager().sendBarNotification(des);
} else {
Log.error(des, e);
UI.getNotificationManager().sendBarNotification(des);
}
}

/**
* Displays an error popup and logs the given error.
* @param error a description of the error
* @param e the exception causing the error
*
* @author CloneWith
* @param error a description of the error
* @param e the exception causing the error
*/
public static void notify(String des, Throwable e) {
if (des == null && e == null)
return;
if (des == null)
Log.error(e);
else if (e == null) {
Log.error(des);
UI.getNotificationManager().sendNotification(des, Color.red);
} else {
Log.error(des, e);
UI.getNotificationManager().sendNotification(des, Color.red);
}
}

/**
* Displays an error popup and logs the given error.
*
* @param error a description of the error
* @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) {
Expand Down Expand Up @@ -135,31 +179,32 @@ else if (e == null)
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
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
} 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
} else { // don't report the error
int n = JOptionPane.showOptionDialog(null, message, title,
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
null, optionsLog, optionsLog[1]);
if (n == 0)
desktop.open(Options.LOG_FILE);
}
} else { // display error only
} else { // display error only
JOptionPane.showMessageDialog(null, report ? messageReport : message,
title, JOptionPane.ERROR_MESSAGE);
}
Expand All @@ -170,16 +215,16 @@ else if (n == 1)

/**
* Returns the issue reporting URI with the given title and body.
*
* @param title the issue title
* @param body the issue body
* @param body the issue body
* @return the encoded URI
*/
public static URI getIssueURI(String title, String body) {
try {
return URI.create(String.format(OpsuConstants.ISSUES_URL,
URLEncoder.encode(title, "UTF-8"),
URLEncoder.encode(body, "UTF-8"))
);
URLEncoder.encode(title, "UTF-8"),
URLEncoder.encode(body, "UTF-8")));
} catch (UnsupportedEncodingException e) {
Log.warn("URLEncoder failed to encode the auto-filled issue report URL.");
return URI.create(String.format(OpsuConstants.ISSUES_URL, "", ""));
Expand Down Expand Up @@ -209,7 +254,7 @@ public static String getEnvironmentInfoForIssue() {
}
String timestamp = props.getProperty("build.date");
if (timestamp != null &&
!timestamp.equals("${maven.build.timestamp}") && !timestamp.equals("${timestamp}")) {
!timestamp.equals("${maven.build.timestamp}") && !timestamp.equals("${timestamp}")) {
sb.append("**Build date:** ");
sb.append(timestamp);
sb.append('\n');
Expand All @@ -236,8 +281,9 @@ public static String getEnvironmentInfoForIssue() {
/**
* 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 e the exception causing the error
* @param trace the stack trace
* @return the created URI
*/
Expand Down
5 changes: 3 additions & 2 deletions src/itdelatrisu/opsu/GameImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.Log;
import org.newdawn.slick.util.ResourceLoader;

/**
Expand Down Expand Up @@ -773,7 +774,7 @@ private Image[] loadImageArray(File dir) {
img = img.getScaledCopy(0.5f);
list.add(img);
} catch (SlickException e) {
ErrorHandler.error(String.format("Failed to set image '%s'.", name), null, false);
Log.error(String.format("Failed to set image '%s'.", name), e);
break;
}
}
Expand All @@ -799,7 +800,7 @@ private Image loadImageSingle(File dir) {
img = img.getScaledCopy(0.5f);
return img;
} catch (SlickException e) {
ErrorHandler.error(String.format("Failed to set image '%s'.", filename), null, false);
Log.error(String.format("Failed to set image '%s'.", name), e);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/itdelatrisu/opsu/OpsuConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public class OpsuConstants {
public static final URI CREDITS_URI = URI.create("https://github.com/clonewith/opsu/blob/master/CREDITS.md");

/** Issue reporting address. */
public static final String ISSUES_URL = "https://github.com/clonewith/opsu/issues/new?title=%s&body=%s";
public static final String ISSUES_URL = "https://github.com/CloneWith/opsu/discussions/new?category=bug-report&title=%s&body=%s";

/** Address containing the latest version file. */
public static final String VERSION_REMOTE = "https://raw.githubusercontent.com/itdelatrisu/opsu/gh-pages/version";
public static final String VERSION_REMOTE = "https://raw.githubusercontent.com/clonewith/opsu/gh-pages/version";

/** Changelog address. */
private static final String CHANGELOG_URL = "https://github.com/clonewith/opsu/releases/tag/%s";
Expand Down
2 changes: 2 additions & 0 deletions src/itdelatrisu/opsu/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ public void click() {
try {
Utils.openInFileManager(file);
} catch (IOException e) {
UI.getNotificationManager()
.sendBarNotification("Failed to open screenshot location.");
Log.warn("Failed to open screenshot location.", e);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/itdelatrisu/opsu/audio/SoundController.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static void init() {
// menu and game sounds
for (SoundEffect s : SoundEffect.values()) {
if ((currentFileName = getSoundFileName(s.getFileName())) == null) {
ErrorHandler.error(String.format("Could not find sound file '%s'.", s.getFileName()), null, false);
ErrorHandler.notify(String.format("Could not find sound file '%s'.", s.getFileName()), null);
continue;
}
MultiClip newClip = loadClip(currentFileName);
Expand All @@ -249,7 +249,7 @@ public static void init() {
for (HitSound s : HitSound.values()) {
String filename = String.format("%s-%s", ss.getName(), s.getFileName());
if ((currentFileName = getSoundFileName(filename)) == null) {
ErrorHandler.error(String.format("Could not find hit sound file '%s'.", filename), null, false);
ErrorHandler.notify(String.format("Could not find hit sound file '%s'.", filename), null);
continue;
}
MultiClip newClip = loadClip(currentFileName);
Expand Down
6 changes: 3 additions & 3 deletions src/itdelatrisu/opsu/beatmap/BeatmapSetList.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ else if (ePrev != null && ePrev.index == expandedIndex)
try {
Utils.deleteToTrash(dir);
} catch (IOException e) {
ErrorHandler.error("Could not delete song group.", e, true);
ErrorHandler.bar("Could not delete song group.", e);
}
if (ws != null)
ws.resume();
Expand Down Expand Up @@ -270,7 +270,7 @@ public boolean deleteSong(BeatmapSetNode node) {
try {
Utils.deleteToTrash(file);
} catch (IOException e) {
ErrorHandler.error("Could not delete song.", e, true);
ErrorHandler.bar("Could not delete song.", e);
}
if (ws != null)
ws.resume();
Expand Down Expand Up @@ -544,4 +544,4 @@ public boolean search(String query) {
public Beatmap getBeatmapFromHash(String beatmapHash) {
return beatmapHashDB.get(beatmapHash);
}
}
}
2 changes: 1 addition & 1 deletion src/itdelatrisu/opsu/downloads/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public void cancel() {
}
} catch (IOException e) {
this.status = Status.ERROR;
ErrorHandler.error("Failed to cancel download.", e, true);
ErrorHandler.notify("Failed to cancel download.", e);
}
}
}
2 changes: 1 addition & 1 deletion src/itdelatrisu/opsu/downloads/DownloadNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public void drawResult(Graphics g, float position, boolean hover, boolean focus,
public void drawDownload(Graphics g, float position, int id, boolean hover) {
Download download = this.download; // in case clearDownload() is called asynchronously
if (download == null) {
ErrorHandler.error("Trying to draw download information for button without Download object.", null, false);
ErrorHandler.notify("Trying to draw download information for button without Download object.", null);
return;
}

Expand Down
Loading

0 comments on commit 58d765b

Please sign in to comment.