Skip to content

Commit d5df08e

Browse files
dimonchik0036Space Team
authored and
Space Team
committed
[LL FIR] LLFirDiagnosticVisitor: prefer logging to runtime exceptions
It's better to log errors than throw them right away, as in this case we won't interrupt other checkers. It will help the IntelliJ plugin to not break the entire highlighting. ^KT-68021 Fixed
1 parent 3165f51 commit d5df08e

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostics/LLFirDiagnosticVisitor.kt

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostics
77

8+
import com.intellij.openapi.diagnostic.Logger
89
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getFirResolveSession
910
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.resolveToFirSymbol
1011
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.checkCanceled
@@ -23,6 +24,7 @@ import org.jetbrains.kotlin.fir.declarations.FirScript
2324
import org.jetbrains.kotlin.fir.psi
2425
import org.jetbrains.kotlin.psi.KtCodeFragment
2526
import org.jetbrains.kotlin.psi.KtDeclaration
27+
import org.jetbrains.kotlin.utils.exceptions.shouldIjPlatformExceptionBeRethrown
2628

2729
internal open class LLFirDiagnosticVisitor(
2830
context: CheckerContextForProvider,
@@ -40,15 +42,19 @@ internal open class LLFirDiagnosticVisitor(
4042

4143
override fun checkElement(element: FirElement) {
4244
beforeElementDiagnosticCollectionHandler?.beforeCollectingForElement(element)
43-
components.regularComponents.forEach {
45+
components.regularComponents.forEach { diagnosticVisitor ->
4446
checkCanceled()
45-
element.accept(it, context)
47+
suppressAndLogExceptions {
48+
element.accept(diagnosticVisitor, context)
49+
}
4650
}
4751

4852
checkCanceled()
49-
element.accept(components.reportCommitter, context)
53+
suppressAndLogExceptions {
54+
element.accept(components.reportCommitter, context)
5055

51-
commitPendingDiagnosticsOnNestedDeclarations(element)
56+
commitPendingDiagnosticsOnNestedDeclarations(element)
57+
}
5258
}
5359

5460
override fun visitCodeFragment(codeFragment: FirCodeFragment, data: Nothing?) {
@@ -118,3 +124,24 @@ internal open class LLFirDiagnosticVisitor(
118124
}
119125
}
120126
}
127+
128+
private val logger: Logger = Logger.getInstance(LLFirDiagnosticVisitor::class.java)
129+
130+
/**
131+
* We don't want to throw exceptions right away to not interrupt other checkers there possible.
132+
* It is better to report as much as possible and not crash the entire visitor.
133+
*
134+
* By default [logger] throws exceptions, but it is up to the user code to provide
135+
* alternative handler.
136+
*
137+
* For instance, the IntelliJ plugin reports such exceptions and doesn't interrupt the execution flow.
138+
*/
139+
private inline fun suppressAndLogExceptions(block: () -> Unit) {
140+
try {
141+
block()
142+
} catch (e: Throwable) {
143+
if (shouldIjPlatformExceptionBeRethrown(e)) throw e
144+
145+
logger.error(e)
146+
}
147+
}

0 commit comments

Comments
 (0)