YAML - 类型错误:无法转储匿名模块(YAML - TypeError: can't

2019-08-19 06:48发布

在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和发送电子邮件,因为delayed_job的需要序列化对象到数据库我得到这个错误。

Answer 1:

问题是,该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 -hash(我们不能转换)。 删除它。

最后tmp_env["action_dispatch.remote_ip"]看起来像一个字符串(检查时),但它是一个ActionDispatch::RemoteIp::GetIp实例。 它包含了另一个参照原来的env哈希值。 我们把它转换为字符串,因为我不知道你是否有兴趣在该键后。

此外,您可能会删除更多的键来减少你的YAML输出的大小。 不过,这应该不会引发您所遇到的错误。 一个精简的解决办法是先建立一个空的Hash,只复制你真的需要在你的YAML输出的关键。

红宝石1.9.3和3.2.13轨测试的



Answer 2:

这就是我想出的基础上,tessi的例子:

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

  ## From http://stackoverflow.com/questions/7604153/rails-2-3-14-how-to-serialise-an-actioncontrollerrequest-object
  ## 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


Answer 3:

通过从代码物理 ,可能听起来有点奇怪,但

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

可能工作。 酷,哼?



文章来源: YAML - TypeError: can't dump anonymous module