Skip to content

Commit

Permalink
fix(#2236): Stripping the internals of Grape::Endpoint when `NoMeth…
Browse files Browse the repository at this point in the history
…odError` is raised (#2368)

* fix(#2236): Stripping the internals of `Grape::Endpoint` when `NoMethodError` is raised

* fix(#2236): Follow quote ruby style and including the endpoint in the error message

* fix(#2236): Running rubocop and applying corrections
  • Loading branch information
jcagarcia authored Nov 11, 2023
1 parent 32b8d22 commit 0c03b5d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

* [#2364](https://github.com/ruby-grape/grape/pull/2364): Add missing requires - [@ericproulx](https://github.com/ericproulx).
* [#2366](https://github.com/ruby-grape/grape/pull/2366): Default quality to 1.0 in the `Accept` header when omitted - [@hiddewie](https://github.com/hiddewie).
* [#2368](https://github.com/ruby-grape/grape/pull/2368): Stripping the internals of `Grape::Endpoint` when `NoMethodError` is raised - [@jcagarcia](https://github.com/jcagarcia).
* Your contribution here.

### 1.8.0 (2023/08/30)
Expand Down
8 changes: 8 additions & 0 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,13 @@ def options?
options[:options_route_enabled] &&
env[Grape::Http::Headers::REQUEST_METHOD] == Grape::Http::Headers::OPTIONS
end

def method_missing(name, *_args)
raise NoMethodError.new("undefined method `#{name}' for #{self.class} in `#{route.origin}' endpoint")
end

def respond_to_missing?(method_name, include_private = false)
super
end
end
end
24 changes: 24 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,30 @@ def app
end
end

describe '#method_missing' do
context 'when referencing an undefined local variable' do
it 'raises NoMethodError but stripping the internals of the Grape::Endpoint class and including the API route' do
subject.get('/hey') do
undefined_helper
end
expect do
get '/hey'
end.to raise_error(NoMethodError, %r{^undefined method `undefined_helper' for #<Class:0x[0-9a-fA-F]+> in `/hey' endpoint})
end
end

context 'when performing an undefined method of an instance inside the API' do
it 'raises NoMethodError but stripping the internals of the Object class' do
subject.get('/hey') do
Object.new.x
end
expect do
get '/hey'
end.to raise_error(NoMethodError, /^undefined method `x' for #<Object:0x[0-9a-fA-F]+>$/)
end
end
end

it 'does not persist params between calls' do
subject.post('/new') do
params[:text]
Expand Down

0 comments on commit 0c03b5d

Please sign in to comment.