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 3 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
14 changes: 8 additions & 6 deletions internal/handlers/common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ const (
// For ProtocolError only.
errInternalError = ErrorCode(1) // InternalError

ErrBadValue = ErrorCode(2) // BadValue
ErrNamespaceNotFound = ErrorCode(26) // NamespaceNotFound
ErrNamespaceExists = ErrorCode(48) // NamespaceExists
ErrCommandNotFound = ErrorCode(59) // CommandNotFound
ErrNotImplemented = ErrorCode(238) // NotImplemented
ErrRegexOptions = ErrorCode(51075) // Location51075
ErrBadValue = ErrorCode(2) // BadValue
ErrNamespaceNotFound = ErrorCode(26) // NamespaceNotFound
ErrNamespaceExists = ErrorCode(48) // NamespaceExists
ErrCommandNotFound = ErrorCode(59) // CommandNotFound
ErrNotImplemented = ErrorCode(238) // NotImplemented
ErrProjectionInclusionInExclusion = ErrorCode(31253) // Cannot do inclusion on field array in exclusion projection
ErrProjectionExclusionInInclusion = ErrorCode(31253) // Cannot do exclusion on field array in inclusion projection
Copy link
Member

Choose a reason for hiding this comment

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

There are several problems:

  • the error code for that one is 31254;
  • "array" there is a field name, not a part of the the error message;
  • that comment specifies a string value produced by stringer tool (that wasn't run), not a error message.

ErrRegexOptions = ErrorCode(51075) // Location51075
)

// Error represents wire protocol error.
Expand Down
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(ErrProjectionExclusionInInclusion,
fmt.Errorf("Cannot do exclusion on field array in inclusion projection"),
)
return
}
inclusion = true
} else {
if inclusion {
err = errors.New(errMsg)
err = NewError(ErrProjectionInclusionInExclusion,
fmt.Errorf("Cannot do inclusion on field array in exclusion projection"),
)
return
}
exclusion = true
}
case int32, int64, float64:
if compareScalars(v, int32(0)) == equal {
if inclusion {
err = errors.New(errMsg)
err = NewError(ErrProjectionInclusionInExclusion,
fmt.Errorf("Cannot do inclusion on field array in exclusion projection"),
)
return
}
exclusion = true
} else {
if exclusion {
err = errors.New(errMsg)
err = NewError(ErrProjectionExclusionInInclusion,
fmt.Errorf("Cannot do exclusion on field array in inclusion projection"),
)
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