title: Capistrano: how to run a command-line rake task and rescue only for specific errors?
body: <p>In our Capistrano deploy, we have a task to make sure our Solr (via Sunspot gem) is running:</p>
  
  <pre><code>run "cd #{current_path} &amp;&amp; RAILS_ENV=#{rails_env} bundle exec rake sunspot:solr:start"
  </code></pre>
  
  <p>However, this will throw an error if Solr is already running, killing the deploy, so we add <code>; true</code> to the end of the command so Capistrano will finish:</p>
  
  <pre><code>run "cd #{current_path} &amp;&amp; RAILS_ENV=#{rails_env} bundle exec rake sunspot:solr:start; true"
  </code></pre>
  
  <p>There's two problems with this:</p>
  
  <ol>
  <li>It will still continue running if a more serious error occurs, in which case we want the deploy to stop.</li>
  <li>It prints out a long and ugly error trace.</li>
  </ol>
  
  <p>I've tried putting this command in a <code>begin … rescue</code> block, then matching the message like so:</p>
  
  <pre><code>begin
    run "cd #{current_path} &amp;&amp; RAILS_ENV=#{rails_env} bundle exec rake sunspot:solr:start"
  rescue RuntimeError =&gt; failure
    if failure.message =~ /#{text_to_match}/
      logger.debug "Solr is already running: continuing"
    else
      raise failure
    end
  end
  </code></pre>
  
  <p>Unfortunately, the <code>message</code> on the error is just "failed: " followed by the command that failed, which doesn't allow me to distinguish between the benign error of the server already running and a possibly more malignant other error. It also still prints out the long and ugly error trace, so neither of my problems are solved.</p>
  
  <p><strong>Is there a way to run a command-line rake task and rescue only for specific errors in Capistrano?</strong> Ideally, I would also like to get rid of the long, ugly error trace, but that's of course a secondary concern.</p>