Skip to content

Commit dc7135f

Browse files
committed
Throw BulkWriteCommandException directly for server errors
This also ensures that BulkWriteCommandException uses the original server-side error code and message (if available) for top-level errors.
1 parent 3928264 commit dc7135f

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/phongo_error.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void phongo_throw_exception_from_bson_error_t_and_reply(bson_error_t* error, con
189189
/* Server errors (other than ExceededTimeLimit) and write concern errors
190190
* may use CommandException and report the result document for the
191191
* failed command. For BC, ExceededTimeLimit errors will continue to use
192-
* ExcecutionTimeoutException and omit the result document. */
192+
* ExecutionTimeoutException and omit the result document. */
193193
if (reply && ((error->domain == MONGOC_ERROR_SERVER && error->code != PHONGO_SERVER_ERROR_EXCEEDED_TIME_LIMIT) || error->domain == MONGOC_ERROR_WRITE_CONCERN)) {
194194
zval zv;
195195

src/phongo_execute.c

+15-7
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,20 @@ bool phongo_execute_bulkwritecommand(zval* manager, php_phongo_bulkwritecommand_
408408
* BulkWriteCommandException is thrown.
409409
*/
410410
if (bw_ret.exc) {
411-
success = false;
412-
bson_error_t error = { 0 };
413-
const bson_t* error_reply = mongoc_bulkwriteexception_errorreply(bw_ret.exc);
414-
415-
// Consult any top-level error to throw the first exception
416-
if (mongoc_bulkwriteexception_error(bw_ret.exc, &error)) {
411+
success = false;
412+
bson_error_t error = { 0 };
413+
bool has_top_level_error = mongoc_bulkwriteexception_error(bw_ret.exc, &error);
414+
const bson_t* error_reply = mongoc_bulkwriteexception_errorreply(bw_ret.exc);
415+
416+
/* Throw an exception if there is a top-level error and it does not
417+
* originate from the server. Assuming we do not return early for an
418+
* InvalidArgumentException, this first exception will be accessible
419+
* via Exception::getPrevious().
420+
*
421+
* TODO: MONGOC_ERROR_WRITE_CONCERN should never be reported as a
422+
* top-level error by mongoc_bulkwrite_execute, so consider removing.
423+
*/
424+
if (has_top_level_error && error.domain != MONGOC_ERROR_SERVER && error.domain != MONGOC_ERROR_WRITE_CONCERN) {
417425
phongo_throw_exception_from_bson_error_t_and_reply(&error, error_reply);
418426
}
419427

@@ -433,7 +441,7 @@ bool phongo_execute_bulkwritecommand(zval* manager, php_phongo_bulkwritecommand_
433441
zend_throw_exception(php_phongo_bulkwritecommandexception_ce, message, 0);
434442
efree(message);
435443
} else {
436-
zend_throw_exception(php_phongo_bulkwritecommandexception_ce, "Bulk write failed", 0);
444+
zend_throw_exception(php_phongo_bulkwritecommandexception_ce, has_top_level_error ? error.message : "Bulk write failed", error.code);
437445
}
438446

439447
/* Initialize BulkWriteCommandException properties. Although a

0 commit comments

Comments
 (0)