# Chef Exception Handlers and gem dependancies

**URL:** <https://discourse.chef.io/t/chef-exception-handlers-and-gem-dependancies/2461>\
**Category:** Chef Infra (archive)\
**Created:** [February 17, 2012, 2:01am UTC](https://discourse.chef.io/t/chef-exception-handlers-and-gem-dependancies/2461 "2012-02-17T02:01:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Bento](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Aaron\_Bento](https://discourse.chef.io/u/Aaron_Bento)\
**Post date:** [February 17, 2012, 2:01am UTC](https://discourse.chef.io/t/chef-exception-handlers-and-gem-dependancies/2461/1 "2012-02-17T02:01:54Z")

</div>

Hey Everyone,  
I’m a bit confused on how to get a gem (or other resource) installed on a machine that is a dependency for a Chef Exception Handler.

I’m using the EmailMe example from the wiki to familiarize myself with writing handlers:  
[http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers](http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers)

I have the example working if I already have the pony gem installed, but once I try to perform a run on a clean machine, I get:

FATAL: LoadError: no such file to load – pony

After reading the following wiki page, I thought the proper place was to put it above the handler code.  
[http://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time](http://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time)

However that results in:

FATAL: NoMethodError: chef\_handler[MyOrganization::EmailMe] (chef\_handler::email\_me line 25) had an error: NoMethodError: undefined method `gem\_package’ for main:Object

I seem to be caught in a catch-22. The handler is evaluated during the compile stage, but I need to install a gem which doesn’t happen until the execute. How are others doing this with gems or other dependancies not on the system?

I’m using the chef\_handler cookbook and pulling the chef handler via the LWRP in a recipe.

Where is the best place to call gem\_package? Thanks!

–  
Aaron Bento

---

<div class="post-metadata">

**Author:** ![AJ\_Christensen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aj_christensen/32/525_2.png) [@AJ\_Christensen](https://discourse.chef.io/u/AJ_Christensen)\
**Post date:** [February 17, 2012, 2:09am UTC](https://discourse.chef.io/t/chef-exception-handlers-and-gem-dependancies/2461/2 "2012-02-17T02:09:36Z")

</div>

you can use gem\_package during the compile phase:

gem\_package "foo" do  
action :nothing  
end.run\_action(:install)

This file does exactly what you are talking about, fwiw:  
[https://github.com/fujin/chef-minitest/blob/develop/cookbooks/minitest/recipes/default.rb#L19-32](https://github.com/fujin/chef-minitest/blob/develop/cookbooks/minitest/recipes/default.rb#L19-32)

--AJ

On 17 February 2012 15:01, Aaron Bento [rkive.lists@gmail.com](mailto:rkive.lists@gmail.com) wrote:

> Hey Everyone,  
> I'm a bit confused on how to get a gem (or other resource) installed on a  
> machine that is a dependency for a Chef Exception Handler.
> 
> I'm using the EmailMe example from the wiki to familiarize myself with  
> writing handlers:  
> [http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers](http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers)
> 
> I have the example working if I already have the pony gem installed, but  
> once I try to perform a run on a clean machine, I get:
> 
> FATAL: LoadError: no such file to load -- pony
> 
> After reading the following wiki page, I thought the proper place was to put  
> it above the handler code.  
> [http://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time](http://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time)
> 
> However that results in:
> 
> FATAL: NoMethodError: chef\_handler[MyOrganization::EmailMe]  
> (chef\_handler::email\_me line 25) had an error: NoMethodError: undefined  
> method `gem\_package' for main:Object
> 
> I seem to be caught in a catch-22. The handler is evaluated during the  
> compile stage, but I need to install a gem which doesn't happen until the  
> execute. How are others doing this with gems or other dependancies not on  
> the system?
> 
> I'm using the chef\_handler cookbook and pulling the chef handler via the  
> LWRP in a recipe.
> 
> Where is the best place to call gem\_package? Thanks!
> 
> --  
> Aaron Bento

---

<div class="post-metadata">

**Author:** ![Aaron\_Bento](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Aaron\_Bento](https://discourse.chef.io/u/Aaron_Bento)\
**Post date:** [February 21, 2012, 10:43pm UTC](https://discourse.chef.io/t/chef-exception-handlers-and-gem-dependancies/2461/3 "2012-02-21T22:43:25Z")

</div>

AJ,  
Thanks for your pointers. I had the gem\_package in there, but the wrong location. It was in the handler code file. I now have a working setup by making these two changes:

1. Call the gem\_package resource in the recipe.
2. Insert gem "pony" at the top of my handler code.

Thanks again!

--  
Aaron Bento

On Thursday, February 16, 2012 at 6:09 PM, AJ Christensen wrote:

> you can use gem\_package during the compile phase:
> 
> gem\_package "foo" do  
> action :nothing  
> end.run\_action(:install)
> 
> This file does exactly what you are talking about, fwiw:  
> [https://github.com/fujin/chef-minitest/blob/develop/cookbooks/minitest/recipes/default.rb#L19-32](https://github.com/fujin/chef-minitest/blob/develop/cookbooks/minitest/recipes/default.rb#L19-32)
> 
> --AJ
> 
> On 17 February 2012 15:01, Aaron Bento \<[rkive.lists@gmail.com](mailto:rkive.lists@gmail.com) ([mailto:rkive.lists@gmail.com](mailto:rkive.lists@gmail.com))\> wrote:
> 
> > Hey Everyone,  
> > I'm a bit confused on how to get a gem (or other resource) installed on a  
> > machine that is a dependency for a Chef Exception Handler.
> > 
> > I'm using the EmailMe example from the wiki to familiarize myself with  
> > writing handlers:  
> > [http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers](http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers)
> > 
> > I have the example working if I already have the pony gem installed, but  
> > once I try to perform a run on a clean machine, I get:
> > 
> > FATAL: LoadError: no such file to load -- pony
> > 
> > After reading the following wiki page, I thought the proper place was to put  
> > it above the handler code.  
> > [http://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time](http://wiki.opscode.com/display/chef/Evaluate+and+Run+Resources+at+Compile+Time)
> > 
> > However that results in:
> > 
> > FATAL: NoMethodError: chef\_handler[MyOrganization::EmailMe]  
> > (chef\_handler::email\_me line 25) had an error: NoMethodError: undefined  
> > method `gem\_package' for main:Object
> > 
> > I seem to be caught in a catch-22. The handler is evaluated during the  
> > compile stage, but I need to install a gem which doesn't happen until the  
> > execute. How are others doing this with gems or other dependancies not on  
> > the system?
> > 
> > I'm using the chef\_handler cookbook and pulling the chef handler via the  
> > LWRP in a recipe.
> > 
> > Where is the best place to call gem\_package? Thanks!
> > 
> > --  
> > Aaron Bento
