recoverStackTrace breaks exception message
See original GitHub issueConsider the following test case:
class ExceptionTest {
@Test
fun `should preserve exception message`() {
Assertions.assertThatThrownBy {
runBlocking {
withContext(newFixedThreadPoolContext(10, "my-context")) {
throw MyTokenException("42")
}
}
}.isInstanceOf(MyTokenException::class.java).hasMessage("Token 42 is not valid")
}
}
class MyTokenException(token: String) : RuntimeException("Token $token is not valid")
It is failing on message assertion:
Expecting message to be:
<"Token 42 is not valid">
but was:
<"Token Token 42 is not valid is not valid">
It looks like recoverStackTrace / tryCopyException
incorrectly recovers the exception. #1040 mentions that similar problem is fixed in 1.2.0, but this test fails on 1.3.2.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
java - What is a stack trace, and how can I use it to debug my ...
In simple terms, a stack trace is a list of the method calls that the application was in the middle of when an...
Read more >How can I log a stack trace when I throw an exception? Is the ...
This works because Exception::__toString includes the stack trace. <pre> ensures that line breaks are shown properly in your browser when ...
Read more >traceback — Print or retrieve a stack traceback — Python 3.11 ...
Print exception information and stack trace entries from traceback object tb ... The message indicating which exception occurred is the always last string ......
Read more >dotPeek - Explore and navigate exception stack traces
Navigate to code that caused an exception · Copy exception stack trace to the clipboard. · Press Ctrl+E T or choose Tools |...
Read more >Understanding the Python Traceback
Tracebacks are known by many names, including stack trace, stack traceback, ... Blue box: The last line of the traceback is the error...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am not sure we can do better automatically without reflectively hacking Throwable class (that probably will produce a shameful warning on Java 9+), I’ll try to figure it out.
To understand why it happens, I recommend you to get familiar with stacktrace recovery machinery:
The main idea is to reflectively create a copy of the exception, preserving as much information from the original exception as possible. Usually, it is a message and a cause, so we reflectively call a constructor with
String
parameter and, if possible, withThrowable
(or callinitCause
, whatever) and we cannot potentially know whether thisString
parameter maps directly toThrowable.message
or not.As a workaround, you can fix this problem by extending
CopyableThrowable
in yourMyTokenException
classThe easiest option is to introspect constructor parameter name using reflection and copy an exception only if the parameter is named “message”. Unfortunately, this functionality is blocked by #1589