rescue_from NoMethodError
My setup: Rails 3.0.9, Ruby 1.9.2
I ran into this Rack bug on Heroku dealing with content-type being sent for non-file fields. Specifically the error I get is
NoMethodError (undefined method `rewind' for "blah":String):
"blah" is the value of a url param I'm passing. I'm thinking it should be possible to ignore this error doing something like this
application_controller.rb
rescue_from NoMethodError do |exception|
logger.debug "\n\n==============Rack rewind error=======================\n\n"
end
How do I only check for NoMethodError rewind method? Or perhaps there is a way to override this Rack method?
Just to clar开发者_如何学Pythonify, I have no control over calling the rewind method, this is handled in Rack itself, so I cannot use try
or fix the error.
I would go and try to fix that error, if at all possible. If not, I don't think Rails lets you re-raise the exception from within rescue_from
, but you can call the default error handler instead:
rescue_from NoMethodError do |exception|
if exception.name == :rewind
logger.debug "rewind error"
else
rescue_action_without_handler(exception)
end
end
I had the same problem, a client app written in python needed to upload a file along with couple of other non-file parameters to a Rails back-end. Only parameters with file object worked fine, but Rails raised NoMethodError (undefined method 'rewind' for <String#F3433>):
when the python client sent string params. I solved it by applying the this patch by jdelStrother. It just required to comment following:
# elsif !filename && content_type
# body.rewind
#
# # Generic multipart cases, not coming from a form
# data = {:type => content_type,
# :name => name, :tempfile => body, :head => head}
and replace them with:
else
data = body
end
in method parse_multipart
in module Rack::Utils::Multipart
. just add the patch code in a ruby file in the config/initializers directory.
精彩评论