@@ -200,7 +200,10 @@ static int phongo_exception_append_error_labels(zval* labels, const bson_iter_t*
200
200
bson_iter_t error_labels ;
201
201
uint32_t label_count = 0 ;
202
202
203
- bson_iter_recurse (iter , & error_labels );
203
+ if (!BSON_ITER_HOLDS_ARRAY (iter ) || !bson_iter_recurse (iter , & error_labels )) {
204
+ return label_count ;
205
+ }
206
+
204
207
while (bson_iter_next (& error_labels )) {
205
208
if (BSON_ITER_HOLDS_UTF8 (& error_labels )) {
206
209
const char * error_label ;
@@ -217,7 +220,7 @@ static int phongo_exception_append_error_labels(zval* labels, const bson_iter_t*
217
220
218
221
static void phongo_exception_add_error_labels (const bson_t * reply )
219
222
{
220
- bson_iter_t iter ;
223
+ bson_iter_t iter , child ;
221
224
zval labels ;
222
225
uint32_t label_count = 0 ;
223
226
@@ -231,12 +234,20 @@ static void phongo_exception_add_error_labels(const bson_t* reply)
231
234
label_count += phongo_exception_append_error_labels (& labels , & iter );
232
235
}
233
236
234
- if (bson_iter_init_find (& iter , reply , "writeConcernError" )) {
235
- bson_iter_t write_concern_error_iter ;
237
+ if (bson_iter_init_find (& iter , reply , "writeConcernError" ) && BSON_ITER_HOLDS_DOCUMENT (& iter ) &&
238
+ bson_iter_recurse (& iter , & child ) && bson_iter_find (& child , "errorLabels" )) {
239
+ label_count += phongo_exception_append_error_labels (& labels , & child );
240
+ }
241
+
242
+ /* mongoc_write_result_t always reports writeConcernErrors in an array, so
243
+ * we must iterate this to collect WCE labels for BulkWrite replies. */
244
+ if (bson_iter_init_find (& iter , reply , "writeConcernErrors" ) && BSON_ITER_HOLDS_ARRAY (& iter ) && bson_iter_recurse (& iter , & child )) {
245
+ bson_iter_t wce ;
236
246
237
- bson_iter_recurse (& iter , & write_concern_error_iter );
238
- if (bson_iter_find (& write_concern_error_iter , "errorLabels" )) {
239
- label_count += phongo_exception_append_error_labels (& labels , & write_concern_error_iter );
247
+ while (bson_iter_next (& child )) {
248
+ if (BSON_ITER_HOLDS_DOCUMENT (& child ) && bson_iter_recurse (& child , & wce ) && bson_iter_find (& wce , "errorLabels" )) {
249
+ label_count += phongo_exception_append_error_labels (& labels , & wce );
250
+ }
240
251
}
241
252
}
242
253
0 commit comments