Migrate some FIR/Analysis API KotlinExceptionWithAttachment usages to buildErrorWithAttachment API

This commit is contained in:
Ilya Kirillov
2023-07-11 15:11:02 +02:00
committed by Space Team
parent 24f60a542d
commit 24db4e62f5
6 changed files with 49 additions and 59 deletions
@@ -12,9 +12,9 @@ import com.intellij.psi.impl.source.resolve.ResolveCache
import org.jetbrains.kotlin.analysis.api.KtAllowAnalysisOnEdt
import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.analysis.api.lifetime.allowAnalysisOnEdt
import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment
import org.jetbrains.kotlin.utils.exceptions.shouldIjPlatformExceptionBeRethrown
import org.jetbrains.kotlin.utils.exceptions.withPsiEntry
object KtFirReferenceResolver : ResolveCache.PolyVariantResolver<KtReference> {
class KotlinResolveResult(element: PsiElement) : PsiElementResolveResult(element)
@@ -29,18 +29,11 @@ object KtFirReferenceResolver : ResolveCache.PolyVariantResolver<KtReference> {
} catch (e: Exception) {
if (shouldIjPlatformExceptionBeRethrown(e)) throw e
throw KtReferenceResolveException(ref, e)
errorWithAttachment("Unable to resolve reference ${ref.element::class.java}", cause = e) {
withPsiEntry("reference", ref.element)
}
}
resolveToPsiElements.map { KotlinResolveResult(it) }.toTypedArray()
}
}
}
class KtReferenceResolveException(
reference: KtReference,
cause: Throwable
) : KotlinExceptionWithAttachments("Unable to resolve reference at: ${PsiDiagnosticUtils.atLocation(reference.element)}", cause) {
init {
withPsiAttachment("element.kt", reference.element)
}
}
@@ -5,14 +5,14 @@
package org.jetbrains.kotlin.analysis.low.level.api.fir.api
import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry
import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry
import org.jetbrains.kotlin.fir.utils.exceptions.withFirSymbolEntry
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry
import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry
import org.jetbrains.kotlin.fir.utils.exceptions.withFirSymbolEntry
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.exceptions.KotlinIllegalArgumentExceptionWithAttachments
import org.jetbrains.kotlin.utils.exceptions.buildAttachment
import java.util.*
import kotlin.reflect.KClass
@@ -21,7 +21,7 @@ class InvalidFirElementTypeException(
actualFirElement: Any?,
ktElement: KtElement?,
expectedFirClasses: List<KClass<*>>,
) : KotlinExceptionWithAttachments("") {
) : KotlinIllegalArgumentExceptionWithAttachments("") {
init {
buildAttachment {
when (actualFirElement) {
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
import org.jetbrains.kotlin.fir.symbols.*
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.declarations.UNDEFINED_PARAMETER_INDEX
@@ -58,8 +59,8 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.NameUtils
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.addToStdlib.runUnless
import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment
import org.jetbrains.kotlin.utils.threadLocal
import java.util.concurrent.ConcurrentHashMap
@@ -1783,8 +1784,10 @@ class Fir2IrDeclarationStorage(
private inline fun <R> convertCatching(element: FirElement, block: () -> R): R {
try {
return block()
} catch (e: Throwable) {
throw KotlinExceptionWithAttachments("Exception was thrown during transformation of ${element.render()}", e)
} catch (e: Exception) {
errorWithAttachment("Exception was thrown during transformation of ${element::class.java}", cause = e) {
withFirEntry("element", element)
}
}
}
}
@@ -5,33 +5,20 @@
package org.jetbrains.kotlin.fir.symbols.impl
import kotlin.reflect.KClass
import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.declarations.FirResolvedDeclarationStatus
import org.jetbrains.kotlin.fir.psi
import org.jetbrains.kotlin.fir.renderer.FirDeclarationRendererWithAttributes
import org.jetbrains.kotlin.fir.renderer.FirFileAnnotationsContainerRenderer
import org.jetbrains.kotlin.fir.renderer.FirRenderer
import org.jetbrains.kotlin.fir.renderer.FirResolvePhaseRenderer
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry
import org.jetbrains.kotlin.fir.utils.exceptions.withFirSymbolIdEntry
import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment
import kotlin.reflect.KClass
internal fun FirBasedSymbol<*>.errorInLazyResolve(name: String, actualClass: KClass<*>, expected: KClass<*>): Nothing {
throw KotlinExceptionWithAttachments("Unexpected $name. Expected is ${expected.simpleName}, but was ${actualClass.simpleName}").apply {
withAttachment(
"FirElement.txt",
FirRenderer(
resolvePhaseRenderer = FirResolvePhaseRenderer(),
declarationRenderer = FirDeclarationRendererWithAttributes(),
fileAnnotationsContainerRenderer = FirFileAnnotationsContainerRenderer(),
).renderElementAsString(fir),
)
withAttachment("FirBasedSymbol.txt", this@errorInLazyResolve::class.simpleName)
withAttachment("KtSourceElementKind.txt", fir.source?.kind?.let { it::class.simpleName })
withPsiAttachment("PsiElement.txt", fir.psi)
errorWithAttachment("Unexpected $name. Expected is ${expected.simpleName}, but was ${actualClass.simpleName}") {
withFirEntry("firElement", fir)
withFirSymbolIdEntry("firSymbol", this@errorInLazyResolve)
}
}
@@ -21,7 +21,8 @@ import com.intellij.openapi.diagnostic.Logger
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
import org.jetbrains.kotlin.psi.stubs.elements.KtTokenSets.INSIDE_DIRECTIVE_EXPRESSIONS
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.exceptions.logErrorWithAttachment
import org.jetbrains.kotlin.utils.exceptions.withPsiEntry
class KtDotQualifiedExpression : KtExpressionImplStub<KotlinPlaceHolderStub<KtDotQualifiedExpression>>, KtQualifiedExpression {
constructor(node: ASTNode) : super(node)
@@ -69,11 +70,9 @@ class KtDotQualifiedExpression : KtExpressionImplStub<KotlinPlaceHolderStub<KtDo
} else {
val expressions = stub.getChildrenByType(INSIDE_DIRECTIVE_EXPRESSIONS, KtExpression.ARRAY_FACTORY)
if (expressions.size !in 1..2) {
LOG.error(
KotlinExceptionWithAttachments(
"Invalid stub structure. DOT_QUALIFIED_EXPRESSION must have one or two children. Was: ${expressions.size}\n"
).withPsiAttachment("file.kt", containingFile)
)
LOG.logErrorWithAttachment("Invalid stub structure. DOT_QUALIFIED_EXPRESSION must have one or two children. Was: ${expressions.size}") {
withPsiEntry("file", containingFile)
}
return null
}
return expressions
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.utils.exceptions
import com.intellij.openapi.diagnostic.Attachment
import com.intellij.openapi.diagnostic.Logger
import org.jetbrains.kotlin.utils.SmartPrinter
import org.jetbrains.kotlin.utils.withIndent
@@ -58,16 +57,6 @@ inline fun KotlinExceptionWithAttachments.buildAttachment(
return withAttachment(name, ExceptionAttachmentBuilder().apply(buildContent).buildString())
}
inline fun errorWithAttachment(
message: String,
cause: Exception? = null,
attachmentName: String = "info.txt",
buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {},
): Nothing {
val exception = KotlinIllegalArgumentExceptionWithAttachments(message, cause)
exception.buildAttachment(attachmentName) { buildAttachment() }
throw exception
}
inline fun Logger.logErrorWithAttachment(
message: String,
@@ -75,8 +64,27 @@ inline fun Logger.logErrorWithAttachment(
attachmentName: String = "info.txt",
buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {},
) {
val attachment = Attachment(attachmentName, ExceptionAttachmentBuilder().apply(buildAttachment).buildString())
this.error(message, cause, attachment)
this.error(buildErrorWithAttachment(message, cause, attachmentName, buildAttachment))
}
inline fun buildErrorWithAttachment(
message: String,
cause: Exception? = null,
attachmentName: String = "info.txt",
buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {},
): Throwable {
val exception = KotlinIllegalArgumentExceptionWithAttachments(message, cause)
exception.buildAttachment(attachmentName) { buildAttachment() }
return exception
}
inline fun errorWithAttachment(
message: String,
cause: Exception? = null,
attachmentName: String = "info.txt",
buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {},
): Nothing {
throw buildErrorWithAttachment(message, cause, attachmentName, buildAttachment)
}
inline fun rethrowExceptionWithDetails(