@@ -24,7 +24,8 @@ class Server
24
24
* @param array $options Supply either a string "url" parameter OR a
25
25
* \GuzzleHttp\ClientInterface "client" parameter if more configuration
26
26
* is required
27
- * @throws \PHPCouchDB\Exception\ServerException if there's a problem with parsing arguments or creating the client
27
+ * @throws \PHPCouchDB\Exception\ServerException if there's a problem
28
+ * with parsing arguments or connecting to the database
28
29
*/
29
30
public function __construct (array $ options )
30
31
{
@@ -35,20 +36,26 @@ public function __construct(array $options)
35
36
}
36
37
37
38
if (isset ($ options ['client ' ]) && $ options ['client ' ] instanceof \GuzzleHttp \ClientInterface) {
38
- $ this -> client = $ options ['client ' ];
39
+ $ client = $ options ['client ' ];
39
40
} elseif (isset ($ options ['url ' ])) {
40
- try {
41
- $ this ->client = new \GuzzleHttp \Client (["base_uri " => $ options ['url ' ]]);
42
- } catch (Exception $ e ) {
43
- throw new \PHPCouchDB \Exception \ServerException (
44
- "Could not connect with URL. Error: " . $ e ->getMessage ()
45
- );
46
- }
41
+ $ client = new \GuzzleHttp \Client (["base_uri " => $ options ['url ' ]]);
47
42
} else {
48
43
throw new \PHPCouchDB \Exception \ServerException (
49
44
'Failed to parse $options, array should contain either a url or a client '
50
45
);
51
46
}
47
+
48
+ // try to connect as well
49
+ try {
50
+ $ client ->get ('/ ' );
51
+ $ this ->client = $ client ;
52
+ } catch (\GuzzleHttp \Exception \ConnectException $ e ) {
53
+ throw new \PHPCouchDB \Exception \ServerException (
54
+ "Could not connect to database. Error: " . $ e ->getMessage (),
55
+ 0 , $ e
56
+ );
57
+ }
58
+
52
59
}
53
60
54
61
/**
0 commit comments