Graceful failure

Hi,

What’s the best way to gracefully fail a recipe? Currently I’m doing
something like “raise ‘some error’ unless foo”

Thanks,
-James

I prefer:

Chef::Log.fatal!("Some message", 1) unless condition

On Wed, Feb 5, 2014 at 2:37 PM, James Kessler <
james.kessler@tradingtechnologies.com> wrote:

Hi,

What's the best way to gracefully fail a recipe? Currently I'm doing
something like "raise 'some error' unless foo"

Thanks,
-James

Thats the same thing under the hood, so either is fine :slight_smile:

--Noah

On Feb 5, 2014, at 2:41 PM, Josiah Kiehl bluepojo@gmail.com wrote:

I prefer:

Chef::Log.fatal!("Some message", 1) unless condition

On Wed, Feb 5, 2014 at 2:37 PM, James Kessler james.kessler@tradingtechnologies.com wrote:
Hi,

What's the best way to gracefully fail a recipe? Currently I'm doing something like "raise 'some error' unless foo"

Thanks,
-James

raise is better. The various fatal! methods in Chef aren’t intended to halt a Chef run, and they call exit which raises a SystemExit exception. Depending on where you do this, chef-client may actually exit when not intended. For bonus points, you can define a custom error class with code like:

class SomeConditionNotMet < StandardError; end

And then use it:

raise SomeConditionNotMet, “additional detail about what a human does to fix the situation”

HTH,

--
Daniel DeLeo

On Wednesday, February 5, 2014 at 2:42 PM, Noah Kantrowitz wrote:

Thats the same thing under the hood, so either is fine :slight_smile:

--Noah

On Feb 5, 2014, at 2:41 PM, Josiah Kiehl <bluepojo@gmail.com (mailto:bluepojo@gmail.com)> wrote:

I prefer:

Chef::Log.fatal!("Some message", 1) unless condition

On Wed, Feb 5, 2014 at 2:37 PM, James Kessler <james.kessler@tradingtechnologies.com (mailto:james.kessler@tradingtechnologies.com)> wrote:
Hi,

What's the best way to gracefully fail a recipe? Currently I'm doing something like "raise 'some error' unless foo"

Thanks,
-James

+1 for raise.

For bonus points, if you're using an exception handler to, for example send
emails, you will get the actual error in your e-mail, instead of just
'SystemExit'.

regards,
Sölvi

On Wed, Feb 5, 2014 at 11:36 PM, Daniel DeLeo dan@kallistec.com wrote:

raise is better. The various fatal! methods in Chef aren't intended
to halt a Chef run, and they call exit which raises a SystemExit
exception. Depending on where you do this, chef-client may actually exit
when not intended. For bonus points, you can define a custom error class
with code like:

class SomeConditionNotMet < StandardError; end

And then use it:

raise SomeConditionNotMet, "additional detail about what a human does to
fix the situation"

HTH,

--
Daniel DeLeo

On Wednesday, February 5, 2014 at 2:42 PM, Noah Kantrowitz wrote:

Thats the same thing under the hood, so either is fine :slight_smile:

--Noah

On Feb 5, 2014, at 2:41 PM, Josiah Kiehl bluepojo@gmail.com wrote:

I prefer:

Chef::Log.fatal!("Some message", 1) unless condition

On Wed, Feb 5, 2014 at 2:37 PM, James Kessler <
james.kessler@tradingtechnologies.com> wrote:
Hi,

What's the best way to gracefully fail a recipe? Currently I'm doing
something like "raise 'some error' unless foo"

Thanks,
-James