[Analysis API FIR] add more info to some exceptions

This commit is contained in:
Ilya Kirillov
2022-07-23 19:50:08 +02:00
parent 5a5e6ad8cd
commit 76f1744868
11 changed files with 81 additions and 38 deletions
@@ -5,7 +5,12 @@
package org.jetbrains.kotlin.analysis.utils.errors
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.analysis.utils.printer.getElementTextInContext
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.errorWithAttachment
import org.jetbrains.kotlin.utils.withAttachmentDetailed
public fun unexpectedElementError(elementName: String, element: Any?): Nothing {
errorWithAttachment("Unexpected $elementName ${element?.let { it::class.simpleName }}") {
@@ -15,4 +20,14 @@ public fun unexpectedElementError(elementName: String, element: Any?): Nothing {
public inline fun <reified ELEMENT> unexpectedElementError(element: Any?): Nothing {
unexpectedElementError(ELEMENT::class.simpleName ?: ELEMENT::class.java.name, element)
}
public fun KotlinExceptionWithAttachments.withPsiAttachment(name: String, psi: PsiElement?): KotlinExceptionWithAttachments {
withAttachmentDetailed(name, psi) { psiElement ->
when (psiElement) {
is KtElement -> psiElement.getElementTextInContext()
else -> psiElement.text
}
}
return this
}