aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/ErrorUtil.kt
blob: afecf256fc081cfc4e23a37c03e7ceb2f4532071 (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
package moe.nea.firmament.util

import moe.nea.firmament.Firmament

object ErrorUtil {
	var aggressiveErrors = run {
		Thread.currentThread().stackTrace.any { it.className.startsWith("org.junit.") } || Firmament.DEBUG
	}

	inline fun softCheck(message: String, func: () -> Boolean) {
		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) {
		if (aggressiveErrors) error(message)
		else Firmament.logger.error(message)
	}

	inline fun <T : Any> notNullOr(nullable: T?, message: String, orElse: () -> T): T {
		if (nullable == null) {
			softError(message)
			return orElse()
		}
		return nullable
	}

}