diff options
author | Linnea Gräf <nea@nea.moe> | 2024-11-13 15:44:04 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-11-15 18:40:19 +0100 |
commit | 59566bcab8a5861e0cb78e8c207884006828bd63 (patch) | |
tree | 4ce3b19b3ab1f78652d5d805d8b7f2def16a1167 /src/main/kotlin/util | |
parent | 57cdcb21ec3aa6f4d4f4ab07a3e4e8fd7d32f28d (diff) | |
download | Firmament-59566bcab8a5861e0cb78e8c207884006828bd63.tar.gz Firmament-59566bcab8a5861e0cb78e8c207884006828bd63.tar.bz2 Firmament-59566bcab8a5861e0cb78e8c207884006828bd63.zip |
fix: Improve exception logging for entity widgets
Diffstat (limited to 'src/main/kotlin/util')
-rw-r--r-- | src/main/kotlin/util/ErrorUtil.kt | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/main/kotlin/util/ErrorUtil.kt b/src/main/kotlin/util/ErrorUtil.kt index 5dc44d3..190381d 100644 --- a/src/main/kotlin/util/ErrorUtil.kt +++ b/src/main/kotlin/util/ErrorUtil.kt @@ -37,6 +37,31 @@ object ErrorUtil { else Firmament.logger.error(message) } + class Catch<T> private constructor(val value: T?, val exc: Throwable?) { + inline fun or(block: (exc: Throwable) -> T): T { + contract { + callsInPlace(block, InvocationKind.AT_MOST_ONCE) + } + if (exc != null) return block(exc) + @Suppress("UNCHECKED_CAST") + return value as T + } + + companion object { + fun <T> fail(exception: Throwable): Catch<T> = Catch(null, exception) + fun <T> succeed(value: T): Catch<T> = Catch(value, null) + } + } + + inline fun <T> catch(message: String, block: () -> T): Catch<T> { + try { + return Catch.succeed(block()) + } catch (exc: Throwable) { + softError(message, exc) + return Catch.fail(exc) + } + } + inline fun <T : Any> notNullOr(nullable: T?, message: String, orElse: () -> T): T { contract { callsInPlace(orElse, InvocationKind.AT_MOST_ONCE) |