Skip to content
This repository was archived by the owner on Apr 20, 2022. It is now read-only.

Commit 90de63d

Browse files
committed
PCBC-236: Throw exceptions from constructor
Change-Id: Icc79789124e0422e34ad119cb79f988d817a916a Reviewed-on: http://review.couchbase.org/26666 Reviewed-by: Matt Ingenthron <matt@couchbase.com> Tested-by: Trond Norbye <trond.norbye@gmail.com>
1 parent 5abf94f commit 90de63d

File tree

2 files changed

+40
-47
lines changed

2 files changed

+40
-47
lines changed

create.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,6 @@ void php_couchbase_create_impl(INTERNAL_FUNCTION_PARAMETERS, int oo)
387387
}
388388
}
389389

390-
if ((retval = lcb_connect(handle)) != LCB_SUCCESS) {
391-
php_error(E_WARNING,
392-
"Failed to connect libcouchbase to the server: %s",
393-
lcb_strerror(handle, retval));
394-
}
395-
396390
php_couchbase_setup_callbacks(handle);
397391

398392
couchbase_res = calloc(1, sizeof(php_couchbase_res));
@@ -408,25 +402,33 @@ void php_couchbase_create_impl(INTERNAL_FUNCTION_PARAMETERS, int oo)
408402
couchbase_res->compressor = COUCHBASE_G(compressor_real);
409403
couchbase_res->ignoreflags = 0;
410404

411-
lcb_set_cookie(handle, (const void *)couchbase_res);
405+
lcb_set_cookie(handle, couchbase_res);
412406

413-
/* wait for the connection established */
414-
if (LCB_SUCCESS == retval) { /* earlier lcb_connect's retval */
415-
lcb_wait(handle);
416-
}
407+
lcb_behavior_set_syncmode(handle, LCB_SYNCHRONOUS);
408+
if ((retval = lcb_connect(handle)) != LCB_SUCCESS) {
409+
if (couchbase_res->errinfo != NULL) {
410+
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
411+
cb_lcb_exception,
412+
"Failed to connect libcouchbase to the server: %s (%s)",
413+
lcb_strerror(handle, retval), couchbase_res->errinfo);
414+
} else {
415+
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
416+
cb_lcb_exception,
417+
"Failed to connect libcouchbase to the server: %s",
418+
lcb_strerror(handle, retval));
419+
}
420+
lcb_destroy(handle);
421+
free(couchbase_res->bucket);
422+
free(couchbase_res);
423+
free_connparams(&cparams);
417424

418-
couchbase_res->seqno = 0;
419-
if (LCB_SUCCESS != (retval = lcb_get_last_error(handle))) {
420-
couchbase_res->rc = retval;
421-
couchbase_res->is_connected = 0;
422-
php_error(E_WARNING,
423-
"Failed to establish libcouchbase connection to server: %s",
424-
lcb_strerror(handle, retval));
425-
} else {
426-
couchbase_res->is_connected = 1;
425+
return ;
427426
}
428427

429-
if (persistent && couchbase_res->is_connected) {
428+
lcb_behavior_set_syncmode(handle, LCB_ASYNCHRONOUS);
429+
couchbase_res->seqno = 0;
430+
couchbase_res->is_connected = 1;
431+
if (persistent) {
430432
zend_rsrc_list_entry le;
431433
Z_TYPE(le) = le_pcouchbase;
432434
le.ptr = couchbase_res;

tests/Connection.inc

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,9 @@ class Connection extends CouchbaseTestCommon
116116
'bad-password',
117117
'does-not-exist');
118118

119-
} catch (Exception $e) {
119+
} catch (CouchbaseLibcouchbaseException $e) {
120120
$this->assertRegExp('/failed/i', $e->getMessage());
121121
}
122-
123-
// now, make sure the object would support the given operations:
124-
$this->setErrorsRaiseException(false);
125-
126-
$oo = new Couchbase('http://999.999.999.999:9999',
127-
'bad-user',
128-
'bad-password',
129-
'does-not-exist');
130-
131-
$this->assertGreaterThan(0, $oo->getResultCode());
132-
$this->assertNotNull($oo->getResultMessage());
133-
$this->assertNotEquals("", $oo->getResultMessage());
134-
135-
$this->setErrorsRaiseException(true);
136122
}
137123

138124
function testConnectUri() {
@@ -170,7 +156,7 @@ class Connection extends CouchbaseTestCommon
170156
array($url),
171157
"/failed/i");
172158

173-
$this->assertExceptionRegexp(function ($u) { return new Couchbase($u); },
159+
$this->assertExceptionRegexp(function ($u) { return new Couchbase($u); },
174160
array($url),
175161
"/failed/i");
176162

@@ -179,15 +165,18 @@ class Connection extends CouchbaseTestCommon
179165
$h = couchbase_connect($url);
180166
$this->assertFalse($h);
181167

182-
$o = new Couchbase($url);
183-
$this->assertEquals(24, $o->getResultCode()); // 24 = 0x18 = LCB_CONNECT_ERROR
184-
$this->assertContains('Connection', $o->getResultMessage());
168+
try {
169+
$o = new Couchbase($url);
170+
$this->assertNull("We should fail to connect");
171+
} catch (CouchbaseLibcouchbaseException $e) {
172+
$this->assertRegexp('/connect/i', $e->getMessage());
173+
}
185174

186175
# Try with a malformed uri
187176

188-
try {
189-
$cb = new Couchbase(array("http://"));
190-
$his->assertNull("We should detect bogus url");
177+
try {
178+
$cb = new Couchbase(array("http://"));
179+
$this->assertNull("We should detect bogus url");
191180
} catch (CouchbaseException $e) {
192181
$this->assertRegexp('/malformed/i', $e->getMessage());
193182
}
@@ -201,11 +190,13 @@ class Connection extends CouchbaseTestCommon
201190
$h = couchbase_connect($hosts);
202191
$this->assertFalse($h);
203192

204-
$o = new Couchbase($hosts);
205-
$this->assertEquals(24, $o->getResultCode()); // see note above re: 24
206-
$this->assertContains('Connection', $o->getResultMessage());
193+
try {
194+
$o = new Couchbase($hosts);
195+
$this->assertNull("We should fail to connect");
196+
} catch (CouchbaseLibcouchbaseException $e) {
197+
$this->assertRegexp('/connect/i', $e->getMessage());
198+
}
207199
}
208-
209200
}
210201

211202
/**

0 commit comments

Comments
 (0)