Skip to content

Don't suppress regular ArgumentError exceptions #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,10 @@ def exec_with_object(options, &block)
end
rescue StandardError => e
# it handles: https://github.com/ruby/ruby/blob/v3_0_0_preview1/NEWS.md#language-changes point 3, Proc
raise Grape::Entity::Deprecated.new e.message, 'in ruby 3.0' if e.is_a?(ArgumentError)
# accounting for expose :foo, &:bar
if e.is_a?(ArgumentError) && block.parameters == [[:req], [:rest]]
raise Grape::Entity::Deprecated.new e.message, 'in ruby 3.0'
end

raise e
end
Expand Down
15 changes: 15 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ class SomeObject
def method_without_args
'result'
end

def raises_argument_error
raise ArgumentError, 'something different'
end
end

describe 'with block passed in' do
Expand All @@ -402,6 +406,17 @@ def method_without_args
value = subject.represent(object).value_for(:that_method_without_args)
expect(value).to eq('result')
end

it 'does not suppress ArgumentError' do
subject.expose :raises_argument_error do |object|
object.raises_argument_error
end

object = SomeObject.new
expect do
subject.represent(object).value_for(:raises_argument_error)
end.to raise_error(ArgumentError, 'something different')
end
end

context 'with block passed in via &' do
Expand Down