Skip to content

GODRIVER-3086 Add ErrorCodes to ServerError API#1894

Merged
prestonvasquez merged 7 commits intomongodb:masterfrom
prestonvasquez:GODRIVER-3086
Jan 30, 2025
Merged

GODRIVER-3086 Add ErrorCodes to ServerError API#1894
prestonvasquez merged 7 commits intomongodb:masterfrom
prestonvasquez:GODRIVER-3086

Conversation

@prestonvasquez
Copy link
Copy Markdown
Member

GODRIVER-3086

Summary

Extend the ServerError interface to include ErrorCodes which will return a list of deduplicated error codes returned by the server during the lifetime of operation execution.

Background & Motivation

The Go Driver provides a way to check if a ServerError has a specific error code, but no way to determine all error codes that occurred during the operation.

@prestonvasquez prestonvasquez marked this pull request as draft November 15, 2024 22:29
@prestonvasquez prestonvasquez marked this pull request as ready for review November 15, 2024 22:39
@mongodb-drivers-pr-bot mongodb-drivers-pr-bot Bot added the review-priority-low Low Priority PR for Review: within 3 business days label Nov 15, 2024
@mongodb-drivers-pr-bot
Copy link
Copy Markdown
Contributor

API Change Report

./v2/mongo

compatible changes

BulkWriteException.ErrorCodes: added
CommandError.ErrorCodes: added
ServerError.ErrorCodes: added
WriteError.ErrorCodes: added
WriteException.ErrorCodes: added

Copy link
Copy Markdown
Contributor

@FGasper FGasper left a comment

Choose a reason for hiding this comment

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

Thank you for this! :)

Comment thread mongo/errors.go Outdated
HasErrorCodeWithMessage(int, string) bool

// ErrorCodes returns a deduplicated list of error codes returned by the
// server.
Copy link
Copy Markdown
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@prestonvasquez Thoughts?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, I suggest:

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.

Comment thread mongo/errors.go
return hasErrorCode(e, code)
}

// ErrorCodes returns a list of error codes returned by the server.
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread mongo/errors.go Outdated
}
return false

// Deduplicate error codes.
Copy link
Copy Markdown
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.

Comment thread mongo/errors.go Outdated
Comment thread mongo/errors.go Outdated
Comment thread mongo/errors.go Outdated
matthewdale
matthewdale previously approved these changes Jan 22, 2025
Copy link
Copy Markdown
Contributor

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

Comment thread mongo/errors.go Outdated
// 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The latest revision does not deduplicate error codes.

Suggested change
// ErrorCodes returns a deduplicated list of error codes returned by the
// ErrorCodes returns a list of error codes returned by the

Copy link
Copy Markdown
Contributor

@FGasper FGasper left a comment

Choose a reason for hiding this comment

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

I’ve made a few suggestions on verbiage; see what you think.

Also: are there any cases where it is, in fact, correct just to check HasErrorCode() without also checking for other, unexpected error codes? If not, should HasErrorCode()’s documentation also be updated to discourage its use?

Basically ISTM that anyplace an application calls HasErrorCode(123) should instead be a check of whether ErrorCodes()’s return contains solely 123. Is that reasonable?

Comment thread mongo/errors.go Outdated
HasErrorCodeWithMessage(int, string) bool

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

Choose a reason for hiding this comment

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

@prestonvasquez Thoughts?

Comment thread mongo/errors.go Outdated
HasErrorCodeWithMessage(int, string) bool

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

Choose a reason for hiding this comment

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

Also, I suggest:

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.

Comment thread mongo/errors.go
return hasErrorCode(e, code)
}

// ErrorCodes returns a list of error codes returned by the server.
Copy link
Copy Markdown
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.

@prestonvasquez
Copy link
Copy Markdown
Member Author

@FGasper

I’ve made a few suggestions on verbiage; see what you think.

I agree with the changes to the interface documentation but don't think we need to be more specific on the methods.

anyplace an application calls HasErrorCode(123) should instead be a check of whether ErrorCodes()’s return contains solely 123. Is that reasonable?

HasErrorCode's logic has not changed and so we should maintain the current intentions and usage guidelines for our users. The current documentation correctly expresses the purpose of the method for each error type.

Copy link
Copy Markdown
Contributor

@FGasper FGasper left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@matthewdale
Copy link
Copy Markdown
Contributor

@FGasper I believe there are plenty of cases where HasErrorCode is sufficient, especially for single-read or single-write operations (i.e. non-bulk operations). I think it would be confusing to discourage its use.

Copy link
Copy Markdown
Contributor

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@FGasper
Copy link
Copy Markdown
Contributor

FGasper commented Jan 30, 2025

I’ve moved the HasErrorCode discussion to Slack.

@prestonvasquez prestonvasquez merged commit 4e0ab11 into mongodb:master Jan 30, 2025
@prestonvasquez prestonvasquez deleted the GODRIVER-3086 branch January 30, 2025 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-priority-low Low Priority PR for Review: within 3 business days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants