Skip to content

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

Merged
dblock merged 3 commits into
ruby-grape:masterfrom
jcagarcia:error-log
Nov 11, 2023
Merged

fix(#2236): Stripping the internals of Grape::Endpoint when NoMethodError is raised#2368
dblock merged 3 commits into
ruby-grape:masterfrom
jcagarcia:error-log

Conversation

@jcagarcia

@jcagarcia jcagarcia commented Nov 9, 2023

Copy link
Copy Markdown
Contributor

Hi all! 👋

My first contribution to the Grape project. Let's see if this can help to solve the issue #2236

With the fix, when referencing an undefined local variable within a grape controller, Grape is returning this error message.

(error trace updated after applying @dblock suggestions)

NoMethodError: undefined method `authenticate!' for #<Class:0x0000000103996728> in `/foo' endpoint
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:408:in `method_missing'
	/Users/Juancarlos.Garcia/code/grape-2236/api.rb:7:in `block in <class:MyAPI>'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:58:in `call'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:58:in `block (2 levels) in generate_api_method'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/activesupport-7.0.8/lib/active_support/notifications.rb:208:in `instrument'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:57:in `block in generate_api_method'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:328:in `execute'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:260:in `block in run'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/activesupport-7.0.8/lib/active_support/notifications.rb:208:in `instrument'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:240:in `run'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:316:in `block in build_stack'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/middleware/base.rb:36:in `call!'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/middleware/base.rb:29:in `call'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/middleware/error.rb:39:in `block in call!'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/middleware/error.rb:38:in `catch'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/middleware/error.rb:38:in `call!'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/middleware/base.rb:29:in `call'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/rack-2.2.8/lib/rack/head.rb:12:in `call'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:224:in `call!'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/endpoint.rb:218:in `call'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/router/route.rb:58:in `exec'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/router.rb:120:in `process_route'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/router.rb:74:in `block in identity'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/router.rb:94:in `transaction'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/router.rb:72:in `identity'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/router.rb:56:in `block in call'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/router.rb:136:in `with_optimization'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/router.rb:55:in `call'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/api/instance.rb:165:in `call'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/api/instance.rb:70:in `call!'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/api/instance.rb:65:in `call'
	/Users/Juancarlos.Garcia/code/grape/lib/grape/api.rb:81:in `call'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/rack-2.2.8/lib/rack/tempfile_reaper.rb:15:in `call'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/rack-2.2.8/lib/rack/lint.rb:50:in `_call'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/rack-2.2.8/lib/rack/lint.rb:38:in `call'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/rack-2.2.8/lib/rack/show_exceptions.rb:23:in `call'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/rack-2.2.8/lib/rack/common_logger.rb:38:in `call'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/rack-2.2.8/lib/rack/content_length.rb:17:in `call'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/puma-5.6.7/lib/puma/configuration.rb:252:in `call'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/puma-5.6.7/lib/puma/request.rb:77:in `block in handle_request'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/puma-5.6.7/lib/puma/thread_pool.rb:340:in `with_force_shutdown'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/puma-5.6.7/lib/puma/request.rb:76:in `handle_request'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/puma-5.6.7/lib/puma/server.rb:443:in `process_client'
	/Users/Juancarlos.Garcia/.rvm/gems/ruby-3.2.2/gems/puma-5.6.7/lib/puma/thread_pool.rb:147:in `block in spawn_thread'
127.0.0.1 - - [10/Nov/2023:10:14:40 +0100] "GET /foo HTTP/1.1" 500 154280 0.0466

Don't know the implications of overriding the method_missing for the Grape::Endpoint instance class. Also, don't know if you want to show more descriptive information a part of the anonymous class object. So suggestions, feedback or any kind of comment is really welcome :)

@dblock dblock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! See comments.

Comment thread lib/grape/endpoint.rb Outdated
@dblock

dblock commented Nov 10, 2023

Copy link
Copy Markdown
Member

Also add a test when you do Object.new.x inside the API. I bet that with this change it references the wrong class where the error is raised (should be Object).

@jcagarcia

Copy link
Copy Markdown
Contributor Author

Hey @dblock , thanks for the review and the suggestions!

I've just pushed two new commits for applying your suggestions.

  1. The quotes now follows the ruby style using backtick and a quote.
  2. I've included the API route in the error message. I'm using the route.origin method for getting the path, let me know if that's correct or if I should use a different method. Also let me know if the message is now fine or you have any other suggestion.
  3. I've added a test for using Object.new.x inside the API. Seems that is referencing the correct class, as it is Object. I think this is because when performing the instance method is not entering in our method_missing method, so I think we are fine.
  4. Performing rubocop and applying corrections.

Hope this changes help!

@dblock
dblock merged commit 0c03b5d into ruby-grape:master Nov 11, 2023
@dblock

dblock commented Nov 11, 2023

Copy link
Copy Markdown
Member

Great work, merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants