简体   繁体   English

rescue_from NoMethodError

[英]rescue_from NoMethodError

My setup: Rails 3.0.9, Ruby 1.9.2 我的设置: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. 我遇到了Heroku上的Rack 错误 ,该错误处理的是针对非文件字段发送的内容类型。 Specifically the error I get is 具体来说,我得到的错误是

NoMethodError (undefined method `rewind' for "blah":String):

"blah" is the value of a url param I'm passing. “等等”是我传递的url参数的值。 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? 如何只检查NoMethodError快退方法? Or perhaps there is a way to override this Rack method? 还是有一种方法可以覆盖此Rack方法?

Just to clarify, I have no control over calling the rewind method, this is handled in Rack itself, so I cannot use try or fix the error. 为了澄清起见,我无法控制调用rewind方法,这是在Rack本身中处理的,因此我无法使用try或修复错误。

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: 如果不是这样,我认为Rails不会让您从rescue_from内重新引发异常,但是您可以改为调用默认的错误处理程序:

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. 我有同样的问题,用python编写的客户端应用程序需要将文件以及其他几个非文件参数上传到Rails后端。 Only parameters with file object worked fine, but Rails raised NoMethodError (undefined method 'rewind' for <String#F3433>): when the python client sent string params. 只有带有文件对象的参数才能正常工作,但是Rails引发了NoMethodError (undefined method 'rewind' for <String#F3433>):当python客户端发送字符串参数时。 I solved it by applying the this patch by jdelStrother. 我通过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 . 在模块Rack::Utils::Multipart parse_multipart方法中。 just add the patch code in a ruby file in the config/initializers directory. 只需将补丁代码添加到config / initializers目录中的ruby文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM