Skip to content

Commit 5a3767e

Browse files
committed
PHPC-2344 Do not use BulkWriteException for client-side errors
1 parent e8cc2e5 commit 5a3767e

File tree

2 files changed

+6
-42
lines changed

2 files changed

+6
-42
lines changed

src/phongo_execute.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ bool phongo_execute_bulk_write(zval* manager, const char* namespace, php_phongo_
296296
/* A BulkWriteException is always thrown if mongoc_bulk_operation_execute()
297297
* fails to ensure that the write result is accessible. If the error does
298298
* not originate from the server (e.g. socket error), throw the appropriate
299-
* exception first. It will be included in BulkWriteException's message and
300-
* will also be accessible via Exception::getPrevious(). */
299+
* exception first. It will be thrown directly */
301300
if (!success) {
302301
if (error.domain != MONGOC_ERROR_SERVER && error.domain != MONGOC_ERROR_WRITE_CONCERN) {
303302
phongo_throw_exception_from_bson_error_t_and_reply(&error, &reply);
@@ -311,11 +310,7 @@ bool phongo_execute_bulk_write(zval* manager, const char* namespace, php_phongo_
311310
}
312311

313312
if (EG(exception)) {
314-
char* message;
315-
316-
(void) spprintf(&message, 0, "Bulk write failed due to previous %s: %s", PHONGO_ZVAL_EXCEPTION_NAME(EG(exception)), error.message);
317-
zend_throw_exception(php_phongo_bulkwriteexception_ce, message, 0);
318-
efree(message);
313+
zend_throw_exception(phongo_exception_from_phongo_domain(error.domain), error.message, error.code);
319314
} else {
320315
zend_throw_exception(php_phongo_bulkwriteexception_ce, error.message, error.code);
321316
}

tests/manager/manager-executeBulkWrite_error-005.phpt

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,14 @@ $bulk->insert(['x' => 1]);
2424
$bulk->update(['x' => 1], ['$set' => ['y' => 1]]);
2525
$bulk->delete(['x' => 1]);
2626

27-
try {
27+
echo throws(function() use ($server, $bulk) {
2828
$server->executeBulkWrite(NS, $bulk);
29-
} catch (MongoDB\Driver\Exception\BulkWriteException $e) {
30-
printf("%s(%d): %s\n", get_class($e), $e->getCode(), $e->getMessage());
31-
$prev = $e->getPrevious();
32-
printf("%s(%d): %s\n", get_class($prev), $prev->getCode(), $prev->getMessage());
33-
var_dump($e->getWriteResult());
34-
}
29+
}, MongoDB\Driver\Exception\ConnectionTimeoutException::class), "\n";
3530

3631
?>
3732
===DONE===
3833
<?php exit(0); ?>
3934
--EXPECTF--
40-
MongoDB\Driver\Exception\BulkWriteException(0): Bulk write failed due to previous MongoDB\Driver\Exception\ConnectionTimeoutException: Failed to send "delete" command with database "%s": Failed to read 4 bytes: socket error or timeout
41-
MongoDB\Driver\Exception\ConnectionTimeoutException(%d): Failed to send "delete" command with database "%s": Failed to read 4 bytes: socket error or timeout
42-
object(MongoDB\Driver\WriteResult)#%d (%d) {
43-
["nInserted"]=>
44-
int(1)
45-
["nMatched"]=>
46-
int(1)
47-
["nModified"]=>
48-
int(1)
49-
["nRemoved"]=>
50-
int(0)
51-
["nUpserted"]=>
52-
int(0)
53-
["upsertedIds"]=>
54-
array(0) {
55-
}
56-
["writeErrors"]=>
57-
array(0) {
58-
}
59-
["writeConcernError"]=>
60-
NULL
61-
["writeConcern"]=>
62-
object(MongoDB\Driver\WriteConcern)#%d (%d) {
63-
}
64-
["errorReplies"]=>
65-
array(0) {
66-
}
67-
}
35+
OK: Got MongoDB\Driver\Exception\ConnectionTimeoutException
36+
Failed to send "delete" command with database "%s": Failed to read 4 bytes: socket error or timeout
6837
===DONE===

0 commit comments

Comments
 (0)