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
max returns command error for findAndModify
  • Loading branch information
chilagrow committed May 17, 2023
commit 0fe2e5fd8973492f3288d2d6bfaf821cfd8b6165
15 changes: 15 additions & 0 deletions integration/findandmodify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ func TestFindAndModifyErrors(t *testing.T) {
altMessage: "Failed to apply $inc operations to current value " +
"((NumberInt)42) for document {_id: \"int32\"}",
},
"MaxUnsuitableValue": {
command: bson.D{
{"query", bson.D{{"_id", "array-documents-nested"}}},
{"update", bson.D{{"$max", 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 @@ -185,6 +185,15 @@ func TestUpdateFieldErrors(t *testing.T) {
},
provider: shareddata.Int32s,
},
"MaxUnsuitableValue": {
id: "array-documents-nested",
update: bson.D{{"$max", 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 @@ -107,7 +107,7 @@ func UpdateDocument(command string, doc, update *types.Document) (bool, error) {
}

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

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

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

changed = true
Expand Down