Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions mongo/client_bulk_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (bw *clientBulkWrite) execute(ctx context.Context) error {
Message: ce.Message,
Raw: ce.Raw,
},
Labels: ce.Labels,
}
}
if len(batches.writeConcernErrors) > 0 || len(batches.writeErrors) > 0 {
Expand Down Expand Up @@ -435,16 +436,17 @@ func (mb *modelBatches) processResponse(ctx context.Context, resp bsoncore.Docum
return nil
}
var res struct {
Ok bool
Cursor bsoncore.Document
NDeleted int32
NInserted int32
NMatched int32
NModified int32
NUpserted int32
NErrors int32
Code int32
Errmsg string
Ok bool
Cursor bsoncore.Document
NDeleted int32
NInserted int32
NMatched int32
NModified int32
NUpserted int32
NErrors int32
Code int32
Errmsg string
ErrorLabels []string
}
err := bson.Unmarshal(resp, &res)
if err != nil {
Expand All @@ -460,6 +462,7 @@ func (mb *modelBatches) processResponse(ctx context.Context, resp bsoncore.Docum
WriteConcernErrors: mb.writeConcernErrors,
WriteErrors: mb.writeErrors,
PartialResult: mb.result,
Labels: res.ErrorLabels,
}
}

Expand Down Expand Up @@ -520,6 +523,7 @@ func (mb *modelBatches) processResponse(ctx context.Context, resp bsoncore.Docum
WriteConcernErrors: mb.writeConcernErrors,
WriteErrors: mb.writeErrors,
PartialResult: mb.result,
Labels: writeCmdErr.Labels,
}
}
return nil
Expand Down
89 changes: 89 additions & 0 deletions mongo/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ var (
_ ServerError = WriteError{}
_ ServerError = WriteException{}
_ ServerError = BulkWriteException{}
_ ServerError = ClientBulkWriteException{}
)

var _ error = ClientBulkWriteException{}
Expand Down Expand Up @@ -797,6 +798,94 @@ type ClientBulkWriteException struct {
// The results of any successful operations that were performed before the error
// was encountered.
PartialResult *ClientBulkWriteResult

Labels []string
}

func (bwe ClientBulkWriteException) serverError() {}

// ErrorCodes returns the list of server error codes contained in the error.
func (bwe ClientBulkWriteException) ErrorCodes() []int {
errorCodes := []int{}

bwe.iterateErrors(func(code int, _ string) bool {
errorCodes = append(errorCodes, code)
return false
})

return errorCodes
}

// HasErrorCode returns true if the error has the specified code.
func (bwe ClientBulkWriteException) HasErrorCode(code int) bool {
for _, errorCode := range bwe.ErrorCodes() {
if errorCode == code {
return true
}
}

return false
}

// HasErrorCodeWithMessage returns true if any of the contained errors have the specified code and message.
func (bwe ClientBulkWriteException) HasErrorCodeWithMessage(code int, message string) bool {
has := false
bwe.iterateErrors(func(errCode int, errMsg string) bool {
if errCode == code && strings.Contains(errMsg, message) {
has = true
return true
}

return false
})

return has
}

// HasErrorMessage returns true if the error contains the specified message.
func (bwe ClientBulkWriteException) HasErrorMessage(message string) bool {
has := false
bwe.iterateErrors(func(_ int, errMsg string) bool {
if strings.Contains(errMsg, message) {
has = true
return true
}

return false
})

return has
}

// HasErrorLabel returns true if the error contains the specified label.
func (bwe ClientBulkWriteException) HasErrorLabel(label string) bool {
for _, l := range bwe.Labels {
if l == label {
return true
}
}

return false
}

func (bwe ClientBulkWriteException) iterateErrors(f func(int, string) bool) {
if bwe.WriteError != nil {
if f(bwe.WriteError.Code, bwe.WriteError.Message) {
return
}
}

for _, err := range bwe.WriteConcernErrors {
if f(err.Code, err.Message) {
return
}
}

for _, writeError := range bwe.WriteErrors {
if f(writeError.Code, writeError.Message) {
return
}
}
}

// Error implements the error interface.
Expand Down
Loading