-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Unexpected error while processing request: undefined method `downcase' for 2:Fixnum #1059
Comments
Do you think you could put a small project up that reproduces this? |
I will try it tomorrow |
Using pry to debug ... grape 0.10.0 send an Array as response [200, {header}, {body}] grape 0.10.0 reading swagger_doc
grape 0.12.0 send a Rack::Response and activerecord failed with rack on undefined method `downcase' for 2:Fixnum grape 0.12.0 reading swagger_doc
If i transform the Rack::Response to an array, it could work ! |
If i set this is connection_pool.rb in activerecord line 649
It works ! but this is not a solution to "monkey patch" activerecord ! |
A grape middleware could do the |
It will be difficult for me to put up a project with all dependencies in a short time. |
Do you use some connection pooling middleware anywhere explicitly via a FWIW https://github.com/dblock/grape-on-rails worked without changes with Grape 0.12.0. The clean way of working around this is to insert a middleware ahead of the ActiveRecord one that calls class RackResponseMiddleware
def initialize(app)
@app = app
end
def self.call(env)
response = @app.call(env)
response = response.to_a if response.is_a?(Rack::Response)
response
end
end # config.ru
use RackResponseMiddleware |
No, i don't use a connection pooling middleware via a use I establish a connection to the database in an initializer via ActiveRecord::Base.establish_connection ... If i create a monkey patch for ActiveRecord, grape 0.12.0 works module ActiveRecord
module ConnectionAdapters
class ConnectionManagement
def initialize(app)
@app = app
end
def call(env)
testing = env['rack.test']
response = @app.call(env)
response = response.to_a if response.is_a?(Rack::Response)
response[2] = ::Rack::BodyProxy.new(response[2]) do
ActiveRecord::Base.clear_active_connections! unless testing
end
response
rescue Exception
ActiveRecord::Base.clear_active_connections! unless testing
raise
end
end
end
end |
Working with your solution with grape 0.12.0 without monkey patching ActiveRecord I have set this in an initializer/90_rackresponse_middleware.rb file loaded after establishing connection but before mounting my API class RackResponseMiddleware
def initialize(app)
@app = app
end
def call(env)
response = @app.call(env)
response = response.to_a if response.is_a?(Rack::Response)
response
end
end And in my API class class Api < Grape::API
include Grape::ActiveRecord::Extension
use RackResponseMiddleware
... Thank you ! |
I think we really need to understand whether this is the right solution and document it. But not without a setup that reproduces the problem in which we understand what's going on. Note that the above patch prevents streaming, and while it's maybe a 1% scenario it still means someone is doing something wrong somewhere ;) Leaving this open. |
I have reproduced the bug with a short code that you could retrieve here It seems to become from Grape::ActiveRecord extension |
For informations, I use include Grape::ActiveRecord::Extension to have rake tasks associated to my project |
I got finally another issue with database connection pools. # Middleware to clear sleeping connections with ActiveRecord/Grape
# @see https://github.com/intridea/grape/issues/517
class GrapeActiveRecordMiddleware
def initialize(app)
@app = app
end
def call(env)
ActiveRecord::Base.connection_pool.connections.map(&:verify!)
status, headers, bodies = catch(:error) do
@app.call(env)
end
ActiveRecord::Base.clear_active_connections!
if status.is_a?(Hash)
throw :error, status
else
[status, headers, bodies]
end
end
end
class API < Grape::API
include Grape::ActiveRecord::Extension
use GrapeActiveRecordMiddleware
... |
@patfrat I encourage you to write some tests, release this as a gem and we should link it from Grape README. |
@dblock when I have time, I will do that ! |
Sometimes `response` is not what you think it is. `@app_response` is always a proper Rack::Response object so it's safe to get its status. See ruby-grape/grape#1059
Recently encountered a similar issue. Is this issue related to the fact that In ActiveRecord 4.2.x's Rack middleware, it does this: def call(env)
...
response[2] = ::Rack::BodyProxy.new(response[2]) do
ActiveRecord::Base.clear_active_connections! unless testing
end
...
end Specifically,
|
Yes @stevendaniels. We went back and forth on this and somewhere decided that we should continue returning a |
In case you're using
inside |
My Grape API is not working with grape v0.11.0 nor grape v0.12.0
Working with grape v0.10.0
I am using Rack Cascasde to load my API, ApiDoc with grape_swagger and my statics files
Using grape-activerecord v0.0.8, grape-entity 0.4.5 and grape-swagger 0.10.1
My database connection is loaded before invoking the API
It's only on the first GET that it fails
And it's difficult to see why and where in my project with this kind of trace !
The text was updated successfully, but these errors were encountered: