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:
@@ -18,8 +18,19 @@ package org.jetbrains.kotlin.codegen;
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.util.ExceptionUtilKt;
|
import org.jetbrains.kotlin.util.ExceptionUtilKt;
|
||||||
|
|
||||||
|
import static org.jetbrains.kotlin.utils.ExceptionUtilsKt.rethrow;
|
||||||
|
|
||||||
public interface CompilationErrorHandler {
|
public interface CompilationErrorHandler {
|
||||||
CompilationErrorHandler THROW_EXCEPTION = (exception, fileUrl) -> {
|
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(
|
throw new IllegalStateException(
|
||||||
ExceptionUtilKt.getExceptionMessage("Backend", "Exception during code generation", exception, fileUrl),
|
ExceptionUtilKt.getExceptionMessage("Backend", "Exception during code generation", exception, fileUrl),
|
||||||
exception
|
exception
|
||||||
|
|||||||
@@ -317,9 +317,8 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
catch (ProcessCanceledException | CompilationException e) {
|
catch (ProcessCanceledException | CompilationException e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
catch (Throwable error) {
|
catch (Throwable e) {
|
||||||
String message = error.getMessage();
|
throw new CompilationException("Failed to generate expression: " + selector.getClass().getSimpleName(), e, selector);
|
||||||
throw new CompilationException(message != null ? message : "null", error, selector);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -985,16 +985,14 @@ public class FunctionCodegen {
|
|||||||
catch (ProcessCanceledException e) {
|
catch (ProcessCanceledException e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
catch (Throwable t) {
|
catch (Throwable e) {
|
||||||
String bytecode = renderByteCodeIfAvailable(mv);
|
String bytecode = renderByteCodeIfAvailable(mv);
|
||||||
throw new CompilationException(
|
throw new CompilationException(
|
||||||
"wrong code generated\n" +
|
"wrong bytecode generated" +
|
||||||
(description != null ? " for " + description : "") +
|
(description != null ? " for " + description : "") + "\n" +
|
||||||
t.getClass().getName() +
|
(bytecode != null ? bytecode : "<no bytecode>"),
|
||||||
" " +
|
e, method
|
||||||
t.getMessage() +
|
);
|
||||||
(bytecode != null ? "\nbytecode:\n" + bytecode : ""),
|
|
||||||
t, method);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,29 +13,18 @@ fun getExceptionMessage(
|
|||||||
cause: Throwable?,
|
cause: Throwable?,
|
||||||
location: String?
|
location: String?
|
||||||
): String = ApplicationManager.getApplication().runReadAction<String> {
|
): String = ApplicationManager.getApplication().runReadAction<String> {
|
||||||
val result = StringBuilder(subsystemName + " Internal error: ").append(message).append("\n")
|
buildString {
|
||||||
if (cause != null) {
|
append(subsystemName).append(" Internal error: ").appendln(message)
|
||||||
val causeMessage = cause.message
|
|
||||||
result.append("Cause: ").append(causeMessage ?: cause.toString()).append("\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (location != null) {
|
if (location != null) {
|
||||||
result.append("File being compiled at position: ").append(location).append("\n")
|
append("File being compiled at position: ").appendln(location)
|
||||||
} else {
|
} else {
|
||||||
result.append("Element is unknown")
|
append("Element is unknown")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cause != null) {
|
if (cause != null) {
|
||||||
result.append("The root cause was thrown at: ").append(where(cause))
|
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"
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user