Skip to content

Commit 19b9884

Browse files
committed
Merge pull request #221
2 parents 0a08b03 + a287ded commit 19b9884

File tree

6 files changed

+153
-2
lines changed

6 files changed

+153
-2
lines changed

php_phongo.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,12 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, const p
618618
if (!mongoc_cursor_next(cursor, &doc)) {
619619
bson_error_t error;
620620

621+
/* Check for connection related exceptions */
622+
if (EG(exception)) {
623+
mongoc_cursor_destroy(cursor);
624+
return false;
625+
}
626+
621627
/* Could simply be no docs, which is not an error */
622628
if (mongoc_cursor_error(cursor, &error)) {
623629
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
@@ -652,6 +658,12 @@ int phongo_execute_command(mongoc_client_t *client, const char *db, const bson_t
652658
if (!mongoc_cursor_next(cursor, &doc)) {
653659
bson_error_t error;
654660

661+
/* Check for connection related exceptions */
662+
if (EG(exception)) {
663+
mongoc_cursor_destroy(cursor);
664+
return false;
665+
}
666+
655667
if (mongoc_cursor_error(cursor, &error)) {
656668
mongoc_cursor_destroy(cursor);
657669
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
@@ -1086,6 +1098,7 @@ bool php_phongo_ssl_verify(php_stream *stream, const char *hostname, bson_error_
10861098

10871099
mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_host_list_t *host, void *user_data, bson_error_t *error) /* {{{ */
10881100
{
1101+
zend_error_handling error_handling;
10891102
php_phongo_stream_socket *base_stream = NULL;
10901103
php_stream *stream = NULL;
10911104
const bson_t *options;
@@ -1138,7 +1151,9 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
11381151
spprintf(&uniqid, 0, "%s:%d[%s]", host->host, host->port, mongoc_uri_get_string(uri));
11391152

11401153
MONGOC_DEBUG("Connecting to '%s'", uniqid);
1154+
zend_replace_error_handling(EH_SUPPRESS, NULL, &error_handling TSRMLS_CC);
11411155
stream = php_stream_xport_create(dsn, dsn_len, 0, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, uniqid, timeoutp, (php_stream_context *)user_data, &errmsg, &errcode);
1156+
zend_restore_error_handling(&error_handling TSRMLS_CC);
11421157

11431158
if (!stream) {
11441159
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Failed connecting to '%s:%d': %s", host->host, host->port, phongo_str(errmsg));
@@ -1155,8 +1170,6 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
11551170
efree(uniqid);
11561171

11571172
if (mongoc_uri_get_ssl(uri)) {
1158-
zend_error_handling error_handling;
1159-
11601173
zend_replace_error_handling(EH_THROW, php_phongo_sslconnectionexception_ce, &error_handling TSRMLS_CC);
11611174

11621175
MONGOC_DEBUG("Enabling SSL");

src/MongoDB/Manager.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ PHP_METHOD(Manager, selectServer)
284284
phongo_server_init(return_value, intern->client, selected_server->id TSRMLS_CC);
285285
mongoc_server_description_destroy(selected_server);
286286
} else {
287+
/* Check for connection related exceptions */
288+
if (EG(exception)) {
289+
return;
290+
}
291+
287292
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "%s", error.message);
288293
}
289294
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeBulkWrite() should not issue warning before exception
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$bulk = new MongoDB\Driver\BulkWrite;
10+
$bulk->insert(['x' => 1]);
11+
12+
// Invalid host cannot be resolved
13+
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);
14+
15+
echo throws(function() use ($manager, $bulk) {
16+
$manager->executeBulkWrite(NS, $bulk);
17+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
18+
19+
// Valid host refuses connection
20+
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);
21+
22+
echo throws(function() use ($manager, $bulk) {
23+
$manager->executeBulkWrite(NS, $bulk);
24+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECTF--
30+
OK: Got MongoDB\Driver\Exception\RuntimeException
31+
No suitable servers found (`serverselectiontryonce` set): %s
32+
OK: Got MongoDB\Driver\Exception\RuntimeException
33+
No suitable servers found (`serverselectiontryonce` set): %s
34+
===DONE===
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeCommand() should not issue warning before exception
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$command = new MongoDB\Driver\Command(['ping' => 1]);
10+
11+
// Invalid host cannot be resolved
12+
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);
13+
14+
echo throws(function() use ($manager, $command) {
15+
$manager->executeCommand(DATABASE_NAME, $command);
16+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
17+
18+
// Valid host refuses connection
19+
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);
20+
21+
echo throws(function() use ($manager, $command) {
22+
$manager->executeCommand(DATABASE_NAME, $command);
23+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECTF--
29+
OK: Got MongoDB\Driver\Exception\RuntimeException
30+
No suitable servers found (`serverselectiontryonce` set): %s
31+
OK: Got MongoDB\Driver\Exception\RuntimeException
32+
No suitable servers found (`serverselectiontryonce` set): %s
33+
===DONE===
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeQuery() should not issue warning before exception
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$query = new MongoDB\Driver\Query([]);
10+
11+
// Invalid host cannot be resolved
12+
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);
13+
14+
echo throws(function() use ($manager, $query) {
15+
$manager->executeQuery(NS, $query);
16+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
17+
18+
// Valid host refuses connection
19+
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);
20+
21+
echo throws(function() use ($manager, $query) {
22+
$manager->executeQuery(NS, $query);
23+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECTF--
29+
OK: Got MongoDB\Driver\Exception\RuntimeException
30+
No suitable servers found (`serverselectiontryonce` set): %s
31+
OK: Got MongoDB\Driver\Exception\RuntimeException
32+
No suitable servers found (`serverselectiontryonce` set): %s
33+
===DONE===
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::selectServer() should not issue warning before exception
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
10+
11+
// Invalid host cannot be resolved
12+
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);
13+
14+
echo throws(function() use ($manager, $rp) {
15+
$manager->selectServer($rp);
16+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
17+
18+
// Valid host refuses connection
19+
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);
20+
21+
echo throws(function() use ($manager, $rp) {
22+
$manager->selectServer($rp);
23+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECTF--
29+
OK: Got MongoDB\Driver\Exception\RuntimeException
30+
No suitable servers found (`serverselectiontryonce` set): %s
31+
OK: Got MongoDB\Driver\Exception\RuntimeException
32+
No suitable servers found (`serverselectiontryonce` set): %s
33+
===DONE===

0 commit comments

Comments
 (0)