Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-3086 Add ErrorCodes to ServerError API #1894

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
80 changes: 64 additions & 16 deletions mongo/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,23 @@ type ServerError interface {
// HasErrorCodeWithMessage returns true if any of the contained errors have the specified code and message.
HasErrorCodeWithMessage(int, string) bool

// ErrorCodes returns a deduplicated list of error codes returned by the
// server.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’d be good to mention if there’s a defined sort order.

ErrorCodes() []int

serverError()
}

func hasErrorCode(srvErr ServerError, code int) bool {
for _, srvErrCode := range srvErr.ErrorCodes() {
if code == srvErrCode {
return true
}
}

return false
}

var _ ServerError = CommandError{}
var _ ServerError = WriteError{}
var _ ServerError = WriteException{}
Expand Down Expand Up @@ -285,7 +299,12 @@ func (e CommandError) Unwrap() error {

// HasErrorCode returns true if the error has the specified code.
func (e CommandError) HasErrorCode(code int) bool {
return int(e.Code) == code
return hasErrorCode(e, code)
}

// ErrorCodes returns a list of error codes returned by the server.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’d be good to mention if there’s a defined sort order.

func (e CommandError) ErrorCodes() []int {
return []int{int(e.Code)}
}

// HasErrorLabel returns true if the error contains the specified label.
Expand Down Expand Up @@ -340,7 +359,12 @@ func (we WriteError) Error() string {

// HasErrorCode returns true if the error has the specified code.
func (we WriteError) HasErrorCode(code int) bool {
return we.Code == code
return hasErrorCode(we, code)
}

// ErrorCodes returns a list of error codes returned by the server.
func (we WriteError) ErrorCodes() []int {
return []int{we.Code}
}

// HasErrorLabel returns true if the error contains the specified label. WriteErrors do not contain labels,
Expand Down Expand Up @@ -449,15 +473,27 @@ func (mwe WriteException) Error() string {

// HasErrorCode returns true if the error has the specified code.
func (mwe WriteException) HasErrorCode(code int) bool {
if mwe.WriteConcernError != nil && mwe.WriteConcernError.Code == code {
return true
return hasErrorCode(mwe, code)
}

// ErrorCodes returns a list of error codes returned by the server.
func (mwe WriteException) ErrorCodes() []int {
errorCodeSet := make(map[int]struct{})
for _, writeError := range mwe.WriteErrors {
errorCodeSet[writeError.Code] = struct{}{}
}
for _, we := range mwe.WriteErrors {
if we.Code == code {
return true
}

if mwe.WriteConcernError != nil {
errorCodeSet[mwe.WriteConcernError.Code] = struct{}{}
}
return false

// Deduplicate error codes.
errorCodes := make([]int, 0, len(errorCodeSet))
for code := range errorCodeSet {
errorCodes = append(errorCodes, code)
}

return errorCodes
}

// HasErrorLabel returns true if the error contains the specified label.
Expand Down Expand Up @@ -561,15 +597,27 @@ func (bwe BulkWriteException) Error() string {

// HasErrorCode returns true if any of the errors have the specified code.
func (bwe BulkWriteException) HasErrorCode(code int) bool {
if bwe.WriteConcernError != nil && bwe.WriteConcernError.Code == code {
return true
return hasErrorCode(bwe, code)
}

// ErrorCodes returns a list of error codes returned by the server.
func (bwe BulkWriteException) ErrorCodes() []int {
errorCodeSet := make(map[int]struct{})
for _, writeError := range bwe.WriteErrors {
errorCodeSet[writeError.Code] = struct{}{}
}
for _, we := range bwe.WriteErrors {
if we.Code == code {
return true
}

if bwe.WriteConcernError != nil {
errorCodeSet[bwe.WriteConcernError.Code] = struct{}{}
}
return false

// Deduplicate error codes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it affects us :), but: I see the same logic here and in WriteException’s method. This seems like the sort of thing you’d deduplicate.

errorCodes := make([]int, 0, len(errorCodeSet))
for code := range errorCodeSet {
errorCodes = append(errorCodes, code)
}

return errorCodes
}

// HasErrorLabel returns true if the error contains the specified label.
Expand Down
64 changes: 64 additions & 0 deletions mongo/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,70 @@ func TestIsTimeout(t *testing.T) {
}
}

func TestServerError_ErrorCodes(t *testing.T) {
tests := []struct {
name string
error ServerError
want []int
}{
{
name: "CommandError",
error: CommandError{Code: 1},
want: []int{1},
},
{
name: "WriteError",
error: WriteError{Code: 1},
want: []int{1},
},
{
name: "WriteException single",
error: WriteException{WriteErrors: []WriteError{{Code: 1}}},
want: []int{1},
},
{
name: "WriteException multiple",
error: WriteException{WriteErrors: []WriteError{{Code: 1}, {Code: 2}}},
want: []int{1, 2},
},
{
name: "WriteException duplicates",
error: WriteException{WriteErrors: []WriteError{{Code: 1}, {Code: 2}, {Code: 2}}},
want: []int{1, 2},
},
{
name: "BulkWriteException single",
error: BulkWriteException{WriteErrors: []BulkWriteError{{WriteError: WriteError{Code: 1}}}},
want: []int{1},
},
{
name: "BulkWriteException multiple",
error: BulkWriteException{WriteErrors: []BulkWriteError{
{WriteError: WriteError{Code: 1}},
{WriteError: WriteError{Code: 2}},
}},
want: []int{1, 2},
},
{
name: "BulkWriteException duplicates",
error: BulkWriteException{WriteErrors: []BulkWriteError{
{WriteError: WriteError{Code: 1}},
{WriteError: WriteError{Code: 2}},
{WriteError: WriteError{Code: 2}},
}},
want: []int{1, 2},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := test.error.ErrorCodes()

assert.ElementsMatch(t, got, test.want)
})
}
}

type netErr struct {
timeout bool
}
Expand Down
Loading