aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/ErrorUtil.kt
blob: b06093baffd8ca1a1bd359baf76f6b830eb32cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@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 lazyCheck(message: String, func: () -> Boolean) {
		contract {
			callsInPlace(func, InvocationKind.AT_MOST_ONCE)
		}
		if (!aggressiveErrors) return
		if (func()) return
		error(message)
	}

	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()
		}
		return nullable
	}

}