Skip to content

Commit

Permalink
Exception#set_backtrace accept arrays of Backtrace::Location
Browse files Browse the repository at this point in the history
[Feature #13557]

Setting the backtrace with an array of strings is lossy. The resulting
exception will return nil on `#backtrace_locations`.

By accepting an array of `Backtrace::Location` instance, we can rebuild
a `Backtrace` instance and have a fully functioning Exception.

Co-Authored-By: Étienne Barrié <[email protected]>
  • Loading branch information
2 people authored and eregon committed Mar 14, 2024
1 parent 0d0369d commit cf99ee2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
28 changes: 28 additions & 0 deletions core/exception/set_backtrace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,37 @@
it "allows the user to set the backtrace from a rescued exception" do
bt = ExceptionSpecs::Backtrace.backtrace
err = RuntimeError.new
err.backtrace.should == nil
err.backtrace_locations.should == nil

err.set_backtrace bt

err.backtrace.should == bt
err.backtrace_locations.should == nil
end

ruby_version_is "3.4" do
it "allows the user to set backtrace locations from a rescued exception" do
bt_locations = ExceptionSpecs::Backtrace.backtrace_locations
err = RuntimeError.new
err.backtrace.should == nil
err.backtrace_locations.should == nil

err.set_backtrace bt_locations

err.backtrace_locations.size.should == bt_locations.size
err.backtrace_locations.each_with_index do |loc, index|
other_loc = bt_locations[index]

loc.path.should == other_loc.path
loc.label.should == other_loc.label
loc.base_label.should == other_loc.base_label
loc.lineno.should == other_loc.lineno
loc.absolute_path.should == other_loc.absolute_path
loc.to_s.should == other_loc.to_s
end
err.backtrace.size.should == err.backtrace_locations.size
end
end

it "accepts an empty Array" do
Expand Down
8 changes: 8 additions & 0 deletions shared/kernel/raise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,12 @@ def initialize
@object.raise(ArgumentError, "message", caller)
end.should raise_error(ArgumentError, "message")
end

ruby_version_is "3.4" do
it "allows Exception, message, and backtrace_locations parameters" do
-> do
@object.raise(ArgumentError, "message", caller_locations)
end.should raise_error(ArgumentError, "message")
end
end
end

0 comments on commit cf99ee2

Please sign in to comment.