diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-13 17:42:27 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-13 17:42:27 +0100 |
commit | c4e4985b8b96ed156851c6be6d4a8d5e110c3040 (patch) | |
tree | 4dc7d19ccff486a9f612738db7ea196beffb38d1 /src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt | |
parent | 4e986409ad65aa5c11baf7e8571b78b89ec260a4 (diff) | |
download | LocalTransactionLedger-c4e4985b8b96ed156851c6be6d4a8d5e110c3040.tar.gz LocalTransactionLedger-c4e4985b8b96ed156851c6be6d4a8d5e110c3040.tar.bz2 LocalTransactionLedger-c4e4985b8b96ed156851c6be6d4a8d5e110c3040.zip |
Add error reporting framework
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt b/src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt new file mode 100644 index 0000000..9b6a153 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/utils/ErrorUtil.kt @@ -0,0 +1,37 @@ +package moe.nea.ledger.utils + +import moe.nea.ledger.utils.telemetry.ContextValue +import moe.nea.ledger.utils.telemetry.EventRecorder +import moe.nea.ledger.utils.telemetry.Span + +class ErrorUtil { + + @Inject + lateinit var reporter: EventRecorder + + fun report(exception: Throwable, message: String?) { + Span.current().recordException(reporter, exception, message) + } + + fun <T> Result<T>.getOrReport(): T? { + val exc = exceptionOrNull() + if (exc != null) { + report(exc, null) + } + return getOrNull() + } + + inline fun <T> catch( + vararg pairs: Pair<String, ContextValue>, + crossinline function: () -> T + ): T? { + return Span.current().enterWith(*pairs) { + try { + return@enterWith function() + } catch (ex: Exception) { + report(ex, null) + return@enterWith null + } + } + } +}
\ No newline at end of file |