Skip to content

Add details to the generic BulkWriteCommandException message #1821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/phongo_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ bool phongo_execute_bulkwritecommand(zval* manager, php_phongo_bulkwritecommand_
(void) spprintf(&message, 0, "Bulk write failed due to previous %s: %s", PHONGO_ZVAL_EXCEPTION_NAME(EG(exception)), error.message);
zend_throw_exception(php_phongo_bulkwritecommandexception_ce, message, 0);
efree(message);
} else if (!has_top_level_error && bw_ret.res) {
zend_throw_exception(php_phongo_bulkwritecommandexception_ce, "Bulk write failed with %d write concern errors and %d write errors", error.code, bw_ret.res->n_write_concern_errors, bw_ret.res->n_write_errors);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alcaeus I don't know how to debug this error.

src/phongo_execute.c:444:158: error: incomplete definition of type 'mongoc_bulkwriteresult_t' (aka 'struct _mongoc_bulkwriteresult_t')
  444 |                         zend_throw_exception(php_phongo_bulkwritecommandexception_ce, "Bulk write failed with %d write concern errors and %d write errors", error.code, bw_ret.res->n_write_concern_errors, bw_ret.res->n_write_errors);
      |                                                                                                                                                                         ~~~~~~~~~~^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because of how the mongoc_bulkwriteresult_t is defined. While mongoc_bulkwriteresult_t is defined in the .h file (which we include), it points to a struct that is defined in the .c file. This means that you can access members of a variable in the C file, but not outside of it, for example here as the internal _mongoc_bulkwriteresult_t struct doesn't exist. This makes up for the fact that there are no visibilities for structural members in C, and this way libmongoc limits the scope of its external API.

To access struct fields, libmongoc exposes some functions to extract data from a mongoc_bulkwriteresult_t, but notably it doesn't expose the errorscount field. The n_write_concern_errors and n_write_errors field don't exist in the struct at all.

IMO, we should tell people to check the exception object for error details for the time being; we can then discuss whether the C team can add an API to expose the total number of errors.

Copy link
Member Author

@GromNaN GromNaN May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BulkWriteCommandException class exposes getWriteErrors and getWriteConcernErrors. It should be possible to count this values to inject this counts in the exception message. Or maybe wrap this exception in PHPLIB to improve the message.

} else {
zend_throw_exception(php_phongo_bulkwritecommandexception_ce, has_top_level_error ? error.message : "Bulk write failed", error.code);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/bulkwritecommand/bulkwritecommandexception-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
MongoDB\Driver\BulkWriteCommandResult::isAcknowledged() with unacknowledged write concern
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_live(); ?>
<?php skip_if_server_version('<', '8.0'); ?>
<?php skip_if_not_clean(); ?>
--FILE--
<?php

require_once __DIR__ . "/../utils/basic.inc";

$manager = create_test_manager(URI);

$bulk = new MongoDB\Driver\BulkWriteCommand(['ordered' => false]);
$bulk->insertOne(NS, ['_id' => 1]);
$bulk->insertOne(NS, ['_id' => 1]);

echo throws(function() use ($result) {
$manager->executeBulkWriteCommand($bulk);
}, MongoDB\Driver\Exception\BulkWriteCommandException::class), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
bool(false)
OK: Got MongoDB\Driver\Exception\BulkWriteCommandException
Bulk write failed with 0 write concern errors and 1 write errors
===DONE===
Loading