Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit f89a3b1

Browse files
committed
Fixed HHVM-148: Add writeError and writeConcernError messages to BulkWrite exception message
1 parent 2052978 commit f89a3b1

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/MongoDB/Driver/WriteResult.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const StaticString
7777
Object hippo_write_result_init(mongoc_write_result_t *write_result, bson_error_t *error, mongoc_client_t *client, int server_id, int success, const mongoc_write_concern_t *write_concern)
7878
{
7979
static Class* c_writeResult;
80+
std::string message;
8081

8182
c_writeResult = Unit::lookupClass(s_MongoDriverWriteResult_className.get());
8283
assert(c_writeResult);
@@ -94,6 +95,10 @@ Object hippo_write_result_init(mongoc_write_result_t *write_result, bson_error_t
9495
obj->o_set(s_nModified, Variant((int64_t) write_result->nModified), s_MongoDriverWriteResult_className);
9596
obj->o_set(s_omit_nModified, Variant((int64_t) write_result->omit_nModified), s_MongoDriverWriteResult_className);
9697

98+
if (!success) {
99+
message = "BulkWrite error";
100+
}
101+
97102
if (write_concern) {
98103
Array debugInfoResult = Array::Create();
99104
mongodb_driver_add_write_concern_debug((mongoc_write_concern_t*) write_concern, &debugInfoResult);
@@ -135,6 +140,8 @@ Object hippo_write_result_init(mongoc_write_result_t *write_result, bson_error_t
135140

136141
if (a_we.exists(s_errmsg)) {
137142
we_obj->o_set(s_message, a_we[s_errmsg], s_MongoDriverWriteError_className);
143+
message += " :: ";
144+
message.append(a_we[s_errmsg].toString().c_str());
138145
}
139146
if (a_we.exists(s_code)) {
140147
we_obj->o_set(s_code, a_we[s_code], s_MongoDriverWriteError_className);
@@ -173,6 +180,8 @@ Object hippo_write_result_init(mongoc_write_result_t *write_result, bson_error_t
173180

174181
if (first_item.exists(s_errmsg)) {
175182
wce_obj->o_set(s_message, first_item[s_errmsg], s_MongoDriverWriteConcernError_className);
183+
message += " :: ";
184+
message.append(first_item[s_errmsg].toString().c_str());
176185
}
177186
if (first_item.exists(s_code)) {
178187
wce_obj->o_set(s_code, first_item[s_code], s_MongoDriverWriteConcernError_className);
@@ -194,7 +203,7 @@ Object hippo_write_result_init(mongoc_write_result_t *write_result, bson_error_t
194203
) {
195204
throw MongoDriver::Utils::throwExceptionFromBsonError(error);
196205
} else {
197-
auto bw_exception = MongoDriver::Utils::throwBulkWriteException("BulkWrite error");
206+
auto bw_exception = MongoDriver::Utils::throwBulkWriteException(message);
198207
bw_exception->o_set(s_MongoDriverExceptionBulkWriteException_writeResult, obj, MongoDriver::s_MongoDriverExceptionBulkWriteException_className);
199208

200209
throw bw_exception;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeBulkWrite() duplicate key error message
3+
--FILE--
4+
<?php
5+
define( 'NS', 'demo.test' );
6+
$manager = new MongoDB\Driver\Manager();
7+
8+
$c = new MongoDB\Driver\Command( [ 'drop' => 'test'] );
9+
try {
10+
$manager->executeCommand( 'demo', $c );
11+
}
12+
catch ( MongoDB\Driver\Exception\RuntimeException $e )
13+
{
14+
// Ignore "ns not found" errors
15+
if ( $e->getCode() == 59 ) {
16+
throw $e;
17+
}
18+
}
19+
$bulk = new MongoDB\Driver\BulkWrite();
20+
$bulk->insert(array('_id' => 1, 'x' => 1));
21+
$bulk->insert(array('_id' => 1, 'x' => 1));
22+
try {
23+
$manager->executeBulkWrite(NS, $bulk);
24+
} catch (\MongoDB\Driver\Exception\BulkWriteException $e) {
25+
printf("BulkWriteException: %s\n", $e->getMessage());
26+
}
27+
?>
28+
--EXPECTF--
29+
BulkWriteException: BulkWrite error :: E11000 duplicate key error index: demo.test.$_id_ dup key: { : 1 }

0 commit comments

Comments
 (0)