Skip to content

Commit

Permalink
Projection add error codes when inclusion and exclusion is used (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
seeforschauer authored Mar 22, 2022
1 parent 7613293 commit 8219693
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
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

0 comments on commit 8219693

Please sign in to comment.