Skip to content

Commit

Permalink
Fixed Exception.to_yaml to output the backtrace.
Browse files Browse the repository at this point in the history
Fixed Exception.yaml_new to properly load the message and backtrace.
Added test to ensure an Exception remains the same both sides of a yaml conversion.
  • Loading branch information
cowlibob committed May 7, 2009
1 parent a4f241b commit 4406e06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 15 additions & 3 deletions ext/ruby/lib/yaml/rubytypes.rb
Expand Up @@ -159,17 +159,29 @@ def to_yaml( opts = {} )

class Exception
yaml_as "tag:ruby.yaml.org,2002:exception"

# override exc_equal(), since it fails somehow, although message, backtrace and class are all equal.
def == obj
return false unless self.backtrace == obj.backtrace
return false unless self.message == obj.message
return false unless self.kind_of?(obj.class) || obj.kind_of?(self.class) # transitive
true
end

def Exception.yaml_new( klass, tag, val )
o = YAML.object_maker( klass, { 'mesg' => val.delete( 'message' ) } )
o = eval("#{klass}.new(\"#{val.delete('message')}\")")
o.set_backtrace(val.delete('backtrace'))

val.each_pair do |k,v|
o.instance_variable_set("@#{k}", v)
o.instance_variable_set("@#{k}", v)
end
o
end
def to_yaml( opts = {} )
YAML::quick_emit( object_id, opts ) do |out|
YAML::quick_emit( self, opts ) do |out|
out.map( taguri, to_yaml_style ) do |map|
map.add( 'message', message )
map.add( 'backtrace', backtrace )
to_yaml_properties.each do |m|
map.add( m[1..-1], instance_variable_get( m ) )
end
Expand Down
10 changes: 10 additions & 0 deletions ext/ruby/tests/basic.rb
Expand Up @@ -1651,6 +1651,16 @@ def test_time_now_cycle
# inspect_str = "[[...], [...]]"
# assert_equals( inspect_str, YAML::load( a.to_yaml ).inspect )
# end

#
# Test Exception
#
def test_exception
require 'yaml'
e1 = Exception.new("hello")
e1.set_backtrace(["file1.rb", "file2.rb"])
assert_equals(e1, YAML.load( YAML.dump( e1 ) ) )
end
end

RUNIT::CUI::TestRunner.run( YAML_Unit_Tests.suite )

0 comments on commit 4406e06

Please sign in to comment.