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 correct error codes for projections #384

Merged
merged 8 commits into from
Mar 22, 2022
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
2 changes: 2 additions & 0 deletions internal/handlers/common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const (
ErrNamespaceExists = ErrorCode(48) // NamespaceExists
ErrCommandNotFound = ErrorCode(59) // CommandNotFound
ErrNotImplemented = ErrorCode(238) // NotImplemented
ErrProjectionInEx = ErrorCode(31253) // Location31253
ErrProjectionExIn = ErrorCode(31254) // Location31254
ErrRegexOptions = ErrorCode(51075) // Location51075
)

Expand Down
11 changes: 9 additions & 2 deletions internal/handlers/common/errorcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions internal/handlers/common/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package common

import (
"errors"
"fmt"

"github.com/FerretDB/FerretDB/internal/types"
Expand All @@ -25,7 +24,6 @@ import (
// isProjectionInclusion: projection can be only inclusion or exlusion. Validate and return true if inclusion.
// Exception for the _id field.
func isProjectionInclusion(projection *types.Document) (inclusion bool, err error) {
errMsg := "projection must contain only inclusions or exclusions"
var exclusion bool
for k, v := range projection.Map() {
if k == "_id" { // _id is a special case and can be both
Expand All @@ -35,27 +33,35 @@ func isProjectionInclusion(projection *types.Document) (inclusion bool, err erro
case bool:
if v {
if exclusion {
err = errors.New(errMsg)
err = NewError(ErrProjectionInEx,
fmt.Errorf("Cannot do inclusion on field %s in exclusion projection", k),
)
return
}
inclusion = true
} else {
if inclusion {
err = errors.New(errMsg)
err = NewError(ErrProjectionExIn,
fmt.Errorf("Cannot do exclusion on field %s in inclusion projection", k),
)
return
}
exclusion = true
}
case int32, int64, float64:
if compareScalars(v, int32(0)) == equal {
if inclusion {
err = errors.New(errMsg)
err = NewError(ErrProjectionExIn,
fmt.Errorf("Cannot do exclusion on field %s in inclusion projection", k),
)
return
}
exclusion = true
} else {
if exclusion {
err = errors.New(errMsg)
err = NewError(ErrProjectionInEx,
fmt.Errorf("Cannot do inclusion on field %s in exclusion projection", k),
)
return
}
inclusion = true
Expand All @@ -76,7 +82,7 @@ func ProjectDocuments(docs []*types.Document, projection *types.Document) error

inclusion, err := isProjectionInclusion(projection)
if err != nil {
return NewError(ErrBadValue, err)
return err
}

projectionMap := projection.Map()
Expand Down