0

How can I get the following Ruby code to return a nil, if the regvalue doesn't exist?

Right now it works fine when there is something to return, but when there isn't it spits out this Error:

Could not retrieve fact='notepadpp', resolution='': The system cannot find the file specified.

I would expect it to just not output anything if the entry doesn't exist.

Facter.add(:notepadpp) do
    # restricts module to windows
    confine :kernel => :windows
    setcode do
      require 'facter/util/registry'

      notepadpp = nil
      regvalue = Facter::Util::Registry.hklm_read('SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Notepad++', 'Display')
      notepadpp = regvalue if regvalue && !regvalue.empty?

      # if regvalue and not regvalue.empty?
        # notepadpp = regvalue
      # else nil end
    end
end
tek0011
  • 147
  • 1
  • 4
  • 12

2 Answers2

2

Thank you! You did point me in the correct direction. Let me know if this is a real fix, and Ill mark this answered. I took your code and added a rescue.

Facter.add(:notepadpp) do
    confine :kernel => :windows # restricts module to windows
    setcode do
      require 'facter/util/registry'
      begin
        Facter::Util::Registry.hklm_read('SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Notepad++', 'DisplayVersion')
      rescue
        nil
      end
    end
end
tek0011
  • 147
  • 1
  • 4
  • 12
1

I did clean the code up and everything suddenly got working:

Facter.add(:notepadpp) do
    confine :kernel => :windows # restricts module to windows
    setcode do
      require 'facter/util/registry'
      Facter::Util::Registry.hklm_read('...\Notepad++', 'Display') rescue nil
    end 
end

#⇒ #<Facter::Util::Fact:0xb907a44 ...>

The function is not clever enough to return nil when the registry branch is not found, it likely throws an exception, so we are to handle it.

UPD If you do not want the fact to be added unless the registry key exists:

require 'facter/util/registry'
reg_value = Facter::Util::Registry.hklm_read('...\Notepad++', 'Display') rescue nil
Facter.add(:notepadpp) do
    confine :kernel => :windows # restricts module to windows
    setcode do
      regvalue
    end 
end if regvalue
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Thanks, but it still fails with `Could not retrieve fact='notepadpp', resolution='': The system cannot find the file specified.` – tek0011 May 05 '15 at 13:23
  • It fails in different place. Please show the code around the line you see in this error message. – Aleksei Matiushkin May 05 '15 at 13:27
  • The code is yours above. The issue is that if the registry key doesnt exist, it will end the flow of the code. So I need a way to handle the registry variable if the registry key doesnt exist. – tek0011 May 05 '15 at 13:30
  • This code just adds a `fact`, it can’t fail. You might try it in the console: it perfectly works. – Aleksei Matiushkin May 05 '15 at 13:36
  • Ive been at it for 5 hours now (I suck at programming). It does indeed fail via console. It should return nothing if the entry doesnt exist. – tek0011 May 05 '15 at 13:41
  • Thank you! You did point me in the correct direction. Let me know if this is a real fix, and Ill mark this answered. I took your code and added a rescue. `Facter.add(:notepadpp) do confine :kernel => :windows # restricts module to windows setcode do require 'facter/util/registry' begin Facter::Util::Registry.hklm_read('SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Notepad++', 'DisplayVersion') rescue nil end end end` – tek0011 May 05 '15 at 13:50
  • I finally got what the problem is. It sounds like `Facter::Util::Registry.hklm_read` throws an exception on inexisting keys, that’s why the code worked on my side. Well, than rescueing the exception definitely works. One might use the short rescue form, please see an update. – Aleksei Matiushkin May 05 '15 at 14:02