Rework checkWithAttachment/requireWithAttachment

* better naming
* throw corresponding KotlinIllegalStateExceptionWithAttachments/KotlinIllegalArgumentExceptionWithAttachments instead of general KotlinRuntimeExceptionWithAttachments
This commit is contained in:
Ilya Kirillov
2023-06-30 15:56:26 +02:00
committed by Space Team
parent 2d791eb292
commit 4b3bff3344
12 changed files with 40 additions and 34 deletions
@@ -91,27 +91,33 @@ inline fun rethrowExceptionWithDetails(
@OptIn(ExperimentalContracts::class)
inline fun checkWithAttachmentBuilder(
inline fun checkWithAttachment(
condition: Boolean,
message: () -> String,
attachmentName: String = "info.txt",
buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {},
) {
contract { returns() implies (condition) }
if (!condition) {
errorWithAttachment(message(), buildAttachment = buildAttachment)
val exception = KotlinIllegalStateExceptionWithAttachments(message())
exception.buildAttachment(attachmentName) { buildAttachment() }
throw exception
}
}
@OptIn(ExperimentalContracts::class)
inline fun requireWithAttachmentBuilder(
inline fun requireWithAttachment(
condition: Boolean,
message: () -> String,
attachmentName: String = "info.txt",
buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {},
) {
contract { returns() implies (condition) }
if (!condition) {
errorWithAttachment(message(), buildAttachment = buildAttachment)
val exception = KotlinIllegalArgumentExceptionWithAttachments(message())
exception.buildAttachment(attachmentName) { buildAttachment() }
throw exception
}
}