diff --git a/php_phongo.c b/php_phongo.c index f5f5a9b9a..fe2a7ad01 100644 --- a/php_phongo.c +++ b/php_phongo.c @@ -853,7 +853,25 @@ int phongo_execute_command(mongoc_client_t* client, php_phongo_command_type_t ty return false; } if (!result) { - phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC); + if (error.domain == MONGOC_ERROR_SERVER || error.domain == MONGOC_ERROR_WRITE_CONCERN) { +#if PHP_VERSION_ID >= 70000 + zval zv; +#else + zval* zv; +#endif + + zend_throw_exception(php_phongo_commandexception_ce, error.message, error.code TSRMLS_CC); + php_phongo_bson_to_zval(bson_get_data(&reply), reply.len, &zv); + +#if PHP_VERSION_ID >= 70000 + phongo_add_exception_prop(ZEND_STRL("resultDocument"), &zv); +#else + phongo_add_exception_prop(ZEND_STRL("resultDocument"), zv TSRMLS_CC); +#endif + zval_ptr_dtor(&zv); + } else { + phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC); + } bson_destroy(&reply); bson_destroy(&opts); return false; diff --git a/tests/manager/manager-executeWriteCommand_error-002.phpt b/tests/manager/manager-executeWriteCommand_error-002.phpt new file mode 100644 index 000000000..a0b1ff9a5 --- /dev/null +++ b/tests/manager/manager-executeWriteCommand_error-002.phpt @@ -0,0 +1,64 @@ +--TEST-- +MongoDB\Driver\Manager::executeWriteCommand() throws CommandException for invalid writeConcern +--SKIPIF-- + + +--FILE-- + COLLECTION_NAME, + 'query' => ['_id' => 'foo'], + 'update' => ['foo' => ['bar']], + 'upsert' => true, + 'new' => true, +]); + +try { + $manager->executeWriteCommand(DATABASE_NAME, $command, ['writeConcern' => new MongoDB\Driver\WriteConcern("undefined")]); +} catch (MongoDB\Driver\Exception\CommandException $e) { + printf("%s(%d): %s\n", get_class($e), $e->getCode(), $e->getMessage()); + var_dump($e->getResultDocument()); +} + +?> +===DONE=== + +--EXPECTF-- +MongoDB\Driver\Exception\CommandException(79): Write Concern error: No write concern mode named 'undefined' found in replica set configuration +object(stdClass)#%d (%d) { + ["lastErrorObject"]=> + object(stdClass)#%d (3) { + ["n"]=> + int(1) + ["updatedExisting"]=> + bool(false) + ["upserted"]=> + string(3) "foo" + } + ["value"]=> + object(stdClass)#%d (2) { + ["_id"]=> + string(3) "foo" + ["foo"]=> + array(1) { + [0]=> + string(3) "bar" + } + } + ["writeConcernError"]=> + object(stdClass)#%d (3) { + ["code"]=> + int(79) + ["codeName"]=> + string(23) "UnknownReplWriteConcern" + ["errmsg"]=> + string(74) "No write concern mode named 'undefined' found in replica set configuration" + } + ["ok"]=> + float(1)%A +} +===DONE=== diff --git a/tests/manager/manager-executeWriteCommand_error-003.phpt b/tests/manager/manager-executeWriteCommand_error-003.phpt new file mode 100644 index 000000000..3339a710a --- /dev/null +++ b/tests/manager/manager-executeWriteCommand_error-003.phpt @@ -0,0 +1,41 @@ +--TEST-- +MongoDB\Driver\Manager::executeWriteCommand() throws CommandException for unsupported update operator +--SKIPIF-- + + +--FILE-- + COLLECTION_NAME, + 'query' => ['_id' => 'foo'], + 'upsert' => true, + 'new' => true, +]); + +try { + $manager->executeWriteCommand(DATABASE_NAME, $command); +} catch (MongoDB\Driver\Exception\CommandException $e) { + printf("%s(%d): %s\n", get_class($e), $e->getCode(), $e->getMessage()); + var_dump($e->getResultDocument()); +} + +?> +===DONE=== + +--EXPECT-- +MongoDB\Driver\Exception\CommandException(9): Either an update or remove=true must be specified +object(stdClass)#4 (4) { + ["ok"]=> + float(0) + ["errmsg"]=> + string(49) "Either an update or remove=true must be specified" + ["code"]=> + int(9) + ["codeName"]=> + string(13) "FailedToParse" +} +===DONE===