Skip to content

PHPC-1387: Consider error document for cursor exceptions #983

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

Merged
merged 2 commits into from
May 31, 2019
Merged
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
10 changes: 5 additions & 5 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void phongo_throw_exception_from_bson_error_t(bson_error_t* error TSRMLS_DC)
zend_throw_exception(phongo_exception_from_mongoc_domain(error->domain, error->code), error->message, error->code TSRMLS_CC);
}

void phongo_throw_exception_from_bson_error_and_reply_t(bson_error_t* error, bson_t* reply TSRMLS_DC)
void phongo_throw_exception_from_bson_error_t_and_reply(bson_error_t* error, const bson_t* reply TSRMLS_DC)
{
/* Server errors (other than ExceededTimeLimit) and write concern errors
* may use CommandException and report the result document for the
Expand Down Expand Up @@ -737,7 +737,7 @@ bool phongo_execute_bulk_write(mongoc_client_t* client, const char* namespace, p
* returned and an exception is thrown. */
bool phongo_cursor_advance_and_check_for_error(mongoc_cursor_t* cursor TSRMLS_DC) /* {{{ */
{
const bson_t* doc;
const bson_t* doc = NULL;

if (!mongoc_cursor_next(cursor, &doc)) {
bson_error_t error = { 0 };
Expand All @@ -748,8 +748,8 @@ bool phongo_cursor_advance_and_check_for_error(mongoc_cursor_t* cursor TSRMLS_DC
}

/* Could simply be no docs, which is not an error */
if (mongoc_cursor_error(cursor, &error)) {
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
if (mongoc_cursor_error_document(cursor, &error, &doc)) {
phongo_throw_exception_from_bson_error_t_and_reply(&error, doc TSRMLS_CC);
return false;
}
}
Expand Down Expand Up @@ -966,7 +966,7 @@ bool phongo_execute_command(mongoc_client_t* client, php_phongo_command_type_t t
free_reply = true;

if (!result) {
phongo_throw_exception_from_bson_error_and_reply_t(&error, &reply TSRMLS_CC);
phongo_throw_exception_from_bson_error_t_and_reply(&error, &reply TSRMLS_CC);
goto cleanup;
}

Expand Down
2 changes: 1 addition & 1 deletion php_phongo.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void phongo_throw_exception(php_phongo_error_domain_t domain TSRMLS
#endif /* PHP_VERSION_ID < 70000 */
;
void phongo_throw_exception_from_bson_error_t(bson_error_t* error TSRMLS_DC);
void phongo_throw_exception_from_bson_error_and_reply_t(bson_error_t* error, bson_t* reply TSRMLS_DC);
void phongo_throw_exception_from_bson_error_t_and_reply(bson_error_t* error, const bson_t* reply TSRMLS_DC);

/* This enum is used for processing options in phongo_execute_parse_options and
* selecting a libmongoc function to use in phongo_execute_command. The values
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB/Session.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ static PHP_METHOD(Session, commitTransaction)
}

if (!mongoc_client_session_commit_transaction(intern->client_session, &reply, &error)) {
phongo_throw_exception_from_bson_error_and_reply_t(&error, &reply TSRMLS_CC);
phongo_throw_exception_from_bson_error_t_and_reply(&error, &reply TSRMLS_CC);
bson_destroy(&reply);
}
} /* }}} */
Expand Down
30 changes: 30 additions & 0 deletions tests/manager/manager-executeQuery_error-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
MongoDB\Driver\Manager::executeQuery() exposes error document via CommandException
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_live(); ?>
<?php skip_if_server_version('<', '3.2'); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$manager = new MongoDB\Driver\Manager(URI);
$query = new MongoDB\Driver\Query(['$foo' => 1]);

try {
$manager->executeQuery(NS, $query);
} catch (\MongoDB\Driver\Exception\CommandException $e) {
printf("%s(%d): %s\n", get_class($e), $e->getCode(), $e->getMessage());
$doc = $e->getResultDocument();
var_dump($doc->errmsg === $e->getMessage());
var_dump($doc->code === $e->getCode());
}

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
MongoDB\Driver\Exception\CommandException(2): unknown top level operator: $foo
bool(true)
bool(true)
===DONE===