Simplify copying attachment in KotlinExceptionWithAttachments
This commit is contained in:
@@ -18,18 +18,20 @@ open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttac
|
|||||||
|
|
||||||
constructor(message: String?, cause: Throwable?) : super(message, cause) {
|
constructor(message: String?, cause: Throwable?) : super(message, cause) {
|
||||||
if (cause is KotlinExceptionWithAttachments) {
|
if (cause is KotlinExceptionWithAttachments) {
|
||||||
attachments.addAll(
|
cause.attachments.mapTo(attachments) { attachment ->
|
||||||
cause.attachments.map { a ->
|
attachment.copyWithNewName("case_${attachment.path}")
|
||||||
val content = a.openContentStream().use { it.reader(StandardCharsets.UTF_8).use { it.readText() } }
|
}
|
||||||
Attachment("case_" + a.path, content)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
if (cause != null) {
|
if (cause != null) {
|
||||||
withAttachment("causeThrowable", cause.stackTraceToString())
|
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()
|
override fun getAttachments(): Array<Attachment> = attachments.toTypedArray()
|
||||||
|
|
||||||
fun withAttachment(name: String, content: Any?): KotlinExceptionWithAttachments {
|
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() })
|
return withAttachment(name, buildString { buildContent() })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user