Skip to content

Commit

Permalink
issue #533: rescue only default StandardError
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDKelley committed Mar 21, 2021
1 parent 8373721 commit 21cb794
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/listen/thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def new(name, &block)

def rescue_and_log(method_name, *args, caller_stack: nil)
yield(*args)
rescue Exception => exception # rubocop:disable Lint/RescueException
rescue => exception
_log_exception(exception, method_name, caller_stack: caller_stack)
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/listen/event/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,20 @@ def status_for_time(time)
end

describe '_process_changes' do
context 'when it raises an exception derived from StandardError or not' do
context 'when it raises an exception derived from StandardError' do
before do
allow(event_queue).to receive(:empty?).and_return(true)
allow(config).to receive(:callable?).and_return(true)
resulting_changes = { modified: ['foo'], added: [], removed: [] }
allow(config).to receive(:optimize_changes).with(anything).and_return(resulting_changes)
expect(config).to receive(:call).and_raise(ArgumentError, "bang!")
expect(config).to receive(:call).and_return(nil)
expect(config).to receive(:call).and_raise(ScriptError, "ruby typo!")
expect(config).to receive(:call).and_raise("error!")
end

it 'rescues and logs exception and continues' do
expect(Listen.logger).to receive(:error).with(/Exception rescued in _process_changes:\nArgumentError: bang!/)
expect(Listen.logger).to receive(:error).with(/Exception rescued in _process_changes:\nScriptError: ruby typo!/)
expect(Listen.logger).to receive(:error).with(/Exception rescued in _process_changes:\nRuntimeError: error!/)
expect(Listen.logger).to receive(:debug).with(/Callback \(exception\) took/)
expect(Listen.logger).to receive(:debug).with(/Callback took/)
expect(Listen.logger).to receive(:debug).with(/Callback \(exception\) took/)
Expand Down
42 changes: 30 additions & 12 deletions spec/lib/listen/thread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@
end
end

class TestExceptionDerivedFromException < Exception; end # rubocop:disable Lint/InheritException

context "when exception raised that is not derived from StandardError" do
[SystemExit, SystemStackError, NoMemoryError, SecurityError, TestExceptionDerivedFromException].each do |exception|
context exception.name do
let(:block) do
-> { raise exception, 'boom!' }
end

it "does not rescue" do
expect(Thread).to receive(:new) do |&block|
expect do
block.call
end.to raise_exception(exception, 'boom!')

thread = instance_double(Thread, "thread")
allow(thread).to receive(:name=).with(any_args)
thread
end

subject
end
end
end
end

context "when nested exceptions raised" do
let(:block) { raise_nested_exception_block }

Expand All @@ -78,15 +104,6 @@
subject.join
end
end

context 'when exception raised that is not derived from StandardError' do
let(:block) { raise_script_error_block }

it "still rescues and logs" do
expect(Listen.logger).to receive(:error).with(/Exception rescued in listen-worker_thread:\nScriptError: ruby typo!/)
subject.join
end
end
end

describe '.rescue_and_log' do
Expand All @@ -106,9 +123,10 @@
context 'when exception raised that is not derived from StandardError' do
let(:block) { raise_script_error_block }

it 'still rescues and logs' do
expect(Listen.logger).to receive(:error).with(/Exception rescued in method:\nScriptError: ruby typo!/)
described_class.rescue_and_log("method", &block)
it "raises out" do
expect do
described_class.rescue_and_log("method", &raise_script_error_block)
end.to raise_exception(ScriptError, "ruby typo!")
end
end
end
Expand Down

0 comments on commit 21cb794

Please sign in to comment.