From efffd27a414000eea0a85dae3162ed65b636cb3b Mon Sep 17 00:00:00 2001 From: Tobias Urdin Date: Tue, 31 Mar 2020 13:41:15 +0200 Subject: [PATCH] Add old ruby support to inet6_prefix When running an older version of Ruby that has an old version of ipaddr this function fails with a uninitialized constant IPAddr::AddressFamilyError error. This can happen if you are for example running this code from inside a Puppetserver that runs under JRuby that has an older version of ipaddr. This adds a check if one of the classes that was introduced to replace the ArgumentError exception is present and customizes the exceptions we catch based on that. Change-Id: I9948d4dc22195300cf1c0a48fcc0694bcf991698 --- lib/puppet/functions/inet6_prefix.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/puppet/functions/inet6_prefix.rb b/lib/puppet/functions/inet6_prefix.rb index 6af2ceeb..6ab25c4b 100644 --- a/lib/puppet/functions/inet6_prefix.rb +++ b/lib/puppet/functions/inet6_prefix.rb @@ -8,6 +8,15 @@ Puppet::Functions.create_function(:inet6_prefix) do def inet6_prefix(*args) require 'ipaddr' + # NOTE(tobias-urdin): Add support for older version of the ipaddr + # lib where ArgumentError hasn't been replaced yet. + if IPAddr.const_defined?('InvalidAddressError') + exp = [IPAddr::AddressFamilyError, IPAddr::Error, IPAddr::InvalidAddressError, + IPAddr::InvalidPrefixError, ArgumentError, NoMethodError] + else + exp = [ArgumentError, NoMethodError] + end + result = [] args = args[0] if args[0].kind_of?(Array) args = [args] unless args.kind_of?(Array) @@ -20,7 +29,7 @@ Puppet::Functions.create_function(:inet6_prefix) do ip = "inet6:[#{ip_parts.shift}]#{ip_parts.join}" end end - rescue IPAddr::AddressFamilyError, IPAddr::Error, IPAddr::InvalidAddressError, IPAddr::InvalidPrefixError, ArgumentError, NoMethodError => e + rescue *exp # ignore it end result << ip