Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 08fe8c1

Browse files
committed
Merge pull request #237 from rspec/non-pass-through-exceptions
Define module we can use to safely rescue most exceptions.
2 parents acd77ac + b7197a2 commit 08fe8c1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/rspec/support.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ def self.warning_notifier
118118
@warning_notifier ||= DEFAULT_WARNING_NOTIFIER
119119
end
120120

121+
# @private
122+
module AllExceptionsExceptOnesWeMustNotRescue
123+
# These exceptions are dangerous to rescue as rescuing them
124+
# would interfere with things we should not interfere with.
125+
AVOID_RESCUING = [NoMemoryError, SignalException, Interrupt, SystemExit]
126+
127+
def self.===(exception)
128+
AVOID_RESCUING.none? { |ar| ar === exception }
129+
end
130+
end
131+
121132
# The Differ is only needed when a a spec fails with a diffable failure.
122133
# In the more common case of all specs passing or the only failures being
123134
# non-diffable, we can avoid the extra cost of loading the differ, diff-lcs,

spec/rspec/support_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,42 @@ def append_to_array_instead_of_warning
207207
}.to append_to_array_instead_of_warning
208208
end
209209
end
210+
211+
describe Support::AllExceptionsExceptOnesWeMustNotRescue do
212+
it "rescues a StandardError" do
213+
expect {
214+
begin
215+
raise StandardError
216+
rescue subject
217+
end
218+
}.not_to raise_error
219+
end
220+
221+
it 'rescues an Exception' do
222+
expect {
223+
begin
224+
raise Exception
225+
rescue subject
226+
end
227+
}.not_to raise_error
228+
end
229+
230+
Support::AllExceptionsExceptOnesWeMustNotRescue::AVOID_RESCUING.each do |klass|
231+
exception = if klass == SignalException
232+
SignalException.new("INT")
233+
else
234+
klass
235+
end
236+
237+
it "does not rescue a #{klass}" do
238+
expect {
239+
begin
240+
raise exception
241+
rescue subject
242+
end
243+
}.to raise_error(klass)
244+
end
245+
end
246+
end
210247
end
211248
end

0 commit comments

Comments
 (0)