Skip to content

GODRIVER-3086 Add ErrorCodes to ServerError API #1894

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

Merged
merged 7 commits into from
Jan 30, 2025
Merged
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
66 changes: 52 additions & 14 deletions mongo/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,25 @@ type ServerError interface {
// HasErrorCodeWithMessage returns true if any of the contained errors have the specified code and message.
HasErrorCodeWithMessage(int, string) bool

// ErrorCodes returns all error codes (unsorted) in the server’s response.
// This would include nested errors (e.g., write concern errors) for
// supporting implementations (e.g., BulkWriteException) as well as the
// top-level error code.
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 @@ -290,6 +306,11 @@ func (e CommandError) HasErrorCode(code int) bool {
return int(e.Code) == 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

ErrorCodes returns all error codes (unsorted) in the server’s response. This includes nested errors (e.g., write concern errors) as well as the top-level error code.

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

// HasErrorLabel returns true if the error contains the specified label.
func (e CommandError) HasErrorLabel(label string) bool {
for _, l := range e.Labels {
Expand Down Expand Up @@ -345,6 +366,11 @@ func (we WriteError) HasErrorCode(code int) bool {
return we.Code == 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,
// so we always return false.
func (we WriteError) HasErrorLabel(string) bool {
Expand Down Expand Up @@ -451,15 +477,21 @@ 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 {
errorCodes := []int{}
for _, writeError := range mwe.WriteErrors {
errorCodes = append(errorCodes, writeError.Code)
}
for _, we := range mwe.WriteErrors {
if we.Code == code {
return true
}

if mwe.WriteConcernError != nil {
errorCodes = append(errorCodes, mwe.WriteConcernError.Code)
}
return false

return errorCodes
}

// HasErrorLabel returns true if the error contains the specified label.
Expand Down Expand Up @@ -563,15 +595,21 @@ 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 {
errorCodes := []int{}
for _, writeError := range bwe.WriteErrors {
errorCodes = append(errorCodes, writeError.Code)
}
for _, we := range bwe.WriteErrors {
if we.Code == code {
return true
}

if bwe.WriteConcernError != nil {
errorCodes = append(errorCodes, bwe.WriteConcernError.Code)
}
return false

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, 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, 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