From 59566bcab8a5861e0cb78e8c207884006828bd63 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Wed, 13 Nov 2024 15:44:04 +0100 Subject: fix: Improve exception logging for entity widgets --- src/main/kotlin/util/ErrorUtil.kt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/main/kotlin/util/ErrorUtil.kt') 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 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 fail(exception: Throwable): Catch = Catch(null, exception) + fun succeed(value: T): Catch = Catch(value, null) + } + } + + inline fun catch(message: String, block: () -> T): Catch { + try { + return Catch.succeed(block()) + } catch (exc: Throwable) { + softError(message, exc) + return Catch.fail(exc) + } + } + inline fun notNullOr(nullable: T?, message: String, orElse: () -> T): T { contract { callsInPlace(orElse, InvocationKind.AT_MOST_ONCE) -- cgit