diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationErrorHandler.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationErrorHandler.java index 61fe7a02cc8..fa94905612b 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationErrorHandler.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationErrorHandler.java @@ -18,8 +18,19 @@ package org.jetbrains.kotlin.codegen; import org.jetbrains.kotlin.util.ExceptionUtilKt; +import static org.jetbrains.kotlin.utils.ExceptionUtilsKt.rethrow; + public interface CompilationErrorHandler { CompilationErrorHandler THROW_EXCEPTION = (exception, fileUrl) -> { + // CompilationException is already supposed to have all information about the context + if (exception instanceof CompilationException) { + try { + throw exception; + } catch (Throwable t) { + throw rethrow(t); + } + } + throw new IllegalStateException( ExceptionUtilKt.getExceptionMessage("Backend", "Exception during code generation", exception, fileUrl), exception diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index a88ab2a0bc3..6430d55ed69 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -317,9 +317,8 @@ public class ExpressionCodegen extends KtVisitor impleme catch (ProcessCanceledException | CompilationException e) { throw e; } - catch (Throwable error) { - String message = error.getMessage(); - throw new CompilationException(message != null ? message : "null", error, selector); + catch (Throwable e) { + throw new CompilationException("Failed to generate expression: " + selector.getClass().getSimpleName(), e, selector); } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java index d0f96f5a2e6..32e2a34f3c0 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java @@ -985,16 +985,14 @@ public class FunctionCodegen { catch (ProcessCanceledException e) { throw e; } - catch (Throwable t) { + catch (Throwable e) { String bytecode = renderByteCodeIfAvailable(mv); throw new CompilationException( - "wrong code generated\n" + - (description != null ? " for " + description : "") + - t.getClass().getName() + - " " + - t.getMessage() + - (bytecode != null ? "\nbytecode:\n" + bytecode : ""), - t, method); + "wrong bytecode generated" + + (description != null ? " for " + description : "") + "\n" + + (bytecode != null ? bytecode : ""), + e, method + ); } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt index 9613202561d..41be20f3439 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt @@ -13,29 +13,18 @@ fun getExceptionMessage( cause: Throwable?, location: String? ): String = ApplicationManager.getApplication().runReadAction { - val result = StringBuilder(subsystemName + " Internal error: ").append(message).append("\n") - if (cause != null) { - val causeMessage = cause.message - result.append("Cause: ").append(causeMessage ?: cause.toString()).append("\n") - } + buildString { + append(subsystemName).append(" Internal error: ").appendln(message) - if (location != null) { - result.append("File being compiled at position: ").append(location).append("\n") - } else { - result.append("Element is unknown") - } + if (location != null) { + append("File being compiled at position: ").appendln(location) + } else { + append("Element is unknown") + } - if (cause != null) { - result.append("The root cause was thrown at: ").append(where(cause)) + if (cause != null) { + append("The root cause ${cause::class.java.name} was thrown at: ") + append(cause.stackTrace?.firstOrNull()?.toString() ?: "unknown") + } } - - result.toString() -} - -private fun where(cause: Throwable): String { - val stackTrace = cause.stackTrace - if (stackTrace != null && stackTrace.size > 0) { - return stackTrace[0].fileName + ":" + stackTrace[0].lineNumber - } - return "unknown" }