[Gradle] Unify a bit logic of compiler execution exceptions wrapping

This commit is contained in:
Alexander.Likhachev
2023-10-18 01:23:30 +02:00
committed by Space Team
parent 65f6f6a07f
commit b1ec2eb18a
3 changed files with 28 additions and 28 deletions
@@ -246,13 +246,7 @@ internal class GradleKotlinCompilerWork @Inject constructor(
exitCodeFromProcessExitCode(log, res.get())
} catch (e: Throwable) {
bufferingMessageCollector.flush(messageCollector)
if (e is OutOfMemoryError || e.hasOOMCause()) {
throw OOMErrorException(kotlinDaemonOOMHelperMessage)
} else if (e is RemoteException) {
throw DaemonCrashedException(e)
} else {
throw e
}
wrapAndRethrowCompilationException(KotlinCompilerExecutionStrategy.DAEMON, e)
} finally {
val memoryUsageAfterBuild = runCatching { daemon.getUsedMemory(withGC = false).takeIf { it.isGood }?.get() }.getOrNull()
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.gradle.plugin.internal.BuildIdService
import org.jetbrains.kotlin.gradle.plugin.internal.state.TaskLoggers
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.tasks.FailedCompilationException
import org.jetbrains.kotlin.gradle.tasks.OOMErrorException
import org.jetbrains.kotlin.gradle.tasks.TaskOutputsBackup
import org.jetbrains.kotlin.incremental.ClasspathChanges
import org.slf4j.LoggerFactory
@@ -130,18 +129,7 @@ internal abstract class BuildToolsApiCompilationWork @Inject constructor(
workArguments.compilerArgs.toList(),
)
} catch (e: Throwable) {
if (e is OutOfMemoryError || e.hasOOMCause()) {
val helpMessage = when (executionStrategy) {
KotlinCompilerExecutionStrategy.DAEMON -> kotlinDaemonOOMHelperMessage
KotlinCompilerExecutionStrategy.IN_PROCESS -> kotlinInProcessOOMHelperMessage
else -> error("The \"$executionStrategy\" execution strategy is not supported by the Build Tools API")
}
throw OOMErrorException(helpMessage)
} else if (e is RemoteException) {
throw DaemonCrashedException(e)
} else {
throw e
}
wrapAndRethrowCompilationException(executionStrategy, e)
} finally {
log.info(executionStrategy.asFinishLogMessage)
}
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.gradle.logging.kotlinDebug
import org.jetbrains.kotlin.incremental.deleteDirectoryContents
import org.jetbrains.kotlin.incremental.deleteRecursivelyOrThrow
import java.io.File
import java.rmi.RemoteException
/** Throws [FailedCompilationException] if compilation completed with [exitCode] != [ExitCode.OK]. */
fun throwExceptionIfCompilationFailed(
@@ -20,14 +21,7 @@ fun throwExceptionIfCompilationFailed(
ExitCode.COMPILATION_ERROR -> throw CompilationErrorException("Compilation error. See log for more details")
ExitCode.INTERNAL_ERROR -> throw FailedCompilationException("Internal compiler error. See log for more details")
ExitCode.SCRIPT_EXECUTION_ERROR -> throw FailedCompilationException("Script execution error. See log for more details")
ExitCode.OOM_ERROR -> {
val exceptionMessage = when (executionStrategy) {
KotlinCompilerExecutionStrategy.DAEMON -> kotlinDaemonOOMHelperMessage
KotlinCompilerExecutionStrategy.IN_PROCESS -> kotlinInProcessOOMHelperMessage
KotlinCompilerExecutionStrategy.OUT_OF_PROCESS -> kotlinOutOfProcessOOMHelperMessage
}
throw OOMErrorException(exceptionMessage)
}
ExitCode.OOM_ERROR -> throw OOMErrorException(executionStrategy)
ExitCode.OK -> Unit
else -> throw IllegalStateException("Unexpected exit code: $exitCode")
}
@@ -49,6 +43,21 @@ internal fun Throwable.hasOOMCause(): Boolean = when (cause) {
else -> cause?.hasOOMCause() ?: false
}
/**
* Wraps an exception occurred during compiler execution.
* Covers the case when compiler invocation failed before returning any [ExitCode].
* Always throws some kind of exception.
*/
internal fun wrapAndRethrowCompilationException(executionStrategy: KotlinCompilerExecutionStrategy, e: Throwable): Nothing {
if (e is OutOfMemoryError || e.hasOOMCause()) {
throw OOMErrorException(executionStrategy)
} else if (e is RemoteException) {
throw DaemonCrashedException(e)
} else {
throw e
}
}
/** Exception thrown when [ExitCode] != [ExitCode.OK]. */
internal open class FailedCompilationException(message: String, cause: Throwable? = null) : RuntimeException(message, cause)
@@ -58,6 +67,15 @@ internal class CompilationErrorException(message: String) : FailedCompilationExc
/** Exception thrown when [ExitCode] == [ExitCode.OOM_ERROR]. */
internal class OOMErrorException(message: String) : FailedCompilationException(message)
private fun OOMErrorException(executionStrategy: KotlinCompilerExecutionStrategy): OOMErrorException {
val exceptionMessage = when (executionStrategy) {
KotlinCompilerExecutionStrategy.DAEMON -> kotlinDaemonOOMHelperMessage
KotlinCompilerExecutionStrategy.IN_PROCESS -> kotlinInProcessOOMHelperMessage
KotlinCompilerExecutionStrategy.OUT_OF_PROCESS -> kotlinOutOfProcessOOMHelperMessage
}
return OOMErrorException(exceptionMessage)
}
/** Exception thrown when during the compilation [java.rmi.RemoteException] is caught */
internal class DaemonCrashedException(cause: Throwable) : FailedCompilationException(kotlinDaemonCrashedMessage, cause)