YAML - 类型错误:无法转储匿名模块

5
在application_controller的一个操作中,如果我们尝试:
p request.env.to_yaml

我遇到了以下错误:

    TypeError: can't dump anonymous module: #<Module:0x007fee26e34ad8>
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:267:in `visit_Module'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:447:in `block in dump_ivars'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `each'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `dump_ivars'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:124:in `visit_Object'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:447:in `block in dump_ivars'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `each'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:445:in `dump_ivars'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:124:in `visit_Object'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:292:in `block in visit_Hash'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:290:in `each'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:290:in `visit_Hash'
    from /Users/twer/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:102:in `accept'

我的问题是:如何将request.env序列化为YAML格式?
实际上,我需要将request.env传递给延迟任务(delayed_job)并发送电子邮件。但是,我遇到了错误,因为延迟任务需要将对象序列化到数据库中。

你测试过我的答案了吗?有什么意见吗? - fotanus
3个回答

5
问题是,request.env哈希具有许多嵌套对象(特别是模块),无法转换为yaml。诀窍是删除那些无法转换的哈希部分。
tmp_env = request.env.clone
tmp_env.delete "action_dispatch.routes"
tmp_env.delete "action_controller.instance"
tmp_env["action_dispatch.remote_ip"] = tmp_env["action_dispatch.remote_ip"].to_s 
p tmp_env.to_yaml # now it works

我们首先克隆原始的env哈希表,以免意外修改它。然后我们从副本中删除那些导致错误的键。 tmp_env["action_dispatch.routes"]包含一个匿名模块的引用,在ActionDispatch::Routing::RouteSet对象内部,这是你的错误原因。我们最好将其删除。 tmp_env["action_controller.instance"]包含对原始env哈希表的引用(我们无法转换)。删除它。
最后,tmp_env["action_dispatch.remote_ip"]看起来像一个字符串(在检查它时),但它是一个ActionDispatch::RemoteIp::GetIp实例。它包含对原始env哈希表的另一个引用。我们将其转换为字符串,因为我不知道你是否在以后对该键感兴趣。
此外,您可以删除许多其他键以减小yaml输出的大小。但是,这应该可以正常工作,而不会引发您遇到的错误。更简洁的解决方案是从空哈希开始,并仅复制您在yaml输出中真正需要的键。
使用ruby 1.9.3和rails 3.2.13测试过。

2

根据tessi的示例,这是我想出来的:

module RequestSerializationHelper
  ::SerializableRequest = Struct.new(
    :env,
    :filtered_parameters,
    :fullpath,
    :headers,
    :request_method,
    :remote_ip
  )

  ## From https://dev59.com/I1vUa4cB1Zd3GeqPwsab
  ## with additional modifications

  # build a serializable Struct that out of the given request object, which looks like a real request
  def make_request_serializable(request)
    serializable_request = ::SerializableRequest.new
    serializable_request.env = request.env.clone
    serializable_request.filtered_parameters = request.filtered_parameters.clone if request.respond_to? :filtered_parameters
    serializable_request.fullpath = request.fullpath
    serializable_request.headers = request.respond_to?(:headers) ? request.headers.clone : {}
    serializable_request.request_method = request.request_method

    delete_identified_unserializable_values(serializable_request.env)
    delete_identified_unserializable_values(serializable_request.headers)

    # Some jobs want this, so set it after it's been converted to a string in the env
    serializable_request.remote_ip = serializable_request.env["action_dispatch.remote_ip"]

    # automatically delete anything left that's non-serializable.  If we end up deleting
    # too much and breaking something, here's where to debug it based on info in warning
    delete_unidentified_unserializable_values :env, serializable_request.env
    delete_unidentified_unserializable_values :headers, serializable_request.headers

    serializable_request
  end

  def delete_identified_unserializable_values(hash)
    hash.delete "async.callback"
    hash.delete "action_dispatch.backtrace_cleaner"
    hash.delete "action_dispatch.cookies"
    hash.delete "action_dispatch.request.accepts"
    hash.delete "action_dispatch.routes"
    hash.delete "action_dispatch.logger"
    hash.delete "action_controller.instance"
    hash.delete "rack.input"
    hash.delete "rack.errors"
    hash.delete "rack.session"
    hash.delete "rack.session.options"
    hash["action_dispatch.remote_ip"] = hash["action_dispatch.remote_ip"].to_s
    hash.delete "warden"
    hash.delete_if { |key, _| key =~ /^rack-cache/ }
  end

  private

  def delete_unidentified_unserializable_values(hash_name, hash)
    hash.each do |key, value|
      begin
        serialized = value.to_yaml
        YAML.load(serialized)
      rescue => e
        warning = "RequestSerializationHelper: Automatically removing un(re)serializable entry in " +
          "'#{hash_name}' for key '#{key}' and value '#{value}'.  Exception was: '#{e}'"
        Rails.logger.warn(warning)
        hash.delete key
      end
    end
  end
end

看起来不错,尤其是你把东西组织在一个帮助类里。 - tessi

0

物理的代码来看,可能听起来有点奇怪,但是

request.env.instance_eval "def name; 'some_name'; end"

可能有效。很酷,对吧?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接