Skip to content

Commit d45dcf9

Browse files
committed
Fix Kernel#raise and don't override cause at exception re-raising
1 parent 8248ce9 commit d45dcf9

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Compatibility:
1818
* Fix explicitly inherited `Struct` subclasses and don't provide `#members` method (#3802, @andrykonchin).
1919
* Support Digest plugins (#1390, @nirvdrum).
2020
* Joni has been updated from 2.2.1 to 2.2.6 (@andrykonchin).
21+
* Fix `Kernel#raise` and don't override `cause` at exception re-raising (#3831, @andrykonchin).
2122

2223
Performance:
2324

spec/ruby/core/kernel/raise_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,50 @@
6262
end
6363
end.should raise_error(StandardError, "aaa")
6464
end
65+
66+
it "re-raises a previously rescued exception without overwriting the cause" do
67+
begin
68+
begin
69+
begin
70+
begin
71+
raise "Error 1"
72+
rescue => e1
73+
raise "Error 2"
74+
end
75+
rescue => e2
76+
raise "Error 3"
77+
end
78+
rescue
79+
e2.cause.should == e1
80+
raise e2
81+
end
82+
rescue => e
83+
e.cause.should == e1
84+
end
85+
end
86+
87+
it "re-raises a previously rescued exception with overwriting the cause when it's explicitly specified with :cause option" do
88+
e4 = RuntimeError.new("Error 4")
89+
90+
begin
91+
begin
92+
begin
93+
begin
94+
raise "Error 1"
95+
rescue => e1
96+
raise "Error 2"
97+
end
98+
rescue => e2
99+
raise "Error 3"
100+
end
101+
rescue
102+
e2.cause.should == e1
103+
raise e2, cause: e4
104+
end
105+
rescue => e
106+
e.cause.should == e4
107+
end
108+
end
65109
end
66110

67111
describe "Kernel#raise" do

src/main/ruby/truffleruby/core/kernel.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def raise(exc = undefined, msg = undefined, ctx = nil, cause: undefined, **kwarg
704704

705705
exc.set_backtrace(ctx) if ctx
706706
Primitive.exception_capture_backtrace(exc, 1) unless Truffle::ExceptionOperations.backtrace?(exc)
707-
Primitive.exception_set_cause exc, cause unless Primitive.equal?(exc, cause)
707+
Primitive.exception_set_cause exc, cause unless (exc.cause && !cause_given) || Primitive.equal?(exc, cause)
708708
end
709709

710710
Truffle::ExceptionOperations.show_exception_for_debug(exc, 1) if $DEBUG

0 commit comments

Comments
 (0)