How to catch Exception of the custom resource


#1

Hi,

I have a wrapper cookbook which use custom resource sensu_gem from cookbook chef-sensu. I want to catch its Exception but cannot.

The code is.

begin
    sensu_gem "pg" do
      compile_time true if respond_to?(:compile_time)
    end
  rescue Exception => e
  puts e.message.to_s
end

It worked fine with built-in Chef resource chef_gem.


#2

That pattern generally won’t work. It happens to work for chef_gem because you’re running it at compile time. If you just want to ignore the failure and keep going, there’s ignore_failure true and if you want retries, there is a property for that. If you need custom logic based on the exception type or content, then you probably need to wrap the resource in another resource.