Skip to content

Ensure core exceptions get raised with $! as cause#9409

Merged
headius merged 5 commits into
jruby:masterfrom
headius:core_exception_causes
May 7, 2026
Merged

Ensure core exceptions get raised with $! as cause#9409
headius merged 5 commits into
jruby:masterfrom
headius:core_exception_causes

Conversation

@headius

@headius headius commented May 1, 2026

Copy link
Copy Markdown
Member

Many places where we raise exceptions from JRuby core code, we do not attach $! as a cause. This leads to several issues, including part of the issue reported in #9398. This PR adds specs and fixes known cases.

@headius headius added this to the JRuby 10.1.1.0 milestone May 1, 2026
@headius
headius force-pushed the core_exception_causes branch from 1855854 to 9804bb8 Compare May 2, 2026 19:44
@headius

headius commented May 2, 2026

Copy link
Copy Markdown
Member Author

@eregon There are some modifications to mspec's raise_error to support matching causes, modifications to existing specs to use that functionality, and some additional cause-checking specs here, if you want to review.

@headius

headius commented May 2, 2026

Copy link
Copy Markdown
Member Author

Failures appear to be GHA issues or bad timing in specs, unrelated to these changes.

@eregon

eregon commented May 3, 2026

Copy link
Copy Markdown
Contributor

Thank you for adding more cause specs.

My initial thought here is whether making raise_error more complicated for this is worth it:

-> { 1 / 0 }.should raise_error(ZeroDivisionError, cause:)
# vs
-> { 1 / 0 }.should raise_error(ZeroDivisionError) { |e| e.cause.should == cause }

It looks nice and it's expressive but OTOH it's not used in that many files.

The main concern is MSpec tries to be as simple as possible so it can be used by early Ruby implementations.
That means so far we don't use keyword arguments in MSpec (git grep -F '**' lib has none).
That's easily solved though, see https://github.com/ruby/mspec/blob/31e8f11c0bf18cf85fa3d3643ea8fc5a395fc3fe/lib/mspec/matchers/complain.rb#L4-L7 for example.

If you want to modify raise_error, could you make a PR https://github.com/ruby/mspec and add spec for it in https://github.com/ruby/mspec/blob/master/spec/matchers/raise_error_spec.rb? Or you could also do it in this PR and run the mspec specs locally.

Comment thread spec/mspec/lib/mspec/matchers/raise_error.rb Outdated
Comment thread spec/mspec/lib/mspec/matchers/raise_error.rb Outdated
@headius

headius commented May 6, 2026

Copy link
Copy Markdown
Member Author

It looks nice and it's expressive but OTOH it's not used in that many files.

I think it will be used in many more as we continue to specify language-level exceptions that should have causes. I've only added a few cases here.

And even without those additional cases, there were many places doing the block form of raise_error just to check causes. Those are almost all one-liners now.

@headius
headius force-pushed the core_exception_causes branch from 9804bb8 to 7ae3e91 Compare May 6, 2026 03:41
@headius

headius commented May 6, 2026

Copy link
Copy Markdown
Member Author

@eregon I cleaned up the commit that adds the cause: functionality, added specs for it, and updated others to support the expanded error output. Also fixed all style issues.

@eregon eregon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you, looks good, just needs a few more tweaks and it's good to go

Comment thread spec/ruby/core/exception/cause_spec.rb Outdated
Comment thread spec/mspec/lib/mspec/matchers/raise_error.rb Outdated
Comment thread spec/mspec/lib/mspec/matchers/raise_error.rb Outdated
Comment thread spec/mspec/spec/matchers/raise_error_spec.rb
Comment thread spec/mspec/spec/matchers/raise_error_spec.rb Outdated
@headius
headius force-pushed the core_exception_causes branch from 7ae3e91 to a08313e Compare May 7, 2026 15:52
headius added 5 commits May 7, 2026 11:04
This adds a cause kwarg to raise_error for matching the cause of
a captured exception. The error output for failed cases is also
modified to show both message and cause if they are being matched.
@headius
headius force-pushed the core_exception_causes branch from a08313e to 45e6592 Compare May 7, 2026 16:04
@headius

headius commented May 7, 2026

Copy link
Copy Markdown
Member Author

All requested changes have been made, merging.

@headius
headius merged commit 5755cfb into jruby:master May 7, 2026
109 checks passed
@headius
headius deleted the core_exception_causes branch May 7, 2026 16:31
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