[Gradle] Unify a bit logic of compiler execution exceptions wrapping
This commit is contained in:
committed by
Space Team
parent
65f6f6a07f
commit
b1ec2eb18a
+1
-7
@@ -246,13 +246,7 @@ internal class GradleKotlinCompilerWork @Inject constructor(
|
|||||||
exitCodeFromProcessExitCode(log, res.get())
|
exitCodeFromProcessExitCode(log, res.get())
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
bufferingMessageCollector.flush(messageCollector)
|
bufferingMessageCollector.flush(messageCollector)
|
||||||
if (e is OutOfMemoryError || e.hasOOMCause()) {
|
wrapAndRethrowCompilationException(KotlinCompilerExecutionStrategy.DAEMON, e)
|
||||||
throw OOMErrorException(kotlinDaemonOOMHelperMessage)
|
|
||||||
} else if (e is RemoteException) {
|
|
||||||
throw DaemonCrashedException(e)
|
|
||||||
} else {
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
val memoryUsageAfterBuild = runCatching { daemon.getUsedMemory(withGC = false).takeIf { it.isGood }?.get() }.getOrNull()
|
val memoryUsageAfterBuild = runCatching { daemon.getUsedMemory(withGC = false).takeIf { it.isGood }?.get() }.getOrNull()
|
||||||
|
|
||||||
|
|||||||
+1
-13
@@ -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.plugin.internal.state.TaskLoggers
|
||||||
import org.jetbrains.kotlin.gradle.tasks.*
|
import org.jetbrains.kotlin.gradle.tasks.*
|
||||||
import org.jetbrains.kotlin.gradle.tasks.FailedCompilationException
|
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.gradle.tasks.TaskOutputsBackup
|
||||||
import org.jetbrains.kotlin.incremental.ClasspathChanges
|
import org.jetbrains.kotlin.incremental.ClasspathChanges
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
@@ -130,18 +129,7 @@ internal abstract class BuildToolsApiCompilationWork @Inject constructor(
|
|||||||
workArguments.compilerArgs.toList(),
|
workArguments.compilerArgs.toList(),
|
||||||
)
|
)
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
if (e is OutOfMemoryError || e.hasOOMCause()) {
|
wrapAndRethrowCompilationException(executionStrategy, e)
|
||||||
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
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
log.info(executionStrategy.asFinishLogMessage)
|
log.info(executionStrategy.asFinishLogMessage)
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-8
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.gradle.logging.kotlinDebug
|
|||||||
import org.jetbrains.kotlin.incremental.deleteDirectoryContents
|
import org.jetbrains.kotlin.incremental.deleteDirectoryContents
|
||||||
import org.jetbrains.kotlin.incremental.deleteRecursivelyOrThrow
|
import org.jetbrains.kotlin.incremental.deleteRecursivelyOrThrow
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.rmi.RemoteException
|
||||||
|
|
||||||
/** Throws [FailedCompilationException] if compilation completed with [exitCode] != [ExitCode.OK]. */
|
/** Throws [FailedCompilationException] if compilation completed with [exitCode] != [ExitCode.OK]. */
|
||||||
fun throwExceptionIfCompilationFailed(
|
fun throwExceptionIfCompilationFailed(
|
||||||
@@ -20,14 +21,7 @@ fun throwExceptionIfCompilationFailed(
|
|||||||
ExitCode.COMPILATION_ERROR -> throw CompilationErrorException("Compilation error. See log for more details")
|
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.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.SCRIPT_EXECUTION_ERROR -> throw FailedCompilationException("Script execution error. See log for more details")
|
||||||
ExitCode.OOM_ERROR -> {
|
ExitCode.OOM_ERROR -> throw OOMErrorException(executionStrategy)
|
||||||
val exceptionMessage = when (executionStrategy) {
|
|
||||||
KotlinCompilerExecutionStrategy.DAEMON -> kotlinDaemonOOMHelperMessage
|
|
||||||
KotlinCompilerExecutionStrategy.IN_PROCESS -> kotlinInProcessOOMHelperMessage
|
|
||||||
KotlinCompilerExecutionStrategy.OUT_OF_PROCESS -> kotlinOutOfProcessOOMHelperMessage
|
|
||||||
}
|
|
||||||
throw OOMErrorException(exceptionMessage)
|
|
||||||
}
|
|
||||||
ExitCode.OK -> Unit
|
ExitCode.OK -> Unit
|
||||||
else -> throw IllegalStateException("Unexpected exit code: $exitCode")
|
else -> throw IllegalStateException("Unexpected exit code: $exitCode")
|
||||||
}
|
}
|
||||||
@@ -49,6 +43,21 @@ internal fun Throwable.hasOOMCause(): Boolean = when (cause) {
|
|||||||
else -> cause?.hasOOMCause() ?: false
|
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]. */
|
/** Exception thrown when [ExitCode] != [ExitCode.OK]. */
|
||||||
internal open class FailedCompilationException(message: String, cause: Throwable? = null) : RuntimeException(message, cause)
|
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]. */
|
/** Exception thrown when [ExitCode] == [ExitCode.OOM_ERROR]. */
|
||||||
internal class OOMErrorException(message: String) : FailedCompilationException(message)
|
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 */
|
/** Exception thrown when during the compilation [java.rmi.RemoteException] is caught */
|
||||||
internal class DaemonCrashedException(cause: Throwable) : FailedCompilationException(kotlinDaemonCrashedMessage, cause)
|
internal class DaemonCrashedException(cause: Throwable) : FailedCompilationException(kotlinDaemonCrashedMessage, cause)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user