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

Return command error from findAndModify #2646

Merged
merged 17 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
min returns command error for findAndModify
  • Loading branch information
chilagrow committed May 17, 2023
commit 1a3478304fbe7eb16fe6c9fc62411c2c1c6b4212
15 changes: 15 additions & 0 deletions integration/findandmodify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ func TestFindAndModifyErrors(t *testing.T) {
altMessage: "Cannot create field 'foo' in element " +
"{v: [ { foo: [ { bar: \"hello\" }, { bar: \"world\" } ] } ]}",
},
"MinUnsuitableValue": {
command: bson.D{
{"query", bson.D{{"_id", "array-documents-nested"}}},
{"update", bson.D{{"$min", bson.D{{"v.foo", 1}}}}},
},
err: &mongo.CommandError{
Code: 28,
Name: "PathNotViable",
Message: "Plan executor error during findAndModify :: caused by :: " +
"Cannot create field 'foo' in element " +
"{v: [ { foo: [ { bar: \"hello\" }, { bar: \"world\" } ] } ]}",
},
altMessage: "Cannot create field 'foo' in element " +
"{v: [ { foo: [ { bar: \"hello\" }, { bar: \"world\" } ] } ]}",
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions integration/update_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ func TestUpdateFieldErrors(t *testing.T) {
"{v: [ { foo: [ { bar: \"hello\" }, { bar: \"world\" } ] } ]}",
},
},
"MinUnsuitableValue": {
id: "array-documents-nested",
update: bson.D{{"$min", bson.D{{"v.foo", 1}}}},
err: &mongo.WriteError{
Code: 28,
Message: "Cannot create field 'foo' in element " +
"{v: [ { foo: [ { bar: \"hello\" }, { bar: \"world\" } ] } ]}",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions internal/handlers/common/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func UpdateDocument(command string, doc, update *types.Document) (bool, error) {
}

case "$min":
changed, err = processMinFieldExpression(doc, updateV)
changed, err = processMinFieldExpression(command, doc, updateV)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -505,7 +505,7 @@ func processMaxFieldExpression(command string, doc *types.Document, updateV any)

// processMinFieldExpression changes document according to $min operator.
// If the document was changed it returns true.
func processMinFieldExpression(doc *types.Document, updateV any) (bool, error) {
func processMinFieldExpression(command string, doc *types.Document, updateV any) (bool, error) {
minExpression := updateV.(*types.Document)
minExpression.SortFieldsByKey()

Expand All @@ -530,7 +530,7 @@ func processMinFieldExpression(doc *types.Document, updateV any) (bool, error) {
if !doc.HasByPath(path) {
err = doc.SetByPath(path, minVal)
if err != nil {
return false, commonerrors.NewWriteErrorMsg(commonerrors.ErrUnsuitableValueType, err.Error())
return false, newUpdateError(commonerrors.ErrUnsuitableValueType, err.Error(), command)
}

changed = true
Expand Down