Skip to content

Commit 4b70d9b

Browse files
committed
PHPC-578: Expose result document for failed commands
1 parent 14726af commit 4b70d9b

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

php_phongo.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,25 @@ int phongo_execute_command(mongoc_client_t* client, php_phongo_command_type_t ty
853853
return false;
854854
}
855855
if (!result) {
856-
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
856+
if (error.domain == MONGOC_ERROR_SERVER || error.domain == MONGOC_ERROR_WRITE_CONCERN) {
857+
#if PHP_VERSION_ID >= 70000
858+
zval zv;
859+
#else
860+
zval* zv;
861+
#endif
862+
863+
zend_throw_exception(php_phongo_commandexception_ce, error.message, error.code TSRMLS_CC);
864+
php_phongo_bson_to_zval(bson_get_data(&reply), reply.len, &zv);
865+
866+
#if PHP_VERSION_ID >= 70000
867+
phongo_add_exception_prop(ZEND_STRL("resultDocument"), &zv);
868+
#else
869+
phongo_add_exception_prop(ZEND_STRL("resultDocument"), zv TSRMLS_CC);
870+
#endif
871+
zval_ptr_dtor(&zv);
872+
} else {
873+
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
874+
}
857875
bson_destroy(&reply);
858876
bson_destroy(&opts);
859877
return false;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeWriteCommand() throws CommandException for invalid writeConcern
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php NEEDS('REPLICASET'); CLEANUP(REPLICASET); ?>
6+
--FILE--
7+
<?php
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
$manager = new MongoDB\Driver\Manager(REPLICASET);
11+
12+
$command = new MongoDB\Driver\Command([
13+
'findAndModify' => COLLECTION_NAME,
14+
'query' => ['_id' => 'foo'],
15+
'update' => ['foo' => ['bar']],
16+
'upsert' => true,
17+
'new' => true,
18+
]);
19+
20+
try {
21+
$manager->executeWriteCommand(DATABASE_NAME, $command, ['writeConcern' => new MongoDB\Driver\WriteConcern("undefined")]);
22+
} catch (MongoDB\Driver\Exception\CommandException $e) {
23+
printf("%s(%d): %s\n", get_class($e), $e->getCode(), $e->getMessage());
24+
var_dump($e->getResultDocument());
25+
}
26+
27+
?>
28+
===DONE===
29+
<?php exit(0); ?>
30+
--EXPECTF--
31+
MongoDB\Driver\Exception\CommandException(79): Write Concern error: No write concern mode named 'undefined' found in replica set configuration
32+
object(stdClass)#%d (%d) {
33+
["lastErrorObject"]=>
34+
object(stdClass)#%d (3) {
35+
["n"]=>
36+
int(1)
37+
["updatedExisting"]=>
38+
bool(false)
39+
["upserted"]=>
40+
string(3) "foo"
41+
}
42+
["value"]=>
43+
object(stdClass)#%d (2) {
44+
["_id"]=>
45+
string(3) "foo"
46+
["foo"]=>
47+
array(1) {
48+
[0]=>
49+
string(3) "bar"
50+
}
51+
}
52+
["writeConcernError"]=>
53+
object(stdClass)#%d (3) {
54+
["code"]=>
55+
int(79)
56+
["codeName"]=>
57+
string(23) "UnknownReplWriteConcern"
58+
["errmsg"]=>
59+
string(74) "No write concern mode named 'undefined' found in replica set configuration"
60+
}
61+
["ok"]=>
62+
float(1)%A
63+
}
64+
===DONE===
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeWriteCommand() throws CommandException for unsupported update operator
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php NEEDS('STANDALONE'); ?>
6+
--FILE--
7+
<?php
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
$manager = new MongoDB\Driver\Manager(STANDALONE);
11+
12+
$command = new MongoDB\Driver\Command([
13+
'findAndModify' => COLLECTION_NAME,
14+
'query' => ['_id' => 'foo'],
15+
'upsert' => true,
16+
'new' => true,
17+
]);
18+
19+
try {
20+
$manager->executeWriteCommand(DATABASE_NAME, $command);
21+
} catch (MongoDB\Driver\Exception\CommandException $e) {
22+
printf("%s(%d): %s\n", get_class($e), $e->getCode(), $e->getMessage());
23+
var_dump($e->getResultDocument());
24+
}
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECT--
30+
MongoDB\Driver\Exception\CommandException(9): Either an update or remove=true must be specified
31+
object(stdClass)#4 (4) {
32+
["ok"]=>
33+
float(0)
34+
["errmsg"]=>
35+
string(49) "Either an update or remove=true must be specified"
36+
["code"]=>
37+
int(9)
38+
["codeName"]=>
39+
string(13) "FailedToParse"
40+
}
41+
===DONE===

0 commit comments

Comments
 (0)