@@ -522,6 +522,56 @@ mongoc_bulk_operation_t *phongo_bulkwrite_init(zend_bool ordered) { /* {{{ */
522522 return mongoc_bulk_operation_new (ordered );
523523} /* }}} */
524524
525+ static void phongo_bulk_write_error_add_message (char * * tmp_msg , bson_t * errors )
526+ {
527+ bson_iter_t iter ;
528+
529+ bson_iter_init (& iter , errors );
530+
531+ while (bson_iter_next (& iter )) {
532+ bson_t cbson ;
533+ uint32_t len ;
534+ const uint8_t * data ;
535+ bson_iter_t inner_iter ;
536+
537+ if (!BSON_ITER_HOLDS_DOCUMENT (& iter )) {
538+ continue ;
539+ }
540+
541+ bson_iter_document (& iter , & len , & data );
542+
543+ if (!bson_init_static (& cbson , data , len )) {
544+ continue ;
545+ }
546+
547+ if (bson_iter_init_find (& inner_iter , & cbson , "errmsg" ) && BSON_ITER_HOLDS_UTF8 (& inner_iter )) {
548+ const char * tmp_errmsg = bson_iter_utf8 (& inner_iter , NULL );
549+ size_t tmp_errmsg_len = strlen (tmp_errmsg );
550+
551+ * tmp_msg = erealloc (* tmp_msg , strlen (* tmp_msg ) + tmp_errmsg_len + 5 );
552+ strncpy (* tmp_msg + strlen (* tmp_msg ), " :: " , 5 );
553+ strncpy (* tmp_msg + strlen (* tmp_msg ), tmp_errmsg , tmp_errmsg_len + 1 );
554+ }
555+ }
556+ }
557+
558+ static char * phongo_assemble_bulk_write_error (mongoc_write_result_t * write_result )
559+ {
560+ char * tmp_msg = emalloc (sizeof ("BulkWrite error" ));
561+
562+ strncpy (tmp_msg , "BulkWrite error" , sizeof ("BulkWrite error" ));
563+
564+ if (!bson_empty0 (& write_result -> writeErrors )) {
565+ phongo_bulk_write_error_add_message (& tmp_msg , & write_result -> writeErrors );
566+ }
567+
568+ if (!bson_empty0 (& write_result -> writeConcernErrors )) {
569+ phongo_bulk_write_error_add_message (& tmp_msg , & write_result -> writeConcernErrors );
570+ }
571+
572+ return tmp_msg ;
573+ }
574+
525575bool phongo_execute_write (mongoc_client_t * client , const char * namespace , mongoc_bulk_operation_t * bulk , const mongoc_write_concern_t * write_concern , int server_id , zval * return_value , int return_value_used TSRMLS_DC ) /* {{{ */
526576{
527577 bson_error_t error ;
@@ -576,7 +626,11 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, mongoc
576626 /* FIXME: Maybe we can look at write_result.error and not pass error at all? */
577627 phongo_throw_exception_from_bson_error_t (& error TSRMLS_CC );
578628 } else {
579- phongo_throw_exception (PHONGO_ERROR_WRITE_FAILED TSRMLS_CC , "BulkWrite error" );
629+ char * bulk_error_msg ;
630+
631+ bulk_error_msg = phongo_assemble_bulk_write_error (& writeresult -> write_result );
632+ phongo_throw_exception (PHONGO_ERROR_WRITE_FAILED TSRMLS_CC , "%s" , bulk_error_msg );
633+ efree (bulk_error_msg );
580634 phongo_add_exception_prop (ZEND_STRL ("writeResult" ), return_value TSRMLS_CC );
581635 }
582636 return false;
0 commit comments