How to catch Exception of the custom resource


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.

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

It worked fine with built-in Chef resource chef_gem.

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.

1 Like