Skip to content

Commit

Permalink
Handle error in extractors when the extractor is a class
Browse files Browse the repository at this point in the history
Unfortunatly the the change I made in rubocop#13263 is not sufficient.
This gracefully handles the other case as well.
  • Loading branch information
Earlopain authored and bbatsov committed Oct 24, 2024
1 parent 61e8759 commit b42c30b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_handling_extractor_class.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13382](https://github.com/rubocop/rubocop/pull/13382): Fix an error during error handling for custom ruby extractors when the extractor is a class. ([@earlopain][])
8 changes: 6 additions & 2 deletions lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,12 @@ def extract_ruby_sources(processed_source)
result = ruby_extractor.call(processed_source)
break result if result
rescue StandardError
raise Error, "Ruby extractor #{ruby_extractor.source_location[0]} failed to process " \
"#{processed_source.path}."
location = if ruby_extractor.is_a?(Proc)
ruby_extractor.source_location
else
ruby_extractor.method(:call).source_location
end
raise Error, "Ruby extractor #{location[0]} failed to process #{processed_source.path}."
end
end

Expand Down
38 changes: 28 additions & 10 deletions spec/rubocop/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,40 @@ def INVALID_CODE; end
end

context 'when the extractor crashes' do
let(:custom_ruby_extractor) do
lambda do |_processed_source|
raise 'Oh no!'
end
end

let(:source) { <<~RUBY }
# frozen_string_literal: true
def foo; end
RUBY

it 'raises an error with the crashing extractor/file' do
expect do
runner.run([])
end.to raise_error(RuboCop::Error, /runner_spec\.rb failed to process .*example\.rb/)
shared_examples 'error handling' do
it 'raises an error with the crashing extractor/file' do
expect do
runner.run([])
end.to raise_error(RuboCop::Error, /runner_spec\.rb failed to process .*example\.rb/)
end
end

context 'and it is a Proc' do
let(:custom_ruby_extractor) do
lambda do |_processed_source|
raise 'Oh no!'
end
end

it_behaves_like 'error handling'
end

context 'and it responds to `call`' do
let(:custom_ruby_extractor) do
Class.new do
def self.call(_processed_source)
raise 'Oh no!'
end
end
end

it_behaves_like 'error handling'
end
end
end
Expand Down

0 comments on commit b42c30b

Please sign in to comment.