# How to fail the chef run from a start handler?

**URL:** <https://discourse.chef.io/t/how-to-fail-the-chef-run-from-a-start-handler/4693>\
**Category:** Chef Infra (archive)\
**Created:** [November 16, 2013, 11:26pm UTC](https://discourse.chef.io/t/how-to-fail-the-chef-run-from-a-start-handler/4693 "2013-11-16T23:26:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steffen\_Gebert1](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Steffen\_Gebert1](https://discourse.chef.io/u/Steffen_Gebert1)\
**Post date:** [November 16, 2013, 11:26pm UTC](https://discourse.chef.io/t/how-to-fail-the-chef-run-from-a-start-handler/4693/1 "2013-11-16T23:26:37Z")

</div>

Hi,

I’m playing around with etckeeper and chef and wrote a start handler  
that should prevent chef from doing anything, if /etc was modified  
previously.

Is there any way to fail the chef run from a handler? They are executed  
with run\_report\_safely, which catches all the exceptions.

Thanks for your help  
Steffen

[1] [https://github.com/opscode/chef/blob/master/lib/chef/handler.rb#L213](https://github.com/opscode/chef/blob/master/lib/chef/handler.rb#L213)

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [November 18, 2013, 4:58pm UTC](https://discourse.chef.io/t/how-to-fail-the-chef-run-from-a-start-handler/4693/2 "2013-11-18T16:58:51Z")

</div>

On Saturday, November 16, 2013 at 3:26 PM, Steffen Gebert wrote:

> Hi,
> 
> I'm playing around with etckeeper and chef and wrote a start handler  
> that should prevent chef from doing anything, if /etc was modified  
> previously.
> 
> Is there any way to fail the chef run from a handler? They are executed  
> with run\_report\_safely, which catches all the exceptions.
> 
> Thanks for your help  
> Steffen
> 
> [1] [chef/lib/chef/handler.rb at main · chef/chef · GitHub](https://github.com/opscode/chef/blob/master/lib/chef/handler.rb#L213)  
> You could override the #run\_report\_safely method. The code comment about "not overriding this method unless you know what you’re doing” applies to your case. Of course, the caveat there is that other handlers won’t run. For a more elegant approach, you’d need to patch that method, maybe using catch and throw, to allow for changing the state of the chef run to failure.

You could also move the code to the first recipe in the run\_list if that’s something you can control.

--  
Daniel DeLeo

---

<div class="post-metadata">

**Author:** ![Steffen\_Gebert1](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Steffen\_Gebert1](https://discourse.chef.io/u/Steffen_Gebert1)\
**Post date:** [November 18, 2013, 11:46pm UTC](https://discourse.chef.io/t/how-to-fail-the-chef-run-from-a-start-handler/4693/3 "2013-11-18T23:46:01Z")

</div>

Hi Daniel,

thanks for your response!

That worked great, of course.

And here's the result:  
[https://github.com/TYPO3-cookbooks/etckeeper/blob/master/templates/default/chef-client/etckeeper-handler.rb](https://github.com/TYPO3-cookbooks/etckeeper/blob/master/templates/default/chef-client/etckeeper-handler.rb)

Yours  
Steffen

> On Saturday, November 16, 2013 at 3:26 PM, Steffen Gebert wrote:
> 
> > Hi,
> > 
> > I'm playing around with etckeeper and chef and wrote a start handler  
> > that should prevent chef from doing anything, if /etc was modified  
> > previously.
> > 
> > Is there any way to fail the chef run from a handler? They are executed  
> > with run\_report\_safely, which catches all the exceptions.
> > 
> > Thanks for your help  
> > Steffen
> > 
> > [1] [chef/lib/chef/handler.rb at main · chef/chef · GitHub](https://github.com/opscode/chef/blob/master/lib/chef/handler.rb#L213)  
> > You could override the #run\_report\_safely method. The code comment about "not overriding this method unless you know what you’re doing” applies to your case. Of course, the caveat there is that other handlers won’t run. For a more elegant approach, you’d need to patch that method, maybe using catch and throw, to allow for changing the state of the chef run to failure.
> 
> You could also move the code to the first recipe in the run\_list if that’s something you can control.
> 
> --  
> Daniel DeLeo
