Extract a base class from KotlinExceptionWithAttachments
This commit is contained in:
committed by
Space Team
parent
15f6fe2627
commit
52c065b08a
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.name.Name
|
|||||||
import org.jetbrains.kotlin.psi.psiUtil.isIdentifier
|
import org.jetbrains.kotlin.psi.psiUtil.isIdentifier
|
||||||
import org.jetbrains.kotlin.resolve.ImportPath
|
import org.jetbrains.kotlin.resolve.ImportPath
|
||||||
import org.jetbrains.kotlin.utils.checkWithAttachment
|
import org.jetbrains.kotlin.utils.checkWithAttachment
|
||||||
|
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
|
||||||
|
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
@JvmName("KtPsiFactory")
|
@JvmName("KtPsiFactory")
|
||||||
|
|||||||
@@ -7,38 +7,23 @@ package org.jetbrains.kotlin.utils
|
|||||||
|
|
||||||
import com.intellij.openapi.application.ApplicationManager
|
import com.intellij.openapi.application.ApplicationManager
|
||||||
import com.intellij.openapi.diagnostic.Attachment
|
import com.intellij.openapi.diagnostic.Attachment
|
||||||
import com.intellij.openapi.diagnostic.ExceptionWithAttachments
|
import com.intellij.psi.PsiElement
|
||||||
import java.nio.charset.StandardCharsets
|
import org.jetbrains.kotlin.utils.exceptions.KotlinExceptionWithAttachments as KotlinExceptionWithAttachmentsBase
|
||||||
import com.intellij.psi.*
|
import org.jetbrains.kotlin.utils.exceptions.KotlinExceptionWithAttachments.Companion.withAttachmentsFrom
|
||||||
import kotlin.contracts.ExperimentalContracts
|
import kotlin.contracts.ExperimentalContracts
|
||||||
import kotlin.contracts.contract
|
import kotlin.contracts.contract
|
||||||
|
|
||||||
open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttachments {
|
open class KotlinExceptionWithAttachments : RuntimeException, KotlinExceptionWithAttachmentsBase {
|
||||||
private val attachments = mutableListOf<Attachment>()
|
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) : super(message)
|
||||||
|
|
||||||
constructor(message: String?, cause: Throwable?) : super(message, cause) {
|
constructor(message: String?, cause: Throwable?) : super(message, cause) {
|
||||||
if (cause is KotlinExceptionWithAttachments) {
|
withAttachmentsFrom(cause)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun withPsiAttachment(name: String, element: PsiElement?): KotlinExceptionWithAttachments {
|
fun withPsiAttachment(name: String, element: PsiElement?): KotlinExceptionWithAttachments {
|
||||||
@@ -50,7 +35,7 @@ open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttac
|
|||||||
|
|
||||||
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
@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) }
|
contract { returns() implies (value) }
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
|
|||||||
+39
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user