Use Throwable as the exception cause in unwrapOrElseThrow#57
Merged
jaredstehler merged 2 commits intomasterfrom Aug 11, 2025
Merged
Use Throwable as the exception cause in unwrapOrElseThrow#57jaredstehler merged 2 commits intomasterfrom
Throwable as the exception cause in unwrapOrElseThrow#57jaredstehler merged 2 commits intomasterfrom
Conversation
ariofrio-hubspot
commented
Aug 7, 2025
| return unwrapOrElseThrow(err -> new IllegalStateException(err.toString())); | ||
| return unwrapOrElseThrow(err -> { | ||
| if (err instanceof Throwable) { | ||
| return new IllegalStateException((Throwable) err); |
Contributor
Author
There was a problem hiding this comment.
IntellIJ complained I couldn't use err instanceof Throwable throwable because of the language level, which I assume is pretty low for this library to ensure wide compatibility, but lmk if this was just a local setup issue.
jaredstehler
approved these changes
Aug 11, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently the structured call stack and other exception class-specific information is lost because the error is converted to a string unconditionally.
This PR changes the behavior to check if the error is
Throwable, and if so, it uses it as the cause of theIllegalStateException.It also does the same thing for
unwrapErrOrElseThrowfor consistency and completeness, though I imagine this will be pretty rare.Out of Scope
It might be nice to throw the actual error and not wrap it in
IllegalStateException. Not sure how this could be accomplished with Java generics, though. At least with this PR the actual error can still be accessed by traversing the cause chain.