mongoc_bulk_operation_execute() always initializes a write result document on error, regardless of the bson_error_t domain.
Currently, phongo_execute_bulk_write will only throw a BulkWriteException for errors within the SERVER and WRITE_CONCERN domains. In the case of a socket error, the result is ignored. This makes it impossible to make some result assertions within the retryable writes spec tests (
PHPLIB-307). Additionally, if the socket error was encountered on the second or later command within the bulk write, the result may contain relevant data based on the first (or earlier) command(s) that did succeed.
One possible solution would be to throw the original exception (e.g. ConnectionException) and then throw a BulkWriteException with the write result attached. The underlying exception would then be accessible via Exception::getPrevious(). One complication will be deciding what code/message to use for the resulting BulkWriteException – as we would not have a server message/code available and this would technically conflict with the following note in ServerException:
The code of this exception and its subclasses will correspond to the original error code from the server.
A compromise might be to use code 0, explicitly mention getPrevious() in the BulkWriteException error message, and note this exception (no pun intended) in the ServerException docs.