title: Rails exception notifier in rake tasks
body: <p>I have a simple rails application with a few controller and some rake tasks. A couple of tasks are executed by cron configured with <a href="http://github.com/javan/whenever" rel="nofollow noreferrer">whenever gem</a>.</p>
  
  <p>One of my task is executed daily and sometime it raises an exception and by default I receive this warning by cron</p>
  
  <pre><code>rake aborted!
  undefined method `parameterize' for nil:NilClass
  
  Tasks: TOP =&gt; mailboxes:clean_processed
  (See full trace by running task with --trace)
  </code></pre>
  
  <p>I want to debug what's happening and for this reason I've just installed this <a href="https://rubygems.org/gems/exception_notification" rel="nofollow noreferrer">exception notification</a> gem with this line in my <code>Gemfile</code></p>
  
  <pre><code>gem "exception_notification", "~&gt; 2.4.1", :require =&gt; 'exception_notifier'
  </code></pre>
  
  <p>and configured it in my <code>application.rb</code> file with</p>
  
  <pre><code># enable exception notification
  config.middleware.use ExceptionNotifier,
                        :email_prefix =&gt; "[MyAppName] ",
                        :sender_address =&gt; %{"notifier" &lt;report@example.com&gt;},
                        :exception_recipients =&gt; %w{me@example.com}
  </code></pre>
  
  <p>Since this gem is a rack middleware it only works for web requests and not for rake tasks. I'd like to enable it also for rake tasks and I found <a href="https://gist.github.com/551136" rel="nofollow noreferrer">this gist</a> which do the job.</p>
  
  <p>It works, however it's not DRY, I need to repeat gem configuration in that method and I also need to change all my rake tasks to enclose their statements in a block as in</p>
  
  <pre><code>exception_notify { actual_task_code }
  </code></pre>
  
  <p>Is there any better way to solve this?</p>
  
  <p>P.S. If I need to change the notification gem would not be a problem because I added only a few lines of code to my project.</p>
  
  <p>P.P.S. I know that I can also change the rake line in the crontab to add a <code>--trace</code> option, but I don't like that solution, cause exception notifier imho is a better solution which helps also in web code.</p>
  
  <p><strong>Update</strong> I just found out this related question: <a href="https://stackoverflow.com/questions/4104093/exception-notification-for-delayed-job">exception_notification for delayed_job</a> but both the answers use a similar trick.</p>
  
  <p>I'm going to try with an online service like Airbrake (formerly known as hoptoad) or Exceptional, but both of them are paid services...</p>
  
  <p><strong>Update 2</strong>: I tried the <a href="http://airbrakeapp.com/pages/home" rel="nofollow noreferrer">Airbrake App</a>, very nice application, but it suffer for the same problem, I still need to hack the Rakefile to notify exceptions from rake tasks. However the hack is less dry because you just need this code:</p>
  
  <pre><code># notify exceptions
  def exception_notify
    yield
  rescue Exception =&gt; exception
    HoptoadNotifier.notify exception
    raise exception
  end
  </code></pre>
  
  <p>There is no need to repeat any configuration parameter. I think I can't do better than this to get notified of exceptions in rake tasks.</p>