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-2704 Replace input validation error constants with "InvalidArgumentError". #1989

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

qingyang-hu
Copy link
Collaborator

GODRIVER-2704

Summary

Replace input validation error constants with "InvalidArgumentError".

Background & Motivation

@qingyang-hu qingyang-hu marked this pull request as draft March 14, 2025 22:14
@mongodb-drivers-pr-bot mongodb-drivers-pr-bot bot added the priority-3-low Low Priority PR for Review label Mar 14, 2025
Copy link
Contributor

mongodb-drivers-pr-bot bot commented Mar 14, 2025

API Change Report

./v2/mongo

compatible changes

InvalidArgumentError: added

@qingyang-hu qingyang-hu marked this pull request as ready for review March 17, 2025 13:43
@qingyang-hu qingyang-hu requested a review from matthewdale March 17, 2025 13:44
mongo/errors.go Outdated
Comment on lines 43 to 55
var ErrMultipleIndexDrop = InvalidArgumentError{errors.New("multiple indexes would be dropped")}

// ErrNilDocument is returned when a nil document is passed to a CRUD method.
var ErrNilDocument = errors.New("document is nil")
var ErrNilDocument = InvalidArgumentError{errors.New("document is nil")}

// ErrNilValue is returned when a nil value is passed to a CRUD method.
var ErrNilValue = errors.New("value is nil")
var ErrNilValue = InvalidArgumentError{errors.New("value is nil")}

// ErrEmptySlice is returned when an empty slice is passed to a CRUD method that requires a non-empty slice.
var ErrEmptySlice = errors.New("must provide at least one element in input slice")
var ErrEmptySlice = InvalidArgumentError{errors.New("must provide at least one element in input slice")}

// ErrNotSlice is returned when a type other than slice is passed to InsertMany.
var ErrNotSlice = errors.New("must provide a non-empty slice")
var ErrNotSlice = InvalidArgumentError{errors.New("must provide a non-empty slice")}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should keep the type of these error values error to preserve backward compatibility.

E.g.

var ErrNotSlice error = InvalidArgumentError{errors.New("must provide a non-empty slice")}

@@ -31,9 +31,6 @@ var ErrInvalidIndexValue = errors.New("invalid index value")
// ErrNonStringIndexName is returned if an index is created with a name that is not a string.
var ErrNonStringIndexName = errors.New("index name must be a string")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This error value appears to be unused. Can we mark it as deprecated, or should it be returned in some case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It seems not being used in the current v1 code base either. Is removing it considered a breaking change?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, removing it is technically breaking because someone's code might reference the symbol, which would cause a compilation error if we remove it. The best we can do is mark it deprecated and note that it's no longer used.

@@ -50,11 +50,11 @@ type clientBulkWrite struct {

func (bw *clientBulkWrite) execute(ctx context.Context) error {
if len(bw.writePairs) == 0 {
return ErrEmptySlice
return fmt.Errorf("error from writes: %w", ErrEmptySlice)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: Consider a more descriptive error message.

E.g.

fmt.Errorf("empty writes slice: %w", ErrEmptySlice)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think "empty writes slice" is redundant because the message in ErrEmptySlice has stated the slice should be non-empty.

Copy link
Collaborator

Choose a reason for hiding this comment

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

My goal is to clarify which BulkWrite parameter caused the validation error. "error from writes" is somewhat ambiguous because it could be interpreted as meaning there was an error performing the writes on the server. Would a less redundant error like "invalid writes" be better?

@qingyang-hu qingyang-hu requested a review from matthewdale March 20, 2025 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-3-low Low Priority PR for Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants