question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

recoverStackTrace breaks exception message

See original GitHub issue

Consider 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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
qwwdfsadcommented, Oct 23, 2019

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, with Throwable (or call initCause, whatever) and we cannot potentially know whether this String parameter maps directly to Throwable.message or not.

As a workaround, you can fix this problem by extending CopyableThrowable in your MyTokenException class

0reactions
qwwdfsadcommented, Dec 10, 2019

The 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found