Skip to content

Use Throwable as the exception cause in unwrapOrElseThrow#57

Merged
jaredstehler merged 2 commits intomasterfrom
ariofrio/propagate-call-stack
Aug 11, 2025
Merged

Use Throwable as the exception cause in unwrapOrElseThrow#57
jaredstehler merged 2 commits intomasterfrom
ariofrio/propagate-call-stack

Conversation

@ariofrio-hubspot
Copy link
Contributor

@ariofrio-hubspot ariofrio-hubspot commented Aug 7, 2025

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 the IllegalStateException.

It also does the same thing for unwrapErrOrElseThrow for 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.

return unwrapOrElseThrow(err -> new IllegalStateException(err.toString()));
return unwrapOrElseThrow(err -> {
if (err instanceof Throwable) {
return new IllegalStateException((Throwable) err);
Copy link
Contributor Author

@ariofrio-hubspot ariofrio-hubspot Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 jaredstehler merged commit 049f192 into master Aug 11, 2025
1 check passed
@jaredstehler jaredstehler deleted the ariofrio/propagate-call-stack branch August 11, 2025 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants