Refactor UncivShowableException (#6899)

This commit is contained in:
SomeTroglodyte
2022-05-22 13:55:19 +02:00
committed by GitHub
parent 3754108391
commit 740886c890
5 changed files with 59 additions and 34 deletions

View File

@ -1437,6 +1437,12 @@ Invisible to others =
Bison =
Cocoa =
# Exceptions that _may_ be shown to the user
Building '[buildingName]' is buildable and therefore must either have an explicit cost or reference an existing tech. =
Nation [nationName] is not found! =
Unit [unitName] doesn't seem to exist! =
# In English we just paste all these conditionals at the end of each unique, but in your language that
# may not turn into valid sentences. Therefore we have the following two translations to determine
@ -1467,5 +1473,3 @@ ConditionalsPlacement =
########################### AUTOMATICALLY GENERATED TRANSLATABLE STRINGS ###########################

View File

@ -22,9 +22,6 @@ import com.unciv.ui.audio.MusicTrackChooserFlags
import java.util.*
class MissingModsException(val missingMods: String) : UncivShowableException("Missing mods: [$missingMods]")
open class UncivShowableException(errorText: String) : Exception(errorText)
class GameInfo {
//region Fields - Serialized
var civilizations = mutableListOf<CivilizationInfo>()

View File

@ -0,0 +1,24 @@
package com.unciv.logic
import com.unciv.models.translations.tr
/**
* An [Exception] wrapper marking an Exception as suitable to be shown to the user.
*
* @param [errorText] should be the _**untranslated**_ error message.
* Use [getLocalizedMessage] to get the translated [message], _**or**_ use auto-translating helpers like .toLabel() or FormattedLine().
* Usual formatting (`[] or {}`) applies, as does the need to include the text in templates.properties.
*/
open class UncivShowableException(
errorText: String,
override val cause: Throwable? = null
) : Exception(errorText) {
// override because we _definitely_ have a non-null message from [errorText]
override val message: String
get() = super.message!!
override fun getLocalizedMessage() = message.tr()
}
class MissingModsException(
val missingMods: String
) : UncivShowableException("Missing mods: [$missingMods]")

View File

@ -355,7 +355,7 @@ class Ruleset {
for (building in buildings.values) {
if (building.cost == 0 && !building.hasUnique(UniqueType.Unbuildable)) {
val column = technologies[building.requiredTech]?.column
?: throw UncivShowableException("Building (${building.name}) is buildable and therefore must either have an explicit cost or reference an existing tech")
?: throw UncivShowableException("Building '[${building.name}]' is buildable and therefore must either have an explicit cost or reference an existing tech.")
building.cost = if (building.isAnyWonder()) column.wonderCost else column.buildingCost
}
}
@ -982,7 +982,7 @@ object RulesetCache : HashMap<String,Ruleset>() {
// This happens if a building is dependent on a tech not in the base ruleset
// because newRuleset.updateBuildingCosts() in getComplexRuleset() throws an error
Ruleset.RulesetErrorList()
.apply { add(ex.message!!.tr(), Ruleset.RulesetErrorSeverity.Error) }
.apply { add(ex.message, Ruleset.RulesetErrorSeverity.Error) }
}
}

View File

@ -122,7 +122,7 @@ class MapEditorLoadTab(
val rulesetIncompatibilities = map.getRulesetIncompatibility(ruleset)
if (rulesetIncompatibilities.isNotEmpty()) {
map.removeMissingTerrainModReferences(ruleset)
val message = "{This map has errors:}\n\n".tr() +
val message = "{This map has errors:}\n\n" +
rulesetIncompatibilities.sorted().joinToString("\n") { it.tr() } +
"\n\n{The incompatible elements have been removed.}"
ToastPopup(message, editorScreen, 4000L)
@ -134,7 +134,7 @@ class MapEditorLoadTab(
} catch (ex: Throwable) {
needPopup = false
popup?.close()
println("Error displaying map \"$chosenMap\": ${ex.localizedMessage}")
println("Error displaying map \"$chosenMap\": ${ex.message}")
Gdx.input.inputProcessor = editorScreen.stage
ToastPopup("Error loading map!", editorScreen)
}
@ -143,9 +143,9 @@ class MapEditorLoadTab(
needPopup = false
Gdx.app.postRunnable {
popup?.close()
println("Error loading map \"$chosenMap\": ${ex.localizedMessage}")
ToastPopup("Error loading map!".tr() +
(if (ex is UncivShowableException) "\n" + ex.message else ""), editorScreen)
println("Error loading map \"$chosenMap\": ${ex.message}")
ToastPopup("{Error loading map!}" +
(if (ex is UncivShowableException) "\n{${ex.message}}" else ""), editorScreen)
}
}
}