title: Handle TimeOut::Error with retry
body: <p>I have an application running on Heroku that, on occasion, reports <strong>Timeout::Error</strong> and <strong>(ActionView::Template::Error) "execution expired"</strong>. </p>
  
  <p>This happens all over the website (i.e. not in a specific controller) so I would like to create a function that will handle these two errors, first by retrying two times and the redirect the user to a page that will tell them that the server is busy.</p>
  
  <p>My current plan is to use the following in ApplicationController:</p>
  
  <pre><code>  rescue_from Timeout::Error, :with =&gt; :rescue_from_timeout
  
    def rescue_from_timeout
      sleep 2
      retry 
    end
  </code></pre>
  
  <p>but this will just loop and loop. I want it to break after two attempts. How can I do that?</p>
  
  <p>Also, How can I handle ActionView::Template::Error for "execution expired"? I don't want to rescue all ActionView::Template::Error with retries, only the ones that provoke the "execution expired". </p>
  
  <p>This is what my exception says:</p>
  
  <pre><code>[Exception] home#index (ActionView::Template::Error) "execution expired"
  </code></pre>
  
  <p>or</p>
  
  <pre><code>[Exception] calculations#result (ActionView::Template::Error) "Timeout::Error: execution expired
  </code></pre>
  
  <p>My question, thus: How can I handle these two types of errors by first retry twice and then throw an exception / redirect to error page?</p>