@@ -200,7 +200,10 @@ static int phongo_exception_append_error_labels(zval* labels, const bson_iter_t*
200200 bson_iter_t error_labels ;
201201 uint32_t label_count = 0 ;
202202
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+
204207 while (bson_iter_next (& error_labels )) {
205208 if (BSON_ITER_HOLDS_UTF8 (& error_labels )) {
206209 const char * error_label ;
@@ -217,7 +220,7 @@ static int phongo_exception_append_error_labels(zval* labels, const bson_iter_t*
217220
218221static void phongo_exception_add_error_labels (const bson_t * reply )
219222{
220- bson_iter_t iter ;
223+ bson_iter_t iter , child ;
221224 zval labels ;
222225 uint32_t label_count = 0 ;
223226
@@ -231,12 +234,20 @@ static void phongo_exception_add_error_labels(const bson_t* reply)
231234 label_count += phongo_exception_append_error_labels (& labels , & iter );
232235 }
233236
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 ;
236246
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+ }
240251 }
241252 }
242253
0 commit comments