Skip to content

Commit 490e565

Browse files
committed
Manager constructor should check if URI or client creation fails
If the URI cannot be parsed, we should throw an InvalidArgumentException. Failing to initialize the client from a valid URI can remain a RuntimeException.
1 parent 47619db commit 490e565

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

php_phongo.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,10 @@ mongoc_uri_t *php_phongo_make_uri(const char *uri_string, bson_t *options TSRMLS
15041504
uri = mongoc_uri_new(uri_string);
15051505
MONGOC_DEBUG("Connection string: '%s'", uri_string);
15061506

1507+
if (!uri) {
1508+
return NULL;
1509+
}
1510+
15071511
if (options && bson_iter_init(&iter, options)) {
15081512
while (bson_iter_next (&iter)) {
15091513
const char *key = bson_iter_key(&iter);
@@ -1621,7 +1625,7 @@ mongoc_client_t *php_phongo_make_mongo_client(const mongoc_uri_t *uri, zval *dri
16211625
client = mongoc_client_new_from_uri(uri);
16221626

16231627
if (!client) {
1624-
RETURN(false);
1628+
RETURN(NULL);
16251629
}
16261630

16271631

src/MongoDB/Manager.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,24 @@ PHP_METHOD(Manager, __construct)
8181
zval_to_bson(options, PHONGO_BSON_NONE, &bson_options, NULL TSRMLS_CC);
8282
}
8383

84-
uri = php_phongo_make_uri (uri_string, &bson_options TSRMLS_CC);
85-
if (uri) {
86-
intern->client = php_phongo_make_mongo_client(uri, driverOptions TSRMLS_CC);
87-
mongoc_uri_destroy(uri);
88-
} else {
89-
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "%s", "Failed to parse MongoDB URI");
84+
if (!(uri = php_phongo_make_uri(uri_string, &bson_options TSRMLS_CC))) {
85+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Failed to parse MongoDB URI: '%s'", uri_string);
86+
bson_destroy(&bson_options);
87+
88+
return;
9089
}
90+
91+
intern->client = php_phongo_make_mongo_client(uri, driverOptions TSRMLS_CC);
92+
mongoc_uri_destroy(uri);
93+
9194
if (!intern->client) {
92-
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "%s", "Failed to create Manager from URI");
95+
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "Failed to create Manager from URI: '%s'", uri_string);
96+
bson_destroy(&bson_options);
97+
98+
return;
9399
}
94100

101+
bson_destroy(&bson_options);
95102
}
96103
/* }}} */
97104
/* {{{ proto MongoDB\Driver\Cursor Manager::executeCommand(string $db, MongoDB\Driver\Command $command[, MongoDB\Driver\ReadPreference $readPreference = null])

0 commit comments

Comments
 (0)