Rework error handling to be more robust

This commit is contained in:
comp500
2020-12-15 17:28:23 +00:00
parent 1d4c94f5b6
commit 0858c90079
10 changed files with 170 additions and 115 deletions

View File

@@ -3,16 +3,15 @@ package link.infra.packwiz.installer.ui
import link.infra.packwiz.installer.ui.data.ExceptionDetails
import link.infra.packwiz.installer.ui.data.IOptionDetails
import link.infra.packwiz.installer.ui.data.InstallProgress
import kotlin.system.exitProcess
interface IUserInterface {
fun show()
fun dispose()
fun handleException(e: Exception)
fun handleExceptionAndExit(e: Exception) {
handleException(e)
exitProcess(1)
fun showErrorAndExit(message: String): Nothing {
showErrorAndExit(message, null)
}
fun showErrorAndExit(message: String, e: Exception?): Nothing
fun setTitle(title: String) {}
fun submitProgress(progress: InstallProgress)

View File

@@ -5,6 +5,7 @@ import link.infra.packwiz.installer.ui.IUserInterface.ExceptionListResult
import link.infra.packwiz.installer.ui.data.ExceptionDetails
import link.infra.packwiz.installer.ui.data.IOptionDetails
import link.infra.packwiz.installer.ui.data.InstallProgress
import link.infra.packwiz.installer.util.Log
import kotlin.system.exitProcess
class CLIHandler : IUserInterface {
@@ -13,8 +14,13 @@ class CLIHandler : IUserInterface {
@Volatile
override var cancelButtonPressed = false
override fun handleException(e: Exception) {
e.printStackTrace()
override fun showErrorAndExit(message: String, e: Exception?): Nothing {
if (e != null) {
Log.fatal(message, e)
} else {
Log.fatal(message)
}
exitProcess(1)
}
override fun show() {}
@@ -36,7 +42,7 @@ class CLIHandler : IUserInterface {
for (opt in options) {
opt.optionValue = true
// TODO: implement option choice in the CLI?
println("Warning: accepting option " + opt.name + " as option choosing is not implemented in the CLI")
Log.warn("Accepting option ${opt.name} as option choosing is not implemented in the CLI")
}
return false // Can't be cancelled!
}
@@ -44,9 +50,9 @@ class CLIHandler : IUserInterface {
override fun showExceptions(exceptions: List<ExceptionDetails>, numTotal: Int, allowsIgnore: Boolean): ExceptionListResult {
println("Failed to download modpack, the following errors were encountered:")
for (ex in exceptions) {
println(ex.name + ": ")
print(ex.name + ": ")
ex.exception.printStackTrace()
}
exitProcess(1)
return ExceptionListResult.CANCEL
}
}

View File

@@ -5,6 +5,7 @@ import link.infra.packwiz.installer.ui.IUserInterface.ExceptionListResult
import link.infra.packwiz.installer.ui.data.ExceptionDetails
import link.infra.packwiz.installer.ui.data.IOptionDetails
import link.infra.packwiz.installer.ui.data.InstallProgress
import link.infra.packwiz.installer.util.Log
import java.awt.EventQueue
import java.util.concurrent.CompletableFuture
import javax.swing.JDialog
@@ -27,8 +28,7 @@ class GUIHandler : IUserInterface {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
} catch (e: Exception) {
println("Failed to set look and feel:")
e.printStackTrace()
Log.warn("Failed to set look and feel", e)
}
frmPackwizlauncher = InstallWindow(this).apply {
title = this@GUIHandler.title
@@ -44,23 +44,23 @@ class GUIHandler : IUserInterface {
frmPackwizlauncher.dispose()
}
override fun handleException(e: Exception) {
e.printStackTrace()
EventQueue.invokeAndWait {
JOptionPane.showMessageDialog(null,
"An error occurred: \n" + e.javaClass.canonicalName + ": " + e.message,
override fun showErrorAndExit(message: String, e: Exception?): Nothing {
if (e != null) {
Log.fatal(message, e)
EventQueue.invokeAndWait {
JOptionPane.showMessageDialog(null,
"$message: $e",
title, JOptionPane.ERROR_MESSAGE)
}
}
override fun handleExceptionAndExit(e: Exception) {
e.printStackTrace()
EventQueue.invokeAndWait {
JOptionPane.showMessageDialog(null,
"A fatal error occurred: \n" + e.javaClass.canonicalName + ": " + e.message,
}
} else {
Log.fatal(message)
EventQueue.invokeAndWait {
JOptionPane.showMessageDialog(null,
message,
title, JOptionPane.ERROR_MESSAGE)
exitProcess(1)
}
}
exitProcess(1)
}
override fun setTitle(title: String) {
@@ -78,8 +78,7 @@ class GUIHandler : IUserInterface {
sb.append(") ")
}
sb.append(progress.message)
// TODO: better logging library?
println(sb.toString())
Log.info(sb.toString())
EventQueue.invokeLater {
frmPackwizlauncher.displayProgress(progress)
}