From eb948c4a0dc36790c5444fc236b0154c3d716c58 Mon Sep 17 00:00:00 2001 From: Dmitry Ilyin Date: Mon, 24 Aug 2015 22:00:18 +0300 Subject: [MODULES-2462] Improve parseyaml function * Add default value support Second argument will be returned if yaml cannot be parsed instead of false value * Update tests --- lib/puppet/parser/functions/parseyaml.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'lib/puppet/parser/functions/parseyaml.rb') diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb index 53d54fa..d38b3ef 100644 --- a/lib/puppet/parser/functions/parseyaml.rb +++ b/lib/puppet/parser/functions/parseyaml.rb @@ -3,20 +3,22 @@ # module Puppet::Parser::Functions - newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS + newfunction(:parseyaml, :type => :rvalue, :arity => -2, :doc => <<-EOS This function accepts YAML as a string and converts it into the correct Puppet structure. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "parseyaml(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end +The optional second argument can be used to pass a default value that will +be returned if the parsing of YAML string have failed. + EOS + ) do |arguments| + raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless arguments.length >= 1 require 'yaml' - YAML::load(arguments[0]) + begin + YAML::load(arguments[0]) || arguments[1] + rescue Exception + arguments[1] + end end end -- cgit v1.2.3 From 799c38e14e1583e676e2b25a9c1782fd40e29fff Mon Sep 17 00:00:00 2001 From: Morgan Haskel Date: Mon, 21 Sep 2015 10:56:08 -0700 Subject: Fix backwards compatibility from #511 Maintain the old behavior in the case where the optional second parameter isn't passed. Also, adding arity is backwards incompatible since stdlib still supports 2.7, so remove that. --- lib/puppet/parser/functions/parseyaml.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lib/puppet/parser/functions/parseyaml.rb') diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb index d38b3ef..66d0413 100644 --- a/lib/puppet/parser/functions/parseyaml.rb +++ b/lib/puppet/parser/functions/parseyaml.rb @@ -3,7 +3,7 @@ # module Puppet::Parser::Functions - newfunction(:parseyaml, :type => :rvalue, :arity => -2, :doc => <<-EOS + newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS This function accepts YAML as a string and converts it into the correct Puppet structure. @@ -16,8 +16,12 @@ be returned if the parsing of YAML string have failed. begin YAML::load(arguments[0]) || arguments[1] - rescue Exception - arguments[1] + rescue Exception => e + if arguments[1] + arguments[1] + else + raise e + end end end -- cgit v1.2.3 From 0cea94a82ef277092897a03446f6e6fccba90d53 Mon Sep 17 00:00:00 2001 From: Felix Frank Date: Tue, 29 Mar 2016 01:59:54 +0200 Subject: catch StandardError rather than the gratuitous Exception --- lib/puppet/parser/functions/parseyaml.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions/parseyaml.rb') diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb index 66d0413..9e84055 100644 --- a/lib/puppet/parser/functions/parseyaml.rb +++ b/lib/puppet/parser/functions/parseyaml.rb @@ -16,7 +16,7 @@ be returned if the parsing of YAML string have failed. begin YAML::load(arguments[0]) || arguments[1] - rescue Exception => e + rescue StandardError => e if arguments[1] arguments[1] else -- cgit v1.2.3 From 5639828bffd1beb0e44e59554e17c1a891924145 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Thu, 7 Apr 2016 11:47:42 +0100 Subject: (maint) also catch Psych::SyntaxError Psych::SyntaxError is a RuntimeException. This still needs to catch that. This was uncovered by the recent move to catch StandardError rather than the catchall Exception that was here before. --- lib/puppet/parser/functions/parseyaml.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions/parseyaml.rb') diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb index 9e84055..ba9d98a 100644 --- a/lib/puppet/parser/functions/parseyaml.rb +++ b/lib/puppet/parser/functions/parseyaml.rb @@ -16,7 +16,10 @@ be returned if the parsing of YAML string have failed. begin YAML::load(arguments[0]) || arguments[1] - rescue StandardError => e + # in ruby 1.9.3 Psych::SyntaxError is a RuntimeException + # this still needs to catch that and work also on rubies that + # do not have Psych available. + rescue StandardError, Psych::SyntaxError => e if arguments[1] arguments[1] else -- cgit v1.2.3