aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/ErrorUtil.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-11-06 16:14:10 +0100
committerLinnea Gräf <nea@nea.moe>2024-11-06 16:14:10 +0100
commitee21f2da76ea2218ef6a0c6fefa97befa654d115 (patch)
treeaeefb78518a83d37a025673f6cadaa7d89d87449 /src/main/kotlin/util/ErrorUtil.kt
parented3e9f0d70a758ac1c23c6c8d67eeb7105d13817 (diff)
downloadFirmament-ee21f2da76ea2218ef6a0c6fefa97befa654d115.tar.gz
Firmament-ee21f2da76ea2218ef6a0c6fefa97befa654d115.tar.bz2
Firmament-ee21f2da76ea2218ef6a0c6fefa97befa654d115.zip
Fix model unwrapping
Diffstat (limited to 'src/main/kotlin/util/ErrorUtil.kt')
-rw-r--r--src/main/kotlin/util/ErrorUtil.kt25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/main/kotlin/util/ErrorUtil.kt b/src/main/kotlin/util/ErrorUtil.kt
index afecf25..b06093b 100644
--- a/src/main/kotlin/util/ErrorUtil.kt
+++ b/src/main/kotlin/util/ErrorUtil.kt
@@ -1,25 +1,46 @@
+@file:OptIn(ExperimentalContracts::class)
+
package moe.nea.firmament.util
+import kotlin.contracts.ExperimentalContracts
+import kotlin.contracts.InvocationKind
+import kotlin.contracts.contract
import moe.nea.firmament.Firmament
+@Suppress("NOTHING_TO_INLINE") // Suppressed since i want the logger to not pick up the ErrorUtil stack-frame
object ErrorUtil {
var aggressiveErrors = run {
Thread.currentThread().stackTrace.any { it.className.startsWith("org.junit.") } || Firmament.DEBUG
+ || ErrorUtil::class.java.desiredAssertionStatus()
+ }
+
+ inline fun softCheck(message: String, check: Boolean) {
+ if (!check) softError(message)
}
- inline fun softCheck(message: String, func: () -> Boolean) {
+ inline fun lazyCheck(message: String, func: () -> Boolean) {
+ contract {
+ callsInPlace(func, InvocationKind.AT_MOST_ONCE)
+ }
if (!aggressiveErrors) return
if (func()) return
error(message)
}
- @Suppress("NOTHING_TO_INLINE") // Suppressed since i want the logger to not pick up the ErrorUtil stack-frame
+ inline fun softError(message: String, exception: Throwable) {
+ if (aggressiveErrors) throw IllegalStateException(message, exception)
+ else Firmament.logger.error(message, exception)
+ }
+
inline fun softError(message: String) {
if (aggressiveErrors) error(message)
else Firmament.logger.error(message)
}
inline fun <T : Any> notNullOr(nullable: T?, message: String, orElse: () -> T): T {
+ contract {
+ callsInPlace(orElse, InvocationKind.AT_MOST_ONCE)
+ }
if (nullable == null) {
softError(message)
return orElse()