From f770a5be66412fcbdb238db950c80367100a257e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 9 Jan 2019 13:28:08 -0800 Subject: [PATCH] Refactor exception dumping --- ext/psych/lib/psych/visitors/yaml_tree.rb | 45 +++++++++-------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index ed8d9e2dfe..bc7d2352e4 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -181,37 +181,11 @@ module Psych end def visit_Exception o - tag = ['!ruby/exception', o.class.name].join ':' - - @emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK - - msg = private_iv_get(o, 'mesg') - - if msg - @emitter.scalar 'message', nil, nil, true, false, Nodes::Scalar::ANY - accept msg - end - - dump_ivars o - - @emitter.end_mapping + dump_exception o, private_iv_get(o, 'mesg') end def visit_NameError o - tag = ['!ruby/exception', o.class.name].join ':' - - @emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK - - msg = o.message.to_s - - if msg - @emitter.scalar 'message', nil, nil, true, false, Nodes::Scalar::ANY - accept msg - end - - dump_ivars o - - @emitter.end_mapping + dump_exception o, o.message.to_s end def visit_Regexp o @@ -488,6 +462,21 @@ module Psych def dump_list o end + def dump_exception o, msg + tag = ['!ruby/exception', o.class.name].join ':' + + @emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK + + if msg + @emitter.scalar 'message', nil, nil, true, false, Nodes::Scalar::ANY + accept msg + end + + dump_ivars o + + @emitter.end_mapping + end + def format_time time if time.utc? time.strftime("%Y-%m-%d %H:%M:%S.%9N Z")