Extract a base class from KotlinExceptionWithAttachments

This commit is contained in:
Ilya Kirillov
2023-06-30 15:47:28 +02:00
committed by Space Team
parent 15f6fe2627
commit 52c065b08a
3 changed files with 51 additions and 26 deletions
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.psiUtil.isIdentifier
import org.jetbrains.kotlin.resolve.ImportPath
import org.jetbrains.kotlin.utils.checkWithAttachment
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
@JvmOverloads
@JvmName("KtPsiFactory")
@@ -7,38 +7,23 @@ package org.jetbrains.kotlin.utils
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Attachment
import com.intellij.openapi.diagnostic.ExceptionWithAttachments
import java.nio.charset.StandardCharsets
import com.intellij.psi.*
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.utils.exceptions.KotlinExceptionWithAttachments as KotlinExceptionWithAttachmentsBase
import org.jetbrains.kotlin.utils.exceptions.KotlinExceptionWithAttachments.Companion.withAttachmentsFrom
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttachments {
private val attachments = mutableListOf<Attachment>()
open class KotlinExceptionWithAttachments : RuntimeException, KotlinExceptionWithAttachmentsBase {
override val mutableAttachments = mutableListOf<Attachment>()
override fun withAttachment(name: String, content: Any?): KotlinExceptionWithAttachments {
return super.withAttachment(name, content) as KotlinExceptionWithAttachments
}
constructor(message: String) : super(message)
constructor(message: String?, cause: Throwable?) : super(message, cause) {
if (cause is KotlinExceptionWithAttachments) {
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 {
attachments.add(Attachment(name, content?.toString() ?: "<null>"))
return this
withAttachmentsFrom(cause)
}
fun withPsiAttachment(name: String, element: PsiElement?): KotlinExceptionWithAttachments {
@@ -50,7 +35,7 @@ open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttac
@OptIn(ExperimentalContracts::class)
inline fun checkWithAttachment(value: Boolean, lazyMessage: () -> String, attachments: (KotlinExceptionWithAttachments) -> Unit = {}) {
inline fun checkWithAttachment(value: Boolean, lazyMessage: () -> String, attachments: (org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments) -> Unit = {}) {
contract { returns() implies (value) }
if (!value) {
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.utils.exceptions
import com.intellij.openapi.diagnostic.Attachment
import com.intellij.openapi.diagnostic.ExceptionWithAttachments
import java.nio.charset.StandardCharsets
interface KotlinExceptionWithAttachments : ExceptionWithAttachments {
val mutableAttachments: MutableList<Attachment>
override fun getAttachments(): Array<Attachment> = mutableAttachments.toTypedArray()
fun withAttachment(name: String, content: Any?): KotlinExceptionWithAttachments {
mutableAttachments.add(Attachment(name, content?.toString() ?: "<null>"))
return this
}
companion object {
internal fun KotlinExceptionWithAttachments.withAttachmentsFrom(from: Throwable?) {
if (from is KotlinExceptionWithAttachments) {
from.mutableAttachments.mapTo(mutableAttachments) { attachment ->
attachment.copyWithNewName("case_${attachment.path}")
}
}
if (from != null) {
withAttachment("causeThrowable", from.stackTraceToString())
}
}
private fun Attachment.copyWithNewName(newName: String): Attachment {
val content = String(bytes, StandardCharsets.UTF_8)
return Attachment(newName, content)
}
}
}