Simplify copying attachment in KotlinExceptionWithAttachments

This commit is contained in:
Ilya Kirillov
2022-08-02 17:53:06 +02:00
parent 72da40f97c
commit ddbd89aea7
@@ -18,18 +18,20 @@ open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttac
constructor(message: String?, cause: Throwable?) : super(message, cause) {
if (cause is KotlinExceptionWithAttachments) {
attachments.addAll(
cause.attachments.map { a ->
val content = a.openContentStream().use { it.reader(StandardCharsets.UTF_8).use { it.readText() } }
Attachment("case_" + a.path, content)
}
)
cause.attachments.mapTo(attachments) { attachment ->
attachment.copyWithNewName("case_${attachment.path}")
}
}
if (cause != null) {
withAttachment("causeThrowable", cause.stackTraceToString())
}
}
private fun Attachment.copyWithNewName(newName: String): Attachment {
val content = String(bytes, StandardCharsets.UTF_8)
return Attachment(newName, content)
}
override fun getAttachments(): Array<Attachment> = attachments.toTypedArray()
fun withAttachment(name: String, content: Any?): KotlinExceptionWithAttachments {
@@ -39,7 +41,10 @@ open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttac
}
fun KotlinExceptionWithAttachments.withAttachmentBuilder(name: String, buildContent: StringBuilder.() -> Unit): KotlinExceptionWithAttachments {
fun KotlinExceptionWithAttachments.withAttachmentBuilder(
name: String,
buildContent: StringBuilder.() -> Unit
): KotlinExceptionWithAttachments {
return withAttachment(name, buildString { buildContent() })
}