From 3baed5876104d4c0f92c81b8453d49d5b12046cd Mon Sep 17 00:00:00 2001
From: Azul <azul@riseup.net>
Date: Thu, 14 Mar 2013 17:56:59 +0100
Subject: [PATCH] raise a couchrest specific error if connection to the
 database fails

The underlying Errno errors are very generic. It's not clear from the logs that the connection to the couchdb failed. Also hard to catch these because they are all different if the connection fails for different reasons.
---
 lib/couchrest/model/connection.rb | 4 ++++
 lib/couchrest/model/errors.rb     | 7 +++++++
 2 files changed, 11 insertions(+)

diff --git a/lib/couchrest/model/connection.rb b/lib/couchrest/model/connection.rb
index d85debe3..78621e20 100644
--- a/lib/couchrest/model/connection.rb
+++ b/lib/couchrest/model/connection.rb
@@ -33,6 +33,10 @@ def prepare_database(db = nil)
           else
             db
           end
+        rescue RestClient::Unauthorized,
+          Errno::EHOSTUNREACH,
+          Errno::ECONNREFUSED => e
+          raise CouchRest::Model::ConnectionFailed.new(e.to_s)
         end
 
         protected
diff --git a/lib/couchrest/model/errors.rb b/lib/couchrest/model/errors.rb
index 45d8b2f2..fe0ac592 100644
--- a/lib/couchrest/model/errors.rb
+++ b/lib/couchrest/model/errors.rb
@@ -20,6 +20,13 @@ def initialize(document)
       end
     end
 
+    # Raised when the connection to the couch server can not be established.
+    #
+    # The message will contain the corresponding error message from the
+    # underlying error.
+    #
+    class ConnectionFailed < CouchRestModelError; end
+
     class DocumentNotFound < Errors::CouchRestModelError; end
   end
 end