Do not wrap and display cause's message in compiler exceptions

In case the wrapped exception message contains some long text (bytecode,
IR, etc.), it could be displayed several times in the same stack trace,
which significantly worsened the experience of parsing such stack traces
This commit is contained in:
Alexander Udalov
2019-07-03 18:58:48 +02:00
parent 430aed6559
commit 37002748c8
4 changed files with 30 additions and 33 deletions
@@ -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
@@ -317,9 +317,8 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> 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);
}
}
@@ -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 : "<no bytecode>"),
e, method
);
}
}
@@ -13,29 +13,18 @@ fun getExceptionMessage(
cause: Throwable?,
location: String?
): String = ApplicationManager.getApplication().runReadAction<String> {
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"
}