Grape: NoMethodError(undefined method `bad_request?')

Created on 29 Jul 2017  路  9Comments  路  Source: ruby-grape/grape

As an example: config.ru

Once an error is raised in API (in the code above, it's an " undefined method `session' " error), I can only get the following exception in the response:

NoMethodError: undefined method `bad_request?' for #

without any information related to the original error.

bug?

Most helpful comment

This is actually a Sinatra issue. It extends Rack::ShowExceptions and sets the TEMPLATE constant in order to override the displayed exception.

https://github.com/sinatra/sinatra/blob/master/lib/sinatra/show_exceptions.rb#L365

The problem is that the template calls the bad_request? method that only exists in Sinatra::ShowExceptions, so any other Rack app like Grape will raise an error when showing exceptions.

All 9 comments

What's the stack? Why do you think it's a problem with the Grape API?

There's a lot going on in this API, a database, some session code, etc. Strip it down or write it as a spec in this project.

@dblock Here is the stack:

Puma caught this error: undefined method bad_request?' for #<Rack::ShowExceptions:0x000000035d6620> (NoMethodError) (erb):145:inpretty'
/usr/lib/ruby/2.3.0/erb.rb:864:in eval' /usr/lib/ruby/2.3.0/erb.rb:864:inresult'
/var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/show_exceptions.rb:96:in pretty' /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/show_exceptions.rb:32:inrescue in call'
/var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/show_exceptions.rb:23:in call' /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/common_logger.rb:33:incall'
/var/lib/gems/2.3.0/gems/sinatra-2.0.0/lib/sinatra/base.rb:231:in call' /var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/chunked.rb:54:incall'
/var/lib/gems/2.3.0/gems/rack-2.0.3/lib/rack/content_length.rb:15:in call' /var/lib/gems/2.3.0/gems/puma-3.9.1/lib/puma/configuration.rb:224:incall'
/var/lib/gems/2.3.0/gems/puma-3.9.1/lib/puma/server.rb:602:in handle_request' /var/lib/gems/2.3.0/gems/puma-3.9.1/lib/puma/server.rb:435:inprocess_client'
/var/lib/gems/2.3.0/gems/puma-3.9.1/lib/puma/server.rb:299:in block in run' /var/lib/gems/2.3.0/gems/puma-3.9.1/lib/puma/thread_pool.rb:120:inblock in spawn_thread'

It only happens on the requests handled by the Grape part (API).

Actually, what directly cause this exception is that Moneta does not define a "session" method for Grape. So there should have be an exception of "undefined method session'", which also appears in the terminal. But after the exception of "session", another exception(undefined methodbad_request?') is thrown and will be displayed in the response.

Maybe there is something wrong happened when Grape tried to generate a response for the exception in it?

I think you understand what's going on, turn it into a basic minimal project or spec (eg. https://github.com/ruby-grape/grape-on-rack).

@mopperwhite, have you fixed this error?

@incubus Not yet. I could only read error messages using terminals.

This is actually a Sinatra issue. It extends Rack::ShowExceptions and sets the TEMPLATE constant in order to override the displayed exception.

https://github.com/sinatra/sinatra/blob/master/lib/sinatra/show_exceptions.rb#L365

The problem is that the template calls the bad_request? method that only exists in Sinatra::ShowExceptions, so any other Rack app like Grape will raise an error when showing exceptions.

@jkowens thanks!

Sinatra 2.0.1.rc1 has been released and if all goes well 2.0.1 will be released this weekend. A fix for the described problem is included. I think it's safe to close this issue.

cc: @dblock

Does anyone have or wants to contribute an integration spec for this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dblock picture dblock  路  6Comments

TangMonk picture TangMonk  路  8Comments

hoffm picture hoffm  路  5Comments

Legogris picture Legogris  路  6Comments

yanguango picture yanguango  路  8Comments