Introduce Throwable.toStringWithTrace

#KT-37603
This commit is contained in:
Ilya Gorbunov
2020-03-18 07:49:26 +03:00
parent 7b7263c5bf
commit 552bcdb31b
7 changed files with 154 additions and 4 deletions
+43
View File
@@ -87,4 +87,47 @@ class ExceptionTest {
}
}
@Test
fun exceptionDetailedTrace() {
fun root(): Nothing = throw IllegalStateException("Root cause\nDetails: root")
fun suppressedError(id: Int): Throwable = UnsupportedOperationException("Side error\nId: $id")
fun induced(): Nothing {
try {
root()
} catch (e: Throwable) {
e.addSuppressed(suppressedError(1))
throw RuntimeException("Induced", e)
}
}
val e = try {
induced()
} catch (e: Throwable) {
e.apply { addSuppressed(suppressedError(2)) }
}
val topLevelTrace = e.toStringWithTrace()
fun assertInTrace(value: Any) {
if (value.toString() !in topLevelTrace) {
fail("Expected top level trace: $topLevelTrace\n\nto contain: $value")
}
}
assertInTrace(e)
val cause = assertNotNull(e.cause, "Should have cause")
assertInTrace(cause)
if (supportsSuppressedExceptions) {
val topLevelSuppressed = e.suppressedExceptions.single()
assertInTrace(topLevelSuppressed)
val causeSuppressed = cause.suppressedExceptions.single()
assertInTrace(causeSuppressed)
}
// fail(topLevelTrace) // to dump the entire trace
}
}