Ruby Forum Ruby-core > Problem with YAML and Exception

Posted by Bill Lear (rael007)
on 02.05.2008 18:26
(Received via mailing list)
Very sorry if this is the wrong forum, but using ruby 1.8.6
(2008-03-03 patchlevel 114) [x86_64-linux], I am experiencing a
problem with YAML and Exceptions.  I posted to the Ruby forum but
did not get a definitive response, hence my attempt here.

I would like to serialize and deserialize exception objects, but when I
deserialize a serialized exception, it does not appear to be able to
access the original message, even thought the YAML output seems to have
it.

Here is a short test script illustrating the issue:

% cat test_exception_dump.rb
require 'cgi'
require 'yaml'

begin
        raise "Ain't got no mojo"
rescue => ex
        puts "class=#{ex.class} message=#{ex.message}"
        puts ex.message

        dumped = YAML::dump(ex)
        puts dumped

        loaded = YAML::load(dumped)
        puts "class=#{loaded.class} message=#{loaded.message}"

        dumped_2 = YAML::dump(loaded)
        puts dumped_2

        loaded_2 = YAML::load(dumped_2)
        puts "class=#{loaded_2.class} message=#{loaded_2.message}"
end

and here is a run:

% ruby --version
ruby 1.8.6 (2008-03-03 patchlevel 114) [x86_64-linux]

% ruby test_exception_dump.rb
class=RuntimeError message=Ain't got no mojo
Ain't got no mojo
--- !ruby/exception:RuntimeError
message: Ain't got no mojo
class=RuntimeError message=RuntimeError
--- !ruby/exception:RuntimeError
message: RuntimeError
mesg: Ain't got no mojo
class=RuntimeError message=RuntimeError

In short, the problem is that if I do this:

yamilized_exception = YAML::load(YAML::dump(original_exception))

then yamlized_exception.message will be (in my case) "RuntimeError",
whereas original_exception.essage will be "Ain't got no mojo", even
though the YAML::dump(original_exception) is:

--- !ruby/exception:RuntimeError
message: Ain't got no mojo


Again, sorry if this is not the correct forum.


Bill