diff --git a/.golangci.yml b/.golangci.yml index 92fe847354d7..fd73857d6c28 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -54,13 +54,13 @@ linters-settings: - "**/internal/backends/**/*.go" deny: - pkg: github.com/FerretDB/FerretDB/internal/handler/common - commonerrors: + handlererrors: files: - $all - "!**/internal/clientconn/*.go" - "!**/internal/handler/**.go" deny: - - pkg: github.com/FerretDB/FerretDB/internal/handler/commonerrors + - pkg: github.com/FerretDB/FerretDB/internal/handler/handlererrors exhaustive: default-signifies-exhaustive: false gci: diff --git a/internal/clientconn/conn.go b/internal/clientconn/conn.go index 1ab149b85901..36685cac9063 100644 --- a/internal/clientconn/conn.go +++ b/internal/clientconn/conn.go @@ -37,7 +37,7 @@ import ( "github.com/FerretDB/FerretDB/internal/clientconn/conninfo" "github.com/FerretDB/FerretDB/internal/clientconn/connmetrics" "github.com/FerretDB/FerretDB/internal/handler" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/handler/proxy" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -255,7 +255,7 @@ func (c *conn) run(ctx context.Context) (err error) { // TODO https://github.com/FerretDB/FerretDB/issues/2412 // get protocol error to return correct error document - protoErr := commonerrors.ProtocolError(validationErr) + protoErr := handlererrors.ProtocolError(validationErr) var res wire.OpMsg must.NoError(res.SetSections(wire.OpMsgSection{ @@ -476,7 +476,7 @@ func (c *conn) route(ctx context.Context, reqHeader *wire.MsgHeader, reqBody wir if err != nil { switch resHeader.OpCode { case wire.OpCodeMsg: - protoErr := commonerrors.ProtocolError(err) + protoErr := handlererrors.ProtocolError(err) var res wire.OpMsg must.NoError(res.SetSections(wire.OpMsgSection{ @@ -485,9 +485,9 @@ func (c *conn) route(ctx context.Context, reqHeader *wire.MsgHeader, reqBody wir resBody = &res switch protoErr := protoErr.(type) { - case *commonerrors.CommandError: + case *handlererrors.CommandError: result = protoErr.Code().String() - case *commonerrors.WriteErrors: + case *handlererrors.WriteErrors: result = "write-error" default: panic(fmt.Errorf("unexpected error type %T", protoErr)) @@ -574,7 +574,7 @@ func (c *conn) handleOpMsg(ctx context.Context, msg *wire.OpMsg, command string) errMsg := fmt.Sprintf("no such command: '%s'", command) - return nil, commonerrors.NewCommandErrorMsg(commonerrors.ErrCommandNotFound, errMsg) + return nil, handlererrors.NewCommandErrorMsg(handlererrors.ErrCommandNotFound, errMsg) } // logResponse logs response's header and body and returns the log level that was used. diff --git a/internal/handler/cmd_query.go b/internal/handler/cmd_query.go index 2cfedfff612f..7c60a758ebb3 100644 --- a/internal/handler/cmd_query.go +++ b/internal/handler/cmd_query.go @@ -20,7 +20,7 @@ import ( "strings" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/must" "github.com/FerretDB/FerretDB/internal/wire" @@ -53,8 +53,8 @@ func (h *Handler) CmdQuery(ctx context.Context, query *wire.OpQuery) (*wire.OpRe }, nil } - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, fmt.Sprintf("CmdQuery: unhandled command %q for collection %q", cmd, collection), "OpQuery: "+cmd, ) diff --git a/internal/handler/common/add_fields_iterator.go b/internal/handler/common/add_fields_iterator.go index 6fd336bd81e7..ca1acac07c25 100644 --- a/internal/handler/common/add_fields_iterator.go +++ b/internal/handler/common/add_fields_iterator.go @@ -18,7 +18,7 @@ import ( "errors" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/operators" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -102,33 +102,33 @@ func processAddFieldsError(err error) error { switch opErr.Code() { case operators.ErrTooManyFields: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrAddFieldsExpressionWrongAmountOfArgs, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrAddFieldsExpressionWrongAmountOfArgs, "Invalid $addFields :: caused by :: FieldPath field names may not start with '$'."+ " Consider using $getField or $setField.", "$addFields (stage)", ) case operators.ErrNotImplemented: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "Invalid $addFields :: caused by :: "+opErr.Error(), "$addFields (stage)", ) case operators.ErrArgsInvalidLen: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrOperatorWrongLenOfArgs, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrOperatorWrongLenOfArgs, "Invalid $addFields :: caused by :: "+opErr.Error(), "$addFields (stage)", ) case operators.ErrInvalidExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidPipelineOperator, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidPipelineOperator, "Invalid $addFields :: caused by :: "+opErr.Error(), "$addFields (stage)", ) case operators.ErrInvalidNestedExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidPipelineOperator, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidPipelineOperator, "Invalid $addFields :: caused by :: "+opErr.Error(), "$addFields (stage)", ) diff --git a/internal/handler/common/aggregations/operators/accumulators/accumulators.go b/internal/handler/common/aggregations/operators/accumulators/accumulators.go index df2d1e88be53..283c94f7e59a 100644 --- a/internal/handler/common/aggregations/operators/accumulators/accumulators.go +++ b/internal/handler/common/aggregations/operators/accumulators/accumulators.go @@ -25,7 +25,7 @@ import ( "errors" "fmt" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/must" @@ -46,8 +46,8 @@ type Accumulator interface { func NewAccumulator(stage, key string, value any) (Accumulator, error) { accumulation, ok := value.(*types.Document) if !ok || accumulation.Len() == 0 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageGroupInvalidAccumulator, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageGroupInvalidAccumulator, fmt.Sprintf("The field '%s' must be an accumulator object", key), stage+" (stage)", ) @@ -55,8 +55,8 @@ func NewAccumulator(stage, key string, value any) (Accumulator, error) { // accumulation document contains only one field. if accumulation.Len() > 1 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageGroupMultipleAccumulator, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageGroupMultipleAccumulator, fmt.Sprintf("The field '%s' must specify one accumulator", key), stage+" (stage)", ) @@ -90,8 +90,8 @@ func NewAccumulator(stage, key string, value any) (Accumulator, error) { newAccumulator, ok := Accumulators[operator] if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, fmt.Sprintf("%s accumulator %q is not implemented yet", stage, operator), operator+" (accumulator)", ) diff --git a/internal/handler/common/aggregations/operators/accumulators/count.go b/internal/handler/common/aggregations/operators/accumulators/count.go index 9b940c004596..616a059a59cc 100644 --- a/internal/handler/common/aggregations/operators/accumulators/count.go +++ b/internal/handler/common/aggregations/operators/accumulators/count.go @@ -17,7 +17,7 @@ package accumulators import ( "errors" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" ) @@ -28,8 +28,8 @@ type count struct{} // newCount creates a new $count aggregation operator. func newCount(args ...any) (Accumulator, error) { if len(args) != 1 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageGroupUnaryOperator, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageGroupUnaryOperator, "The $count accumulator is a unary operator", "$count (accumulator)", ) @@ -38,8 +38,8 @@ func newCount(args ...any) (Accumulator, error) { doc, ok := args[0].(*types.Document) if !ok || doc.Len() > 0 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, "$count takes no arguments, i.e. $count:{}", "$count (accumulator)", ) diff --git a/internal/handler/common/aggregations/operators/accumulators/sum.go b/internal/handler/common/aggregations/operators/accumulators/sum.go index e82a35da4317..f3489be88c48 100644 --- a/internal/handler/common/aggregations/operators/accumulators/sum.go +++ b/internal/handler/common/aggregations/operators/accumulators/sum.go @@ -19,7 +19,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/operators" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -37,8 +37,8 @@ func newSum(args ...any) (Accumulator, error) { accumulator := new(sum) if len(args) != 1 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageGroupUnaryOperator, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageGroupUnaryOperator, "The $sum accumulator is a unary operator", "$sum (accumulator)", ) diff --git a/internal/handler/common/aggregations/operators/expr.go b/internal/handler/common/aggregations/operators/expr.go index 57d5a8d8ec66..663191268a4b 100644 --- a/internal/handler/common/aggregations/operators/expr.go +++ b/internal/handler/common/aggregations/operators/expr.go @@ -19,7 +19,7 @@ import ( "fmt" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -239,32 +239,32 @@ func processExprOperatorErrors(err error, argument string) error { case errors.As(err, &opErr): switch opErr.Code() { case ErrTooManyFields: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrExpressionWrongLenOfFields, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrExpressionWrongLenOfFields, "An object representing an expression must have exactly one field", argument, ) case ErrNotImplemented: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "Invalid $match :: caused by :: "+opErr.Error(), argument, ) case ErrArgsInvalidLen: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrOperatorWrongLenOfArgs, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrOperatorWrongLenOfArgs, opErr.Error(), argument, ) case ErrInvalidExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidPipelineOperator, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidPipelineOperator, fmt.Sprintf("Unrecognized expression '%s'", opErr.Name()), argument, ) case ErrInvalidNestedExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidPipelineOperator, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidPipelineOperator, opErr.Error(), argument, ) @@ -273,27 +273,27 @@ func processExprOperatorErrors(err error, argument string) error { case errors.As(err, &exErr): switch exErr.Code() { case aggregations.ErrInvalidExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, fmt.Sprintf("'%s' starts with an invalid character for a user variable name", exErr.Name()), argument, ) case aggregations.ErrEmptyFieldPath: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrGroupInvalidFieldPath, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrGroupInvalidFieldPath, "'$' by itself is not a valid FieldPath", argument, ) case aggregations.ErrUndefinedVariable: // TODO https://github.com/FerretDB/FerretDB/issues/2275 - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "Aggregation expression variables are not implemented yet", argument, ) case aggregations.ErrEmptyVariable: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, "empty variable names are not allowed", argument, ) diff --git a/internal/handler/common/aggregations/operators/type.go b/internal/handler/common/aggregations/operators/type.go index c5b2544a2bf7..dda883101b09 100644 --- a/internal/handler/common/aggregations/operators/type.go +++ b/internal/handler/common/aggregations/operators/type.go @@ -22,7 +22,7 @@ import ( "time" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" ) @@ -119,7 +119,7 @@ func (t *typeOp) Process(doc *types.Document) (any, error) { } } - return commonparams.AliasFromType(res), nil + return handlerparams.AliasFromType(res), nil } // check interfaces diff --git a/internal/handler/common/aggregations/stages/add_fields.go b/internal/handler/common/aggregations/stages/add_fields.go index b81241cfa9a8..2b60d44ce8a6 100644 --- a/internal/handler/common/aggregations/stages/add_fields.go +++ b/internal/handler/common/aggregations/stages/add_fields.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -42,8 +42,8 @@ func newAddFields(stage *types.Document) (aggregations.Stage, error) { fieldsDoc, ok := fields.(*types.Document) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrSetBadExpression, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrSetBadExpression, fmt.Sprintf("$addFields specification stage must be an object, got %T", fields), "$addFields (stage)", ) diff --git a/internal/handler/common/aggregations/stages/collstats.go b/internal/handler/common/aggregations/stages/collstats.go index 069214f8482d..12c2fd9ba6f1 100644 --- a/internal/handler/common/aggregations/stages/collstats.go +++ b/internal/handler/common/aggregations/stages/collstats.go @@ -21,8 +21,8 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -46,8 +46,8 @@ type storageStats struct { func newCollStats(stage *types.Document) (aggregations.Stage, error) { fields, err := common.GetRequiredParam[*types.Document](stage, "$collStats") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageCollStatsInvalidArg, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageCollStatsInvalidArg, fmt.Sprintf("$collStats must take a nested object but found: %s", types.FormatAnyValue(stage)), "$collStats (stage)", ) @@ -74,7 +74,7 @@ func newCollStats(stage *types.Document) (aggregations.Stage, error) { var s any if s, err = storageStatsFields.Get("scale"); err == nil { - scale, err := commonparams.GetValidatedNumberParamWithMinValue( + scale, err := handlerparams.GetValidatedNumberParamWithMinValue( "$collStats.storageStats", "scale", s, 1, ) if err != nil { diff --git a/internal/handler/common/aggregations/stages/count.go b/internal/handler/common/aggregations/stages/count.go index 47675141b229..123e8b89a2db 100644 --- a/internal/handler/common/aggregations/stages/count.go +++ b/internal/handler/common/aggregations/stages/count.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" ) @@ -34,40 +34,40 @@ type count struct { func newCount(stage *types.Document) (aggregations.Stage, error) { field, err := common.GetRequiredParam[string](stage, "$count") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageCountNonString, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageCountNonString, "the count field must be a non-empty string", "$count (stage)", ) } if len(field) == 0 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageCountNonEmptyString, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageCountNonEmptyString, "the count field must be a non-empty string", "$count (stage)", ) } if strings.Contains(field, ".") { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageCountBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageCountBadValue, "the count field cannot contain '.'", "$count (stage)", ) } if strings.HasPrefix(field, "$") { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageCountBadPrefix, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageCountBadPrefix, "the count field cannot be a $-prefixed path", "$count (stage)", ) } if field == "_id" { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageGroupID, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageGroupID, "a group's _id may only be specified once", "$count (stage)", ) diff --git a/internal/handler/common/aggregations/stages/group.go b/internal/handler/common/aggregations/stages/group.go index 6b3dc0b25ded..c03c9eb7f4c8 100644 --- a/internal/handler/common/aggregations/stages/group.go +++ b/internal/handler/common/aggregations/stages/group.go @@ -24,7 +24,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/operators" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/operators/accumulators" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -58,8 +58,8 @@ type groupBy struct { func newGroup(stage *types.Document) (aggregations.Stage, error) { fields, err := common.GetRequiredParam[*types.Document](stage, "$group") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageGroupInvalidFields, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageGroupInvalidFields, "a group's fields must be specified in an object", "$group (stage)", ) @@ -103,8 +103,8 @@ func newGroup(stage *types.Document) (aggregations.Stage, error) { } if groupKey == nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageGroupMissingID, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageGroupMissingID, "a group specification must include an _id", "$group (stage)", ) @@ -140,8 +140,8 @@ func (g *group) Process(ctx context.Context, iter types.DocumentsIterator, close if doc.Has(accumulation.outputField) { // document has duplicate key - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrDuplicateField, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrDuplicateField, fmt.Sprintf("duplicate field: %s", accumulation.outputField), "$group (stage)", ) @@ -196,8 +196,8 @@ func validateGroupKey(groupKey any) error { } if _, ok := fields[k]; ok { - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrGroupDuplicateFieldName, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrGroupDuplicateFieldName, fmt.Sprintf("duplicate field name specified in object literal: %s", types.FormatAnyValue(doc)), "$group (stage)", ) @@ -398,32 +398,32 @@ func processGroupStageError(err error) error { case errors.As(err, &opErr): switch opErr.Code() { case operators.ErrTooManyFields: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrExpressionWrongLenOfFields, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrExpressionWrongLenOfFields, "An object representing an expression must have exactly one field", "$group (stage)", ) case operators.ErrNotImplemented: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "Invalid $group :: caused by :: "+opErr.Error(), "$group (stage)", ) case operators.ErrArgsInvalidLen: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrOperatorWrongLenOfArgs, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrOperatorWrongLenOfArgs, opErr.Error(), "$group (stage)", ) case operators.ErrInvalidExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidPipelineOperator, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidPipelineOperator, opErr.Error(), "$group (stage)", ) case operators.ErrInvalidNestedExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidPipelineOperator, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidPipelineOperator, opErr.Error(), "$group (stage)", ) @@ -435,27 +435,27 @@ func processGroupStageError(err error) error { // handled by upstream and this should not be reachable for existing expression implementation fallthrough case aggregations.ErrInvalidExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, "'$' starts with an invalid character for a user variable name", "$group (stage)", ) case aggregations.ErrEmptyFieldPath: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrGroupInvalidFieldPath, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrGroupInvalidFieldPath, "'$' by itself is not a valid FieldPath", "$group (stage)", ) case aggregations.ErrUndefinedVariable: // TODO https://github.com/FerretDB/FerretDB/issues/2275 - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "Aggregation expression variables are not implemented yet", "$group (stage)", ) case aggregations.ErrEmptyVariable: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, "empty variable names are not allowed", "$group (stage)", ) diff --git a/internal/handler/common/aggregations/stages/match.go b/internal/handler/common/aggregations/stages/match.go index 3afe079a1fe6..f347b2ba5648 100644 --- a/internal/handler/common/aggregations/stages/match.go +++ b/internal/handler/common/aggregations/stages/match.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/operators" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" ) @@ -34,8 +34,8 @@ type match struct { func newMatch(stage *types.Document) (aggregations.Stage, error) { filter, err := common.GetRequiredParam[*types.Document](stage, "$match") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrMatchBadExpression, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrMatchBadExpression, "the match filter must be an expression in an object", "$match (stage)", ) diff --git a/internal/handler/common/aggregations/stages/project.go b/internal/handler/common/aggregations/stages/project.go index 673590369509..ec6279116a5c 100644 --- a/internal/handler/common/aggregations/stages/project.go +++ b/internal/handler/common/aggregations/stages/project.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/stages/projection" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" ) @@ -42,8 +42,8 @@ type project struct { func newProject(stage *types.Document) (aggregations.Stage, error) { fields, err := common.GetRequiredParam[*types.Document](stage, "$project") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrProjectBadExpression, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrProjectBadExpression, "$project specification must be an object", "$project (stage)", ) diff --git a/internal/handler/common/aggregations/stages/projection/projection.go b/internal/handler/common/aggregations/stages/projection/projection.go index c7d599b9d3bd..5e7d5d2521da 100644 --- a/internal/handler/common/aggregations/stages/projection/projection.go +++ b/internal/handler/common/aggregations/stages/projection/projection.go @@ -24,7 +24,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/operators" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -52,8 +52,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro validated := types.MakeDocument(0) if projection.Len() == 0 { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrEmptyProject, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrEmptyProject, "Invalid $project :: caused by :: projection specification must have at least one field", "$project (stage)", ) @@ -71,8 +71,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro } if key == "" { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrEmptyFieldPath, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrEmptyFieldPath, "Invalid $project :: caused by :: FieldPath cannot be constructed with empty string", "$project (stage)", ) @@ -82,23 +82,23 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro path, err := types.NewPathFromString(key) if err != nil { if strings.HasSuffix(key, "$") { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrAggregatePositionalProject, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrAggregatePositionalProject, "Invalid $project :: caused by :: Cannot use positional projection in aggregation projection", "$project (stage)", ) } - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrPathContainsEmptyElement, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrPathContainsEmptyElement, "Invalid $project :: caused by :: FieldPath field names may not be empty strings.", "projection", ) } if key == "$" { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFieldPathInvalidName, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFieldPathInvalidName, "Invalid $project :: caused by :: FieldPath field names may not start with '$'. "+ "Consider using $getField or $setField.", "$project (stage)", @@ -106,8 +106,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro } if strings.HasSuffix(key, "$") { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrAggregatePositionalProject, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrAggregatePositionalProject, "Invalid $project :: caused by :: Cannot use positional projection in aggregation projection", "$project (stage)", ) @@ -116,8 +116,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro // if `$` is at prefix, it returns ErrFieldPathInvalidName error code instead. prefixTrimmed := strings.TrimPrefix(key, "$") if slices.Contains(strings.Split(prefixTrimmed, "."), "$") { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrWrongPositionalOperatorLocation, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrWrongPositionalOperatorLocation, "Invalid $project :: caused by :: "+ "Positional projection may only be used at the end, "+ "for example: a.b.$. If the query previously used a form "+ @@ -131,8 +131,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro if strings.HasPrefix(k, "$") { // arbitrary `$` cannot exist in the path // `v.$foo` is invalid, `v.$` and `v.foo$` are fine. - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFieldPathInvalidName, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFieldPathInvalidName, "Invalid $project :: caused by :: FieldPath field names may not start with '$'. "+ "Consider using $getField or $setField.", "$project (stage)", @@ -146,8 +146,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro case *types.Document: if !operators.IsOperator(value) { if value.Len() == 0 { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrEmptySubProject, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrEmptySubProject, "Invalid $project :: caused by :: An empty sub-projection is not a valid value."+ " Found empty object at path", "$project (stage)", @@ -220,15 +220,15 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro } if *projectionVal { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrProjectionExIn, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrProjectionExIn, fmt.Sprintf("Cannot do exclusion on field %s in inclusion projection", key), "projection", ) } - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrProjectionInEx, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrProjectionInEx, fmt.Sprintf("Cannot do inclusion on field %s in exclusion projection", key), "projection", ) @@ -646,33 +646,33 @@ func processOperatorError(err error) error { case errors.As(err, &opErr): switch opErr.Code() { case operators.ErrTooManyFields: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFieldPathInvalidName, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFieldPathInvalidName, "Invalid $project :: caused by :: FieldPath field names may not start with '$'."+ " Consider using $getField or $setField.", "$project (stage)", ) case operators.ErrNotImplemented: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "Invalid $project :: caused by :: "+opErr.Error(), "$project (stage)", ) case operators.ErrArgsInvalidLen: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrOperatorWrongLenOfArgs, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrOperatorWrongLenOfArgs, "Invalid $project :: caused by :: "+opErr.Error(), "$project (stage)", ) case operators.ErrInvalidExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrAggregateInvalidExpression, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrAggregateInvalidExpression, "Invalid $project :: caused by :: "+opErr.Error(), "$project (stage)", ) case operators.ErrInvalidNestedExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidPipelineOperator, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidPipelineOperator, "Invalid $project :: caused by :: "+opErr.Error(), "$project (stage)", ) @@ -684,27 +684,27 @@ func processOperatorError(err error) error { // handled by upstream and this should not be reachable for existing expression implementation fallthrough case aggregations.ErrInvalidExpression: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, "Invalid $project :: caused by :: '$' starts with an invalid character for a user variable name", "$project (stage)", ) case aggregations.ErrEmptyFieldPath: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrGroupInvalidFieldPath, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrGroupInvalidFieldPath, "Invalid $project :: caused by :: '$' by itself is not a valid FieldPath", "$project (stage)", ) case aggregations.ErrUndefinedVariable: // TODO https://github.com/FerretDB/FerretDB/issues/2275 - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "Aggregation expression variables are not implemented yet", "$project (stage)", ) case aggregations.ErrEmptyVariable: - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, "Invalid $project :: caused by :: empty variable names are not allowed", "$project (stage)", ) diff --git a/internal/handler/common/aggregations/stages/set.go b/internal/handler/common/aggregations/stages/set.go index de4d4dc1c136..4a601dfefa86 100644 --- a/internal/handler/common/aggregations/stages/set.go +++ b/internal/handler/common/aggregations/stages/set.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -44,8 +44,8 @@ func newSet(stage *types.Document) (aggregations.Stage, error) { fieldsDoc, ok := fields.(*types.Document) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrSetBadExpression, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrSetBadExpression, fmt.Sprintf("$set specification stage must be an object, got %T", fields), "$set (stage)", ) diff --git a/internal/handler/common/aggregations/stages/sort.go b/internal/handler/common/aggregations/stages/sort.go index 233d17fcd08f..af6c67d459f1 100644 --- a/internal/handler/common/aggregations/stages/sort.go +++ b/internal/handler/common/aggregations/stages/sort.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -35,16 +35,16 @@ type sort struct { func newSort(stage *types.Document) (aggregations.Stage, error) { fields, err := common.GetRequiredParam[*types.Document](stage, "$sort") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrSortBadExpression, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrSortBadExpression, "the $sort key specification must be an object", "$sort (stage)", ) } if fields.Len() == 0 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrSortMissingKey, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrSortMissingKey, "$sort stage must have at least one sort key", "$sort (stage)", ) @@ -66,8 +66,8 @@ func (s *sort) Process(ctx context.Context, iter types.DocumentsIterator, closer // TODO https://github.com/FerretDB/FerretDB/issues/3125 var pathErr *types.PathError if errors.As(err, &pathErr) && pathErr.Code() == types.ErrPathElementEmpty { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrPathContainsEmptyElement, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrPathContainsEmptyElement, "FieldPath field names may not be empty strings.", "$sort (stage)", ) diff --git a/internal/handler/common/aggregations/stages/stages.go b/internal/handler/common/aggregations/stages/stages.go index 76056879276c..ab811401a986 100644 --- a/internal/handler/common/aggregations/stages/stages.go +++ b/internal/handler/common/aggregations/stages/stages.go @@ -19,7 +19,7 @@ import ( "fmt" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" ) @@ -80,8 +80,8 @@ var unsupportedStages = map[string]struct{}{ // NewStage creates a new aggregation stage. func NewStage(stage *types.Document) (aggregations.Stage, error) { if stage.Len() != 1 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageInvalid, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageInvalid, "A pipeline stage specification object must contain exactly one field.", "aggregate", ) @@ -100,15 +100,15 @@ func NewStage(stage *types.Document) (aggregations.Stage, error) { return f(stage) case !supported && unsupported: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, fmt.Sprintf("`aggregate` stage %q is not implemented yet", name), name+" (stage)", // to differentiate update operator $set from aggregation stage $set, etc ) case !supported && !unsupported: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageGroupInvalidAccumulator, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageGroupInvalidAccumulator, fmt.Sprintf("Unrecognized pipeline stage name: %q", name), name+" (stage)", // to differentiate update operator $set from aggregation stage $set, etc ) diff --git a/internal/handler/common/aggregations/stages/unset.go b/internal/handler/common/aggregations/stages/unset.go index b0baf7f7c8d0..cb16b5f05ac9 100644 --- a/internal/handler/common/aggregations/stages/unset.go +++ b/internal/handler/common/aggregations/stages/unset.go @@ -22,7 +22,7 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/stages/projection" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -48,8 +48,8 @@ func newUnset(stage *types.Document) (aggregations.Stage, error) { switch fields := fields.(type) { case *types.Array: if fields.Len() == 0 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageUnsetNoPath, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageUnsetNoPath, "$unset specification must be a string or an array with at least one field", "$unset (stage)", ) @@ -72,8 +72,8 @@ func newUnset(stage *types.Document) (aggregations.Stage, error) { field, ok := v.(string) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageUnsetArrElementInvalidType, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageUnsetArrElementInvalidType, "$unset specification must be a string or an array containing only string values", "$unset (stage)", ) @@ -90,8 +90,8 @@ func newUnset(stage *types.Document) (aggregations.Stage, error) { if errors.As(err, &pathErr) { if pathErr.Code() == types.ErrPathConflictOverwrite { // the path overwrites one of visitedPaths. - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrUnsetPathOverwrite, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrUnsetPathOverwrite, fmt.Sprintf("Invalid $unset :: caused by :: Path collision at %s", field), "$unset (stage)", ) @@ -99,8 +99,8 @@ func newUnset(stage *types.Document) (aggregations.Stage, error) { if pathErr.Code() == types.ErrPathConflictCollision { // the path creates collision at one of visitedPaths. - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrUnsetPathCollision, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrUnsetPathCollision, fmt.Sprintf( "Invalid $unset :: caused by :: Path collision at %s remaining portion %s", path.String(), @@ -126,8 +126,8 @@ func newUnset(stage *types.Document) (aggregations.Stage, error) { exclusion.Set(fields, false) default: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageUnsetInvalidType, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageUnsetInvalidType, "$unset specification must be a string or an array", "$unset (stage)", ) @@ -147,16 +147,16 @@ func (u *unset) Process(_ context.Context, iter types.DocumentsIterator, closer // validateUnsetField returns error on invalid field value. func validateUnsetField(field string) (*types.Path, error) { if field == "" { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrEmptyFieldPath, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrEmptyFieldPath, "Invalid $unset :: caused by :: FieldPath cannot be constructed with empty string", "$unset (stage)", ) } if strings.HasPrefix(field, "$") { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFieldPathInvalidName, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFieldPathInvalidName, "Invalid $unset :: caused by :: FieldPath field names may not start with '$'. "+ "Consider using $getField or $setField.", "$unset (stage)", @@ -164,8 +164,8 @@ func validateUnsetField(field string) (*types.Path, error) { } if strings.HasSuffix(field, ".") { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidFieldPath, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidFieldPath, "Invalid $unset :: caused by :: FieldPath must not end with a '.'.", "$unset (stage)", ) @@ -173,8 +173,8 @@ func validateUnsetField(field string) (*types.Path, error) { path, err := types.NewPathFromString(field) if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrPathContainsEmptyElement, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrPathContainsEmptyElement, "Invalid $unset :: caused by :: FieldPath field names may not be empty strings.", "$unset (stage)", ) diff --git a/internal/handler/common/aggregations/stages/unwind.go b/internal/handler/common/aggregations/stages/unwind.go index 2391f48005be..127a89870b6d 100644 --- a/internal/handler/common/aggregations/stages/unwind.go +++ b/internal/handler/common/aggregations/stages/unwind.go @@ -21,8 +21,8 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" "github.com/FerretDB/FerretDB/internal/handler/commonpath" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -48,8 +48,8 @@ func newUnwind(stage *types.Document) (aggregations.Stage, error) { return nil, common.Unimplemented(stage, "$unwind") case string: if field == "" { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageUnwindNoPath, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageUnwindNoPath, "no path specified to $unwind stage", "$unwind (stage)", ) @@ -71,20 +71,20 @@ func newUnwind(stage *types.Document) (aggregations.Stage, error) { switch exprErr.Code() { case aggregations.ErrNotExpression: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageUnwindNoPrefix, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageUnwindNoPrefix, fmt.Sprintf("path option to $unwind stage should be prefixed with a '$': %v", types.FormatAnyValue(field)), "$unwind (stage)", ) case aggregations.ErrEmptyFieldPath: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrEmptyFieldPath, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrEmptyFieldPath, "Expression cannot be constructed with empty string", "$unwind (stage)", ) case aggregations.ErrEmptyVariable, aggregations.ErrInvalidExpression, aggregations.ErrUndefinedVariable: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFieldPathInvalidName, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFieldPathInvalidName, "Expression field names may not start with '$'. Consider using $getField or $setField", "$unwind (stage)", ) @@ -93,8 +93,8 @@ func newUnwind(stage *types.Document) (aggregations.Stage, error) { } } default: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageUnwindWrongType, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageUnwindWrongType, fmt.Sprintf( "expected either a string or an object as specification for $unwind stage, got %s", types.FormatAnyValue(field), diff --git a/internal/handler/common/aggregations/stages/validate.go b/internal/handler/common/aggregations/stages/validate.go index baaf563858c3..756c0ff526af 100644 --- a/internal/handler/common/aggregations/stages/validate.go +++ b/internal/handler/common/aggregations/stages/validate.go @@ -19,7 +19,7 @@ import ( "fmt" "strings" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -93,8 +93,8 @@ func validateArrayExpression(stage string, arr *types.Array) error { func validateFieldPath(stage string, fieldsDoc *types.Document) error { for _, key := range fieldsDoc.Keys() { if strings.HasPrefix(key, "$") { - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFieldPathInvalidName, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFieldPathInvalidName, fmt.Sprintf( "Invalid %s :: caused by :: FieldPath field names may not start with '$'. "+ "Consider using $getField or $setField.", diff --git a/internal/handler/common/client_metadata.go b/internal/handler/common/client_metadata.go index c7ff30c6e9b6..c69ce132ad5a 100644 --- a/internal/handler/common/client_metadata.go +++ b/internal/handler/common/client_metadata.go @@ -18,7 +18,7 @@ import ( "context" "github.com/FerretDB/FerretDB/internal/clientconn/conninfo" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" ) @@ -31,8 +31,8 @@ func CheckClientMetadata(ctx context.Context, doc *types.Document) error { connInfo := conninfo.Get(ctx) if connInfo.MetadataRecv() { - return commonerrors.NewCommandErrorMsg( - commonerrors.ErrClientMetadataCannotBeMutated, + return handlererrors.NewCommandErrorMsg( + handlererrors.ErrClientMetadataCannotBeMutated, "The client metadata document may only be sent in the first hello", ) } diff --git a/internal/handler/common/client_metadata_test.go b/internal/handler/common/client_metadata_test.go index ac3407dd4275..21573175c330 100644 --- a/internal/handler/common/client_metadata_test.go +++ b/internal/handler/common/client_metadata_test.go @@ -21,7 +21,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/FerretDB/FerretDB/internal/clientconn/conninfo" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/must" ) @@ -77,8 +77,8 @@ func TestCheckClientMetadata(t *testing.T) { {document: metadata, recv: true}, { document: metadata, - err: commonerrors.NewCommandErrorMsg( - commonerrors.ErrClientMetadataCannotBeMutated, + err: handlererrors.NewCommandErrorMsg( + handlererrors.ErrClientMetadataCannotBeMutated, "The client metadata document may only be sent in the first hello", ), recv: true, diff --git a/internal/handler/common/count.go b/internal/handler/common/count.go index 8747163923d6..6e1f27afea21 100644 --- a/internal/handler/common/count.go +++ b/internal/handler/common/count.go @@ -17,7 +17,7 @@ package common import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" ) @@ -44,7 +44,7 @@ type CountParams struct { func GetCountParams(document *types.Document, l *zap.Logger) (*CountParams, error) { var count CountParams - err := commonparams.ExtractParams(document, "count", &count, l) + err := handlerparams.ExtractParams(document, "count", &count, l) if err != nil { return nil, err } diff --git a/internal/handler/common/delete_params.go b/internal/handler/common/delete_params.go index f0f485f6b2f2..d19f2484479d 100644 --- a/internal/handler/common/delete_params.go +++ b/internal/handler/common/delete_params.go @@ -17,7 +17,7 @@ package common import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" ) @@ -56,7 +56,7 @@ func GetDeleteParams(document *types.Document, l *zap.Logger) (*DeleteParams, er Ordered: true, } - err := commonparams.ExtractParams(document, "delete", ¶ms, l) + err := handlerparams.ExtractParams(document, "delete", ¶ms, l) if err != nil { return nil, err } diff --git a/internal/handler/common/distinct.go b/internal/handler/common/distinct.go index 40f4fc207955..02c8651fad46 100644 --- a/internal/handler/common/distinct.go +++ b/internal/handler/common/distinct.go @@ -20,9 +20,9 @@ import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" "github.com/FerretDB/FerretDB/internal/handler/commonpath" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -50,7 +50,7 @@ type DistinctParams struct { func GetDistinctParams(document *types.Document, l *zap.Logger) (*DistinctParams, error) { var dp DistinctParams - err := commonparams.ExtractParams(document, "distinct", &dp, l) + err := handlerparams.ExtractParams(document, "distinct", &dp, l) if err != nil { return nil, err } @@ -61,19 +61,19 @@ func GetDistinctParams(document *types.Document, l *zap.Logger) (*DistinctParams case types.NullType, nil: dp.Filter = types.MakeDocument(0) default: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "BSON field 'distinct.query' is the wrong type '%s', expected type 'object'", - commonparams.AliasFromType(dp.Query), + handlerparams.AliasFromType(dp.Query), ), "distinct", ) } if dp.Key == "" { - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrEmptyFieldPath, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrEmptyFieldPath, "FieldPath cannot be constructed with empty string", ) } diff --git a/internal/handler/common/explain.go b/internal/handler/common/explain.go index 695a7a366f8e..857c2b1cbf3f 100644 --- a/internal/handler/common/explain.go +++ b/internal/handler/common/explain.go @@ -17,8 +17,8 @@ package common import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -95,7 +95,7 @@ func GetExplainParams(document *types.Document, l *zap.Logger) (*ExplainParams, return nil, err } - if limit, err = commonparams.GetValidatedNumberParamWithMinValue("explain", "limit", limit, 0); err != nil { + if limit, err = handlerparams.GetValidatedNumberParamWithMinValue("explain", "limit", limit, 0); err != nil { return nil, err } @@ -103,7 +103,7 @@ func GetExplainParams(document *types.Document, l *zap.Logger) (*ExplainParams, return nil, err } - if skip, err = commonparams.GetValidatedNumberParamWithMinValue("explain", "skip", skip, 0); err != nil { + if skip, err = handlerparams.GetValidatedNumberParamWithMinValue("explain", "skip", skip, 0); err != nil { return nil, err } @@ -114,8 +114,8 @@ func GetExplainParams(document *types.Document, l *zap.Logger) (*ExplainParams, pipeline, err = GetRequiredParam[*types.Array](explain, "pipeline") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrMissingField, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrMissingField, "BSON field 'aggregate.pipeline' is missing but a required field", document.Command(), ) @@ -124,8 +124,8 @@ func GetExplainParams(document *types.Document, l *zap.Logger) (*ExplainParams, stagesDocs = must.NotFail(iterator.ConsumeValues(pipeline.Iterator())) for _, d := range stagesDocs { if _, ok := d.(*types.Document); !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, "Each element of the 'pipeline' array must be an object", document.Command(), ) diff --git a/internal/handler/common/filter.go b/internal/handler/common/filter.go index e7a06df00856..85532f520cca 100644 --- a/internal/handler/common/filter.go +++ b/internal/handler/common/filter.go @@ -23,9 +23,9 @@ import ( "time" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/operators" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" "github.com/FerretDB/FerretDB/internal/handler/commonpath" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -191,16 +191,16 @@ func filterOperator(doc *types.Document, operator string, filterValue any) (bool // {$and: [{expr1}, {expr2}, ...]} exprs, ok := filterValue.(*types.Array) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$and must be an array", operator, ) } if exprs.Len() == 0 { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$and/$or/$nor must be a nonempty array", operator, ) @@ -209,8 +209,8 @@ func filterOperator(doc *types.Document, operator string, filterValue any) (bool for i := 0; i < exprs.Len(); i++ { _, ok := must.NotFail(exprs.Get(i)).(*types.Document) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$or/$and/$nor entries need to be full objects", operator, ) @@ -235,16 +235,16 @@ func filterOperator(doc *types.Document, operator string, filterValue any) (bool // {$or: [{expr1}, {expr2}, ...]} exprs, ok := filterValue.(*types.Array) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$or must be an array", operator, ) } if exprs.Len() == 0 { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$and/$or/$nor must be a nonempty array", operator, ) @@ -253,8 +253,8 @@ func filterOperator(doc *types.Document, operator string, filterValue any) (bool for i := 0; i < exprs.Len(); i++ { _, ok := must.NotFail(exprs.Get(i)).(*types.Document) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$or/$and/$nor entries need to be full objects", operator, ) @@ -279,16 +279,16 @@ func filterOperator(doc *types.Document, operator string, filterValue any) (bool // {$nor: [{expr1}, {expr2}, ...]} exprs, ok := filterValue.(*types.Array) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$nor must be an array", operator, ) } if exprs.Len() == 0 { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$and/$or/$nor must be a nonempty array", operator, ) @@ -297,8 +297,8 @@ func filterOperator(doc *types.Document, operator string, filterValue any) (bool for i := 0; i < exprs.Len(); i++ { _, ok := must.NotFail(exprs.Get(i)).(*types.Document) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$or/$and/$nor entries need to be full objects", operator, ) @@ -331,7 +331,7 @@ func filterOperator(doc *types.Document, operator string, filterValue any) (bool operator, ) - return false, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, msg, "$operator") + return false, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, msg, "$operator") } } @@ -442,8 +442,8 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * return true, nil case types.Regex: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "Can't have regex as arg to $ne.", exprKey, ) @@ -458,7 +458,7 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * // {field: {$gt: exprValue}} if _, ok := exprValue.(types.Regex); ok { msg := fmt.Sprintf(`Can't have RegEx as arg to predicate over field '%s'.`, filterKey) - return false, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, msg, exprKey) + return false, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, msg, exprKey) } // Array and non-array comparison with $gt compares the non-array @@ -483,7 +483,7 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * // {field: {$gte: exprValue}} if _, ok := exprValue.(types.Regex); ok { msg := fmt.Sprintf(`Can't have RegEx as arg to predicate over field '%s'.`, filterKey) - return false, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, msg, exprKey) + return false, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, msg, exprKey) } // Array and non-array comparison with $gte compares the non-array @@ -507,7 +507,7 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * // {field: {$lt: exprValue}} if _, ok := exprValue.(types.Regex); ok { msg := fmt.Sprintf(`Can't have RegEx as arg to predicate over field '%s'.`, filterKey) - return false, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, msg, exprKey) + return false, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, msg, exprKey) } // Array and non-array comparison with $lt compares the non-array @@ -532,7 +532,7 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * // {field: {$lte: exprValue}} if _, ok := exprValue.(types.Regex); ok { msg := fmt.Sprintf(`Can't have RegEx as arg to predicate over field '%s'.`, filterKey) - return false, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, msg, exprKey) + return false, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, msg, exprKey) } // Array and non-array comparison with $lte compares the non-array @@ -557,7 +557,11 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * // {field: {$in: [value1, value2, ...]}} arr, ok := exprValue.(*types.Array) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, "$in needs an array", exprKey) + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, + "$in needs an array", + exprKey, + ) } var found bool @@ -570,8 +574,8 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * case *types.Document: for _, key := range arrValue.Keys() { if strings.HasPrefix(key, "$") { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "cannot nest $ under $in", exprKey, ) @@ -607,8 +611,8 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * // {field: {$nin: [value1, value2, ...]}} arr, ok := exprValue.(*types.Array) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$nin needs an array", exprKey, ) @@ -624,8 +628,8 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * case *types.Document: for _, key := range arrValue.Keys() { if strings.HasPrefix(key, "$") { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "cannot nest $ under $in", exprKey, ) @@ -672,8 +676,8 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * return false, err } default: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$not needs a regex or a document", exprKey, ) @@ -758,8 +762,8 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * } default: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("unknown operator: %s", exprKey), "$operator", ) @@ -774,8 +778,8 @@ func filterFieldExpr(doc *types.Document, filterKey, filterSuffix string, expr * func filterFieldRegex(fieldValue any, regex types.Regex) (bool, error) { for _, option := range regex.Options { if !slices.Contains([]rune{'i', 'm', 's', 'x'}, option) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadRegexOption, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadRegexOption, fmt.Sprintf(" invalid flag in regex options: %c", option), "$options", ) @@ -784,15 +788,15 @@ func filterFieldRegex(fieldValue any, regex types.Regex) (bool, error) { re, err := regex.Compile() if err != nil && err == types.ErrOptionNotImplemented { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, `option 'x' not implemented`, "$options", ) } if err != nil { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrRegexMissingParen, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrRegexMissingParen, err.Error(), "$regex", ) @@ -828,8 +832,8 @@ func filterFieldExprRegex(fieldValue any, regexValue, optionsValue any) (bool, e if optionsValue != nil { var ok bool if options, ok = optionsValue.(string); !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$options has to be a string", "$options", ) @@ -847,8 +851,8 @@ func filterFieldExprRegex(fieldValue any, regexValue, optionsValue any) (bool, e case types.Regex: if options != "" { if regexValue.Options != "" { - return false, commonerrors.NewCommandErrorMsg( - commonerrors.ErrRegexOptions, + return false, handlererrors.NewCommandErrorMsg( + handlererrors.ErrRegexOptions, "options set in both $regex and $options", ) } @@ -857,8 +861,8 @@ func filterFieldExprRegex(fieldValue any, regexValue, optionsValue any) (bool, e return filterFieldRegex(fieldValue, regexValue) default: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$regex has to be a string", "$regex", ) @@ -867,24 +871,24 @@ func filterFieldExprRegex(fieldValue any, regexValue, optionsValue any) (bool, e // filterFieldExprSize handles {field: {$size: sizeValue}} filter. func filterFieldExprSize(fieldValue any, sizeValue any) (bool, error) { - size, err := commonparams.GetWholeNumberParam(sizeValue) + size, err := handlerparams.GetWholeNumberParam(sizeValue) if err != nil { switch err { - case commonparams.ErrUnexpectedType: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + case handlerparams.ErrUnexpectedType: + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Failed to parse $size. Expected a number in: $size: %s`, types.FormatAnyValue(sizeValue)), "$size", ) - case commonparams.ErrNotWholeNumber: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + case handlerparams.ErrNotWholeNumber: + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Failed to parse $size. Expected an integer: $size: %s`, types.FormatAnyValue(sizeValue)), "$size", ) - case commonparams.ErrInfinity: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + case handlerparams.ErrInfinity: + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf( `Failed to parse $size. Cannot represent as a 64-bit integer: $size: %s`, types.FormatAnyValue(sizeValue), @@ -897,8 +901,8 @@ func filterFieldExprSize(fieldValue any, sizeValue any) (bool, error) { } if size < 0 { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf( `Failed to parse $size. Expected a non-negative number in: $size: %s`, types.FormatAnyValue(sizeValue), @@ -925,7 +929,7 @@ func filterFieldExprSize(fieldValue any, sizeValue any) (bool, error) { func filterFieldExprAll(fieldValue any, allValue any) (bool, error) { query, ok := allValue.(*types.Array) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, "$all needs an array", "$all") + return false, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, "$all needs an array", "$all") } if query.Len() == 0 { @@ -971,8 +975,8 @@ func filterFieldExprBitsAllClear(fieldValue, maskValue any) (bool, error) { case types.Binary: // TODO https://github.com/FerretDB/FerretDB/issues/508 - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "BinData() not supported yet", "$bitsAllClear", ) @@ -1005,8 +1009,8 @@ func filterFieldExprBitsAllSet(fieldValue, maskValue any) (bool, error) { case types.Binary: // TODO https://github.com/FerretDB/FerretDB/issues/508 - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "BinData() not supported yet", "$bitsAllSet", ) @@ -1039,8 +1043,8 @@ func filterFieldExprBitsAnyClear(fieldValue, maskValue any) (bool, error) { case types.Binary: // TODO https://github.com/FerretDB/FerretDB/issues/508 - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "BinData() not supported yet", "$bitsAnyClear", ) @@ -1073,8 +1077,8 @@ func filterFieldExprBitsAnySet(fieldValue, maskValue any) (bool, error) { case types.Binary: // TODO https://github.com/FerretDB/FerretDB/issues/508 - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "BinData() not supported yet", "$bitsAnySet", ) @@ -1107,15 +1111,15 @@ func isInvalidBitwiseValue(value float64) bool { func filterFieldMod(fieldValue, exprValue any) (bool, error) { arr := exprValue.(*types.Array) if arr.Len() < 2 { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `malformed mod, not enough elements`, "$mod", ) } if arr.Len() > 2 { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `malformed mod, too many elements`, "$mod", ) @@ -1125,8 +1129,8 @@ func filterFieldMod(fieldValue, exprValue any) (bool, error) { switch d := must.NotFail(arr.Get(0)).(type) { case float64: if math.IsNaN(d) || math.IsInf(d, 0) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `malformed mod, divisor value is invalid :: caused by :: `+`Unable to coerce NaN/Inf to integral type`, "$mod", ) @@ -1134,8 +1138,8 @@ func filterFieldMod(fieldValue, exprValue any) (bool, error) { d = math.Trunc(d) if d >= float64(math.MaxInt64) || d < float64(math.MinInt64) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `malformed mod, divisor value is invalid :: caused by :: `+`Out of bounds coercing to integral value`, "$mod", ) @@ -1150,8 +1154,8 @@ func filterFieldMod(fieldValue, exprValue any) (bool, error) { divisor = d default: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `malformed mod, divisor not a number`, "$mod", ) @@ -1160,8 +1164,8 @@ func filterFieldMod(fieldValue, exprValue any) (bool, error) { switch r := must.NotFail(arr.Get(1)).(type) { case float64: if math.IsNaN(r) || math.IsInf(r, 0) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `malformed mod, remainder value is invalid :: caused by :: `+ `Unable to coerce NaN/Inf to integral type`, "$mod", ) @@ -1170,8 +1174,8 @@ func filterFieldMod(fieldValue, exprValue any) (bool, error) { r = math.Trunc(r) if r >= float64(math.MaxInt64) || r < float64(math.MinInt64) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `malformed mod, remainder value is invalid :: caused by :: `+ `Out of bounds coercing to integral value`, "$mod", ) @@ -1186,16 +1190,16 @@ func filterFieldMod(fieldValue, exprValue any) (bool, error) { remainder = r default: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `malformed mod, remainder not a number`, "$mod", ) } if divisor == 0 { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `divisor cannot be 0`, "$mod", ) @@ -1253,7 +1257,7 @@ func filterFieldExprExists(fieldExist bool, exprValue any) (bool, error) { func filterFieldExprType(fieldValue, exprValue any) (bool, error) { switch exprValue := exprValue.(type) { case *types.Array: - hasSameType := commonparams.HasSameTypeElements(exprValue) + hasSameType := handlerparams.HasSameTypeElements(exprValue) for i := 0; i < exprValue.Len(); i++ { exprValue := must.NotFail(exprValue.Get(i)) @@ -1261,21 +1265,21 @@ func filterFieldExprType(fieldValue, exprValue any) (bool, error) { switch exprValue := exprValue.(type) { case float64: if math.IsNaN(exprValue) || math.IsInf(exprValue, 0) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `Invalid numerical type code: `+strings.Trim(strings.ToLower(fmt.Sprintf("%v", exprValue)), "+"), "$type", ) } if exprValue != math.Trunc(exprValue) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Invalid numerical type code: %v`, exprValue), "$type", ) } - code, err := commonparams.NewTypeCode(int32(exprValue)) + code, err := handlerparams.NewTypeCode(int32(exprValue)) if err != nil { return false, err } @@ -1293,7 +1297,7 @@ func filterFieldExprType(fieldValue, exprValue any) (bool, error) { } case string: - code, err := commonparams.ParseTypeCode(exprValue) + code, err := handlerparams.ParseTypeCode(exprValue) if err != nil { return false, err } @@ -1305,7 +1309,7 @@ func filterFieldExprType(fieldValue, exprValue any) (bool, error) { return true, nil } case int32: - code, err := commonparams.NewTypeCode(exprValue) + code, err := handlerparams.NewTypeCode(exprValue) if err != nil { return false, err } @@ -1322,8 +1326,8 @@ func filterFieldExprType(fieldValue, exprValue any) (bool, error) { return true, nil } default: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Invalid numerical type code: %s`, exprValue), "$type", ) @@ -1333,21 +1337,21 @@ func filterFieldExprType(fieldValue, exprValue any) (bool, error) { case float64: if math.IsNaN(exprValue) || math.IsInf(exprValue, 0) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `Invalid numerical type code: `+strings.Trim(strings.ToLower(fmt.Sprintf("%v", exprValue)), "+"), "$type", ) } if exprValue != math.Trunc(exprValue) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Invalid numerical type code: %v`, exprValue), "$type", ) } - code, err := commonparams.NewTypeCode(int32(exprValue)) + code, err := handlerparams.NewTypeCode(int32(exprValue)) if err != nil { return false, err } @@ -1355,7 +1359,7 @@ func filterFieldExprType(fieldValue, exprValue any) (bool, error) { return filterFieldValueByTypeCode(fieldValue, code) case string: - code, err := commonparams.ParseTypeCode(exprValue) + code, err := handlerparams.ParseTypeCode(exprValue) if err != nil { return false, err } @@ -1363,7 +1367,7 @@ func filterFieldExprType(fieldValue, exprValue any) (bool, error) { return filterFieldValueByTypeCode(fieldValue, code) case int32: - code, err := commonparams.NewTypeCode(exprValue) + code, err := handlerparams.NewTypeCode(exprValue) if err != nil { return false, err } @@ -1371,8 +1375,8 @@ func filterFieldExprType(fieldValue, exprValue any) (bool, error) { return filterFieldValueByTypeCode(fieldValue, code) default: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Invalid numerical type code: %v`, exprValue), "$type", ) @@ -1380,9 +1384,9 @@ func filterFieldExprType(fieldValue, exprValue any) (bool, error) { } // filterFieldValueByTypeCode filters fieldValue by given type code. -func filterFieldValueByTypeCode(fieldValue any, code commonparams.TypeCode) (bool, error) { +func filterFieldValueByTypeCode(fieldValue any, code handlerparams.TypeCode) (bool, error) { // check types.Array elements for match to given code. - if array, ok := fieldValue.(*types.Array); ok && code != commonparams.TypeCodeArray { + if array, ok := fieldValue.(*types.Array); ok && code != handlerparams.TypeCodeArray { for i := 0; i < array.Len(); i++ { value, _ := array.Get(i) @@ -1403,59 +1407,59 @@ func filterFieldValueByTypeCode(fieldValue any, code commonparams.TypeCode) (boo } switch code { - case commonparams.TypeCodeArray: + case handlerparams.TypeCodeArray: if _, ok := fieldValue.(*types.Array); !ok { return false, nil } - case commonparams.TypeCodeObject: + case handlerparams.TypeCodeObject: if _, ok := fieldValue.(*types.Document); !ok { return false, nil } - case commonparams.TypeCodeDouble: + case handlerparams.TypeCodeDouble: if _, ok := fieldValue.(float64); !ok { return false, nil } - case commonparams.TypeCodeString: + case handlerparams.TypeCodeString: if _, ok := fieldValue.(string); !ok { return false, nil } - case commonparams.TypeCodeBinData: + case handlerparams.TypeCodeBinData: if _, ok := fieldValue.(types.Binary); !ok { return false, nil } - case commonparams.TypeCodeObjectID: + case handlerparams.TypeCodeObjectID: if _, ok := fieldValue.(types.ObjectID); !ok { return false, nil } - case commonparams.TypeCodeBool: + case handlerparams.TypeCodeBool: if _, ok := fieldValue.(bool); !ok { return false, nil } - case commonparams.TypeCodeDate: + case handlerparams.TypeCodeDate: if _, ok := fieldValue.(time.Time); !ok { return false, nil } - case commonparams.TypeCodeNull: + case handlerparams.TypeCodeNull: if _, ok := fieldValue.(types.NullType); !ok { return false, nil } - case commonparams.TypeCodeRegex: + case handlerparams.TypeCodeRegex: if _, ok := fieldValue.(types.Regex); !ok { return false, nil } - case commonparams.TypeCodeInt: + case handlerparams.TypeCodeInt: if _, ok := fieldValue.(int32); !ok { return false, nil } - case commonparams.TypeCodeTimestamp: + case handlerparams.TypeCodeTimestamp: if _, ok := fieldValue.(types.Timestamp); !ok { return false, nil } - case commonparams.TypeCodeLong: + case handlerparams.TypeCodeLong: if _, ok := fieldValue.(int64); !ok { return false, nil } - case commonparams.TypeCodeNumber: + case handlerparams.TypeCodeNumber: // TypeCodeNumber should match int32, int64 and float64 types switch fieldValue.(type) { case float64, int32, int64: @@ -1463,15 +1467,15 @@ func filterFieldValueByTypeCode(fieldValue any, code commonparams.TypeCode) (boo default: return false, nil } - case commonparams.TypeCodeDecimal, commonparams.TypeCodeMinKey, commonparams.TypeCodeMaxKey: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + case handlerparams.TypeCodeDecimal, handlerparams.TypeCodeMinKey, handlerparams.TypeCodeMaxKey: + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, fmt.Sprintf(`Type code %v not implemented`, code), "$type", ) default: - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Unknown type name alias: %s`, code.String()), "$type", ) @@ -1485,8 +1489,8 @@ func filterFieldValueByTypeCode(fieldValue any, code commonparams.TypeCode) (boo func filterFieldExprElemMatch(doc *types.Document, filterKey, filterSuffix string, exprValue any) (bool, error) { expr, ok := exprValue.(*types.Document) if !ok { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "$elemMatch needs an Object", "$elemMatch", ) @@ -1494,8 +1498,8 @@ func filterFieldExprElemMatch(doc *types.Document, filterKey, filterSuffix strin for _, key := range expr.Keys() { if slices.Contains([]string{"$text", "$where"}, key) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%s can only be applied to the top-level document", key), "$elemMatch", ) @@ -1503,8 +1507,8 @@ func filterFieldExprElemMatch(doc *types.Document, filterKey, filterSuffix strin // TODO https://github.com/FerretDB/FerretDB/issues/730 if slices.Contains([]string{"$and", "$or", "$nor"}, key) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, fmt.Sprintf("$elemMatch: support for %s not implemented yet", key), "$elemMatch", ) @@ -1512,16 +1516,16 @@ func filterFieldExprElemMatch(doc *types.Document, filterKey, filterSuffix strin // TODO https://github.com/FerretDB/FerretDB/issues/731 if slices.Contains([]string{"$ne", "$not"}, key) { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, fmt.Sprintf("$elemMatch: support for %s not implemented yet", key), "$elemMatch", ) } if expr.Len() > 1 && !strings.HasPrefix(key, "$") { - return false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("unknown operator: %s", key), "$elemMatch", ) diff --git a/internal/handler/common/find.go b/internal/handler/common/find.go index 8f87173fc401..709eba144aa8 100644 --- a/internal/handler/common/find.go +++ b/internal/handler/common/find.go @@ -19,8 +19,8 @@ import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" ) @@ -65,12 +65,12 @@ func GetFindParams(doc *types.Document, l *zap.Logger) (*FindParams, error) { BatchSize: 101, } - err := commonparams.ExtractParams(doc, "find", ¶ms, l) + err := handlerparams.ExtractParams(doc, "find", ¶ms, l) - var ce *commonerrors.CommandError + var ce *handlererrors.CommandError if errors.As(err, &ce) { - if ce.Code() == commonerrors.ErrInvalidNamespace { - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, ce.Err().Error(), "find") + if ce.Code() == handlererrors.ErrInvalidNamespace { + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, ce.Err().Error(), "find") } } diff --git a/internal/handler/common/findandmodify.go b/internal/handler/common/findandmodify.go index b4f3f1deeb96..e96b1a4cd399 100644 --- a/internal/handler/common/findandmodify.go +++ b/internal/handler/common/findandmodify.go @@ -20,8 +20,8 @@ import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/must" ) @@ -89,28 +89,28 @@ type UpsertParams struct { func GetFindAndModifyParams(doc *types.Document, l *zap.Logger) (*FindAndModifyParams, error) { var params FindAndModifyParams - err := commonparams.ExtractParams(doc, "findAndModify", ¶ms, l) + err := handlerparams.ExtractParams(doc, "findAndModify", ¶ms, l) if err != nil { return nil, err } if params.Collection == "" { - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrInvalidNamespace, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrInvalidNamespace, fmt.Sprintf("Invalid namespace specified '%s.'", params.DB), ) } if params.UpdateValue == nil && !params.Remove { - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrFailedToParse, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrFailedToParse, "Either an update or remove=true must be specified", ) } if params.ReturnNewDocument && params.Remove { - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrFailedToParse, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrFailedToParse, "Cannot specify both new=true and remove=true; 'remove' always returns the deleted document", ) } @@ -120,14 +120,14 @@ func GetFindAndModifyParams(doc *types.Document, l *zap.Logger) (*FindAndModifyP case *types.Document: params.Update = updateParam case *types.Array: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "Aggregation pipelines are not supported yet", "update", ) default: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, "Update argument must be either an object or an array", "update", ) @@ -135,15 +135,15 @@ func GetFindAndModifyParams(doc *types.Document, l *zap.Logger) (*FindAndModifyP } if params.Update != nil && params.Remove { - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrFailedToParse, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrFailedToParse, "Cannot specify both an update and remove=true", ) } if params.Upsert && params.Remove { - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrFailedToParse, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrFailedToParse, "Cannot specify both upsert=true and remove=true", ) } @@ -245,8 +245,8 @@ func prepareDocumentForUpdate(docs []*types.Document, params *FindAndModifyParam for _, doc := range docs { id := must.NotFail(doc.Get("_id")) if id != upsertID { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrImmutableField, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrImmutableField, fmt.Sprintf( `Plan executor error during findAndModify :: caused `+ `by :: After applying the update, the (immutable) field `+ @@ -315,8 +315,8 @@ func hasFilterOperator(query *types.Document) (string, bool, error) { } if hasOp { - return "", false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrDollarPrefixedFieldName, + return "", false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrDollarPrefixedFieldName, fmt.Sprintf( "Plan executor error during findAndModify :: "+ "caused by :: _id fields may not contain '$'-prefixed "+ diff --git a/internal/handler/common/getparameter.go b/internal/handler/common/getparameter.go index 17534598b2a0..1d188d156af5 100644 --- a/internal/handler/common/getparameter.go +++ b/internal/handler/common/getparameter.go @@ -20,8 +20,8 @@ import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -81,8 +81,8 @@ func GetParameter(_ context.Context, msg *wire.OpMsg, l *zap.Logger) (*wire.OpMs } if resDoc.Len() < 1 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrorCode(0), + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrorCode(0), "no option found to get", document.Command(), ) @@ -138,14 +138,14 @@ func extractGetParameter(getParameter any) (showDetails, allParameters bool, err if param, ok := getParameter.(*types.Document); ok { if v, _ := param.Get("showDetails"); v != nil { - showDetails, err = commonparams.GetBoolOptionalParam("showDetails", v) + showDetails, err = handlerparams.GetBoolOptionalParam("showDetails", v) if err != nil { return false, false, lazyerrors.Error(err) } } if v, _ := param.Get("allParameters"); v != nil { - allParameters, err = commonparams.GetBoolOptionalParam("allParameters", v) + allParameters, err = handlerparams.GetBoolOptionalParam("allParameters", v) if err != nil { return false, false, lazyerrors.Error(err) } diff --git a/internal/handler/common/insert.go b/internal/handler/common/insert.go index 565f87c67db1..e8faed0266b1 100644 --- a/internal/handler/common/insert.go +++ b/internal/handler/common/insert.go @@ -19,8 +19,8 @@ import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/must" ) @@ -44,7 +44,7 @@ func GetInsertParams(document *types.Document, l *zap.Logger) (*InsertParams, er Ordered: true, } - err := commonparams.ExtractParams(document, "insert", ¶ms, l) + err := handlerparams.ExtractParams(document, "insert", ¶ms, l) if err != nil { return nil, err } @@ -53,12 +53,12 @@ func GetInsertParams(document *types.Document, l *zap.Logger) (*InsertParams, er doc := must.NotFail(params.Docs.Get(i)) if _, ok := doc.(*types.Document); !ok { - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "BSON field 'insert.documents.%d' is the wrong type '%s', expected type 'object'", i, - commonparams.AliasFromType(doc), + handlerparams.AliasFromType(doc), ), ) } diff --git a/internal/handler/common/params.go b/internal/handler/common/params.go index fb1f89a49f93..271ef0809a33 100644 --- a/internal/handler/common/params.go +++ b/internal/handler/common/params.go @@ -19,8 +19,8 @@ import ( "fmt" "math" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -34,13 +34,13 @@ func GetRequiredParam[T types.Type](doc *types.Document, key string) (T, error) v, _ := doc.Get(key) if v == nil { msg := fmt.Sprintf("required parameter %q is missing", key) - return zero, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, msg, key) + return zero, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, msg, key) } res, ok := v.(T) if !ok { msg := fmt.Sprintf("required parameter %q has type %T (expected %T)", key, v, zero) - return zero, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, msg, key) + return zero, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, msg, key) } return res, nil @@ -59,10 +59,10 @@ func GetOptionalParam[T types.Type](doc *types.Document, key string, defaultValu if !ok { msg := fmt.Sprintf( `BSON field '%s' is the wrong type '%s', expected type '%s'`, - key, commonparams.AliasFromType(v), commonparams.AliasFromType(defaultValue), + key, handlerparams.AliasFromType(v), handlerparams.AliasFromType(defaultValue), ) - return defaultValue, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrTypeMismatch, msg, key) + return defaultValue, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrTypeMismatch, msg, key) } return res, nil @@ -88,13 +88,13 @@ func GetOptionalNullParam[T types.Type](doc *types.Document, key string, default // // d, ok := value.(*types.Document) // if !ok { -// return commonerrors.NewCommandErrorMsg(commonerrors.ErrBadValue, "expected document") +// return handlererrors.NewCommandErrorMsg(handlererrors.ErrBadValue, "expected document") // } func AssertType[T types.Type](value any) (T, error) { res, ok := value.(T) if !ok { msg := fmt.Sprintf("got type %T, expected %T", value, res) - return res, commonerrors.NewCommandErrorMsg(commonerrors.ErrBadValue, msg) + return res, handlererrors.NewCommandErrorMsg(handlererrors.ErrBadValue, msg) } return res, nil @@ -107,14 +107,14 @@ func GetLimitParam(doc *types.Document) (int64, error) { return 0, nil } - res, err := commonparams.GetWholeNumberParam(v) + res, err := handlerparams.GetWholeNumberParam(v) if err != nil { msg := fmt.Sprintf( `BSON field '%s' is the wrong type '%s', expected type '%s'`, - "limit", commonparams.AliasFromType(v), commonparams.AliasFromType(res), + "limit", handlerparams.AliasFromType(v), handlerparams.AliasFromType(res), ) - return res, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrTypeMismatch, msg, "limit") + return res, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrTypeMismatch, msg, "limit") } return res, nil @@ -123,25 +123,25 @@ func GetLimitParam(doc *types.Document) (int64, error) { // GetLimitStageParam returns $limit stage argument from the provided value. // It returns the proper error if value doesn't meet requirements. func GetLimitStageParam(value any) (int64, error) { - limit, err := commonparams.GetWholeNumberParam(value) + limit, err := handlerparams.GetWholeNumberParam(value) switch { case err == nil: - case errors.Is(err, commonparams.ErrUnexpectedType): - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageLimitInvalidArg, + case errors.Is(err, handlerparams.ErrUnexpectedType): + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageLimitInvalidArg, fmt.Sprintf("invalid argument to $limit stage: Expected a number in: $limit: %#v", value), "$limit (stage)", ) - case errors.Is(err, commonparams.ErrNotWholeNumber), errors.Is(err, commonparams.ErrInfinity): - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageLimitInvalidArg, + case errors.Is(err, handlerparams.ErrNotWholeNumber), errors.Is(err, handlerparams.ErrInfinity): + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageLimitInvalidArg, fmt.Sprintf("invalid argument to $limit stage: Expected an integer: $limit: %#v", value), "$limit (stage)", ) - case errors.Is(err, commonparams.ErrLongExceededPositive), errors.Is(err, commonparams.ErrLongExceededNegative): - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageLimitInvalidArg, + case errors.Is(err, handlerparams.ErrLongExceededPositive), errors.Is(err, handlerparams.ErrLongExceededNegative): + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageLimitInvalidArg, fmt.Sprintf("invalid argument to $limit stage: Cannot represent as a 64-bit integer: $limit: %#v", value), "$limit (stage)", ) @@ -151,14 +151,14 @@ func GetLimitStageParam(value any) (int64, error) { switch { case limit < 0: - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageLimitInvalidArg, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageLimitInvalidArg, fmt.Sprintf("invalid argument to $limit stage: Expected a non-negative number in: $limit: %#v", limit), "$limit (stage)", ) case limit == 0: - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageLimitZero, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageLimitZero, "The limit must be positive", "$limit (stage)", ) @@ -170,21 +170,21 @@ func GetLimitStageParam(value any) (int64, error) { // GetSkipStageParam returns $skip stage argument from the provided value. // It returns the proper error if value doesn't meet requirements. func GetSkipStageParam(value any) (int64, error) { - limit, err := commonparams.GetWholeNumberParam(value) + limit, err := handlerparams.GetWholeNumberParam(value) switch { case err == nil: - case errors.Is(err, commonparams.ErrNotWholeNumber), - errors.Is(err, commonparams.ErrInfinity), - errors.Is(err, commonparams.ErrUnexpectedType): - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageSkipBadValue, + case errors.Is(err, handlerparams.ErrNotWholeNumber), + errors.Is(err, handlerparams.ErrInfinity), + errors.Is(err, handlerparams.ErrUnexpectedType): + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageSkipBadValue, fmt.Sprintf("invalid argument to $skip stage: Expected an integer: $skip: %#v", value), "$skip (stage)", ) - case errors.Is(err, commonparams.ErrLongExceededPositive), errors.Is(err, commonparams.ErrLongExceededNegative): - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageSkipBadValue, + case errors.Is(err, handlerparams.ErrLongExceededPositive), errors.Is(err, handlerparams.ErrLongExceededNegative): + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageSkipBadValue, fmt.Sprintf("invalid argument to $skip stage: Cannot represent as a 64-bit integer: $skip: %#v", value), "$skip (stage)", ) @@ -194,8 +194,8 @@ func GetSkipStageParam(value any) (int64, error) { switch { case limit < 0: - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrStageSkipBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrStageSkipBadValue, fmt.Sprintf("invalid argument to $skip stage: Expected a non-negative number in: $skip: %#v", limit), "$skip (stage)", ) @@ -215,19 +215,19 @@ func getBinaryMaskParam(operator string, mask any) (uint64, error) { for i := 0; i < mask.Len(); i++ { val := must.NotFail(mask.Get(i)) - b, err := commonparams.GetWholeNumberParam(val) + b, err := handlerparams.GetWholeNumberParam(val) if err != nil { switch { - case errors.Is(err, commonparams.ErrNotWholeNumber), errors.Is(err, commonparams.ErrInfinity), - errors.Is(err, commonparams.ErrLongExceededPositive), errors.Is(err, commonparams.ErrLongExceededNegative): - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + case errors.Is(err, handlerparams.ErrNotWholeNumber), errors.Is(err, handlerparams.ErrInfinity), + errors.Is(err, handlerparams.ErrLongExceededPositive), errors.Is(err, handlerparams.ErrLongExceededNegative): + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Failed to parse bit position. Expected an integer: %d: %#v`, i, val), operator, ) default: - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Failed to parse bit position. Expected a number in: %d: %#v`, i, val), operator, ) @@ -235,8 +235,8 @@ func getBinaryMaskParam(operator string, mask any) (uint64, error) { } if b < 0 { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("Failed to parse bit position. Expected a non-negative number in: %d: %d", i, b), operator, ) @@ -248,16 +248,16 @@ func getBinaryMaskParam(operator string, mask any) (uint64, error) { case float64: // {field: {$bitsAllClear: bitmask}} if mask != math.Trunc(mask) || math.IsInf(mask, 0) { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, fmt.Sprintf("Expected an integer: %s: %#v", operator, mask), operator, ) } if mask < 0 { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, fmt.Sprintf(`Expected a non-negative number in: %s: %.1f`, operator, mask), operator, ) @@ -284,8 +284,8 @@ func getBinaryMaskParam(operator string, mask any) (uint64, error) { case int32: // {field: {$bitsAllClear: bitmask}} if mask < 0 { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, fmt.Sprintf(`Expected a non-negative number in: %s: %v`, operator, mask), operator, ) @@ -296,8 +296,8 @@ func getBinaryMaskParam(operator string, mask any) (uint64, error) { case int64: // {field: {$bitsAllClear: bitmask}} if mask < 0 { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, fmt.Sprintf(`Expected a non-negative number in: %s: %v`, operator, mask), operator, ) @@ -306,8 +306,8 @@ func getBinaryMaskParam(operator string, mask any) (uint64, error) { bitmask = uint64(mask) default: - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`value takes an Array, a number, or a BinData but received: %s: %#v`, operator, mask), operator, ) @@ -330,7 +330,7 @@ func addNumbers(v1, v2 any) (any, error) { case int64: return v1 + float64(v2), nil default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } case int32: switch v2 := v2.(type) { @@ -349,17 +349,17 @@ func addNumbers(v1, v2 any) (any, error) { case int64: if v2 > 0 { if int64(v1) > math.MaxInt64-v2 { - return nil, commonparams.ErrLongExceededPositive + return nil, handlerparams.ErrLongExceededPositive } } else { if int64(v1) < math.MinInt64-v2 { - return nil, commonparams.ErrLongExceededNegative + return nil, handlerparams.ErrLongExceededNegative } } return v2 + int64(v1), nil default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } case int64: switch v2 := v2.(type) { @@ -368,11 +368,11 @@ func addNumbers(v1, v2 any) (any, error) { case int32: if v2 > 0 { if v1 > math.MaxInt64-int64(v2) { - return nil, commonparams.ErrIntExceeded + return nil, handlerparams.ErrIntExceeded } } else { if v1 < math.MinInt64-int64(v2) { - return nil, commonparams.ErrIntExceeded + return nil, handlerparams.ErrIntExceeded } } @@ -380,20 +380,20 @@ func addNumbers(v1, v2 any) (any, error) { case int64: if v2 > 0 { if v1 > math.MaxInt64-v2 { - return nil, commonparams.ErrLongExceededPositive + return nil, handlerparams.ErrLongExceededPositive } } else { if v1 < math.MinInt64-v2 { - return nil, commonparams.ErrLongExceededNegative + return nil, handlerparams.ErrLongExceededNegative } } return v1 + v2, nil default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } default: - return nil, commonparams.ErrUnexpectedLeftOpType + return nil, handlerparams.ErrUnexpectedLeftOpType } } @@ -415,7 +415,7 @@ func multiplyNumbers(v1, v2 any) (any, error) { case int64: res = v1 * float64(v2) default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } return res, nil @@ -434,7 +434,7 @@ func multiplyNumbers(v1, v2 any) (any, error) { return multiplyLongSafely(int64(v1), v2) default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } case int64: switch v2 := v2.(type) { @@ -443,7 +443,7 @@ func multiplyNumbers(v1, v2 any) (any, error) { case int32: v, err := multiplyLongSafely(v1, int64(v2)) if err != nil { - return 0, commonparams.ErrIntExceeded + return 0, handlerparams.ErrIntExceeded } return v, nil @@ -451,10 +451,10 @@ func multiplyNumbers(v1, v2 any) (any, error) { return multiplyLongSafely(v1, v2) default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } default: - return nil, commonparams.ErrUnexpectedLeftOpType + return nil, handlerparams.ErrUnexpectedLeftOpType } } @@ -473,12 +473,12 @@ func multiplyLongSafely(v1, v2 int64) (int64, error) { // This check is necessary only for MinInt64, as multiplying MinInt64 by -1 // results in overflow with the MinInt64 as result. case v1 == math.MinInt64 || v2 == math.MinInt64: - return 0, commonparams.ErrLongExceededNegative + return 0, handlerparams.ErrLongExceededNegative } res := v1 * v2 if res/v2 != v1 { - return 0, commonparams.ErrLongExceededPositive + return 0, handlerparams.ErrLongExceededPositive } return res, nil @@ -499,7 +499,7 @@ func performBitLogic(bitOp string, v1, v2 any) (any, error) { case int64: return int64(v1) & v2, nil default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } case int64: @@ -509,11 +509,11 @@ func performBitLogic(bitOp string, v1, v2 any) (any, error) { case int64: return v1 & v2, nil default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } default: - return nil, commonparams.ErrUnexpectedLeftOpType + return nil, handlerparams.ErrUnexpectedLeftOpType } case "or": @@ -525,7 +525,7 @@ func performBitLogic(bitOp string, v1, v2 any) (any, error) { case int64: return int64(v1) | v2, nil default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } case int64: @@ -536,11 +536,11 @@ func performBitLogic(bitOp string, v1, v2 any) (any, error) { case int64: return v1 | v2, nil default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } default: - return nil, commonparams.ErrUnexpectedLeftOpType + return nil, handlerparams.ErrUnexpectedLeftOpType } case "xor": @@ -552,7 +552,7 @@ func performBitLogic(bitOp string, v1, v2 any) (any, error) { case int64: return int64(v1) ^ v2, nil default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } case int64: @@ -562,11 +562,11 @@ func performBitLogic(bitOp string, v1, v2 any) (any, error) { case int64: return v1 ^ v2, nil default: - return nil, commonparams.ErrUnexpectedRightOpType + return nil, handlerparams.ErrUnexpectedRightOpType } default: - return nil, commonparams.ErrUnexpectedLeftOpType + return nil, handlerparams.ErrUnexpectedLeftOpType } default: diff --git a/internal/handler/common/params_test.go b/internal/handler/common/params_test.go index 9504a99ba250..fa9d217d4046 100644 --- a/internal/handler/common/params_test.go +++ b/internal/handler/common/params_test.go @@ -20,7 +20,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" ) func TestMultiplyLongSafely(t *testing.T) { @@ -53,12 +53,12 @@ func TestMultiplyLongSafely(t *testing.T) { "OverflowLarge": { v1: 1 << 60, v2: 42, - err: commonparams.ErrLongExceededPositive, + err: handlerparams.ErrLongExceededPositive, }, "OverflowMax": { v1: math.MaxInt64, v2: 2, - err: commonparams.ErrLongExceededPositive, + err: handlerparams.ErrLongExceededPositive, }, "MaxMinusOne": { v1: math.MaxInt64, @@ -68,17 +68,17 @@ func TestMultiplyLongSafely(t *testing.T) { "OverflowMaxMinusTwo": { v1: math.MaxInt64, v2: -2, - err: commonparams.ErrLongExceededPositive, + err: handlerparams.ErrLongExceededPositive, }, "OverflowMin": { v1: math.MinInt64, v2: 2, - err: commonparams.ErrLongExceededNegative, + err: handlerparams.ErrLongExceededNegative, }, "OverflowMinMinusOne": { v1: math.MinInt64, v2: -1, - err: commonparams.ErrLongExceededNegative, + err: handlerparams.ErrLongExceededNegative, }, } { name, tc := name, tc diff --git a/internal/handler/common/positional_operator.go b/internal/handler/common/positional_operator.go index 45613eea6cfc..1d4841f2ca7d 100644 --- a/internal/handler/common/positional_operator.go +++ b/internal/handler/common/positional_operator.go @@ -17,7 +17,7 @@ package common import ( "errors" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -39,8 +39,8 @@ import ( // If positional projection is `v.$`, the filter must contain `v` in the filter key such as `{v: 42}`. func getPositionalProjection(arr *types.Array, filter *types.Document, positionalOperatorPath string) (*types.Array, error) { if arr.Len() == 0 || filter.Len() == 0 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadPositionalProjection, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadPositionalProjection, "Executor error during find command :: caused by :: positional operator"+ " '.$' couldn't find a matching element in the array", "projection", @@ -76,14 +76,14 @@ func getPositionalProjection(arr *types.Array, filter *types.Document, positiona filterKey, filterVal, err := iter.Next() if errors.Is(err, iterator.ErrIteratorDone) { if !positionalPathFound { - return nil, commonerrors.NewCommandErrorMsgWithArgument( + return nil, handlererrors.NewCommandErrorMsgWithArgument( // filterKey did not contain path for positional projection. // For example, if the positional operator is "v.$", // the filter must have key `v` such as {"v": 1}. // For nested dot notation such as "v.foo.$", // filter must have key `v.foo` such as {"v.foo": 1}, // and just {"v": 1} is not sufficient. - commonerrors.ErrBadPositionalProjection, + handlererrors.ErrBadPositionalProjection, "Executor error during find command :: caused by :: positional operator"+ " '.$' couldn't find a matching element in the array", "projection", diff --git a/internal/handler/common/projection.go b/internal/handler/common/projection.go index 90724289cac5..11aa9b8106f7 100644 --- a/internal/handler/common/projection.go +++ b/internal/handler/common/projection.go @@ -21,7 +21,7 @@ import ( "strings" "time" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -71,8 +71,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro } if key == "" { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrEmptyFieldPath, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrEmptyFieldPath, "FieldPath cannot be constructed with empty string", "projection", ) @@ -84,15 +84,15 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro path, err := types.NewPathFromString(key) if err != nil { if positionalProjection { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidFieldPath, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidFieldPath, "FieldPath must not end with a '.'.", "projection", ) } - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrPathContainsEmptyElement, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrPathContainsEmptyElement, "FieldPath field names may not be empty strings.", "projection", ) @@ -100,8 +100,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro if path.Len() > 1 && strings.Count(path.TrimSuffix().String(), "$") > 1 { // there cannot be more than one positional operator. - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrWrongPositionalOperatorLocation, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrWrongPositionalOperatorLocation, "Positional projection may only be used at the end, "+ "for example: a.b.$. If the query previously used a form "+ "like a.b.$.d, remove the parts following the '$' and "+ @@ -112,8 +112,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro if key == "$" || strings.HasPrefix(key, "$") { // positional operator cannot be at the prefix. - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFieldPathInvalidName, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFieldPathInvalidName, "FieldPath field names may not start with '$'. Consider using $getField or $setField.", "projection", ) @@ -121,8 +121,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro if path.Len() > 1 && slices.Contains(path.TrimSuffix().Slice(), "$") { // there cannot be a positional operator along the path, can only be at the end. - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrWrongPositionalOperatorLocation, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrWrongPositionalOperatorLocation, "Positional projection may only be used at the end, "+ "for example: a.b.$. If the query previously used a form "+ "like a.b.$.d, remove the parts following the '$' and "+ @@ -135,8 +135,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro if strings.HasPrefix(k, "$") && k != "$" { // arbitrary `$` cannot exist in the path, // `v.$foo` is invalid, `v.$` and `v.foo$` are fine. - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFieldPathInvalidName, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFieldPathInvalidName, "FieldPath field names may not start with '$'. Consider using $getField or $setField.", "projection", ) @@ -147,8 +147,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro switch value := value.(type) { case *types.Document: - return nil, false, commonerrors.NewCommandErrorMsg( - commonerrors.ErrNotImplemented, + return nil, false, handlererrors.NewCommandErrorMsg( + handlererrors.ErrNotImplemented, fmt.Sprintf("projection expression %s is not supported", types.FormatAnyValue(value)), ) case *types.Array, string, types.Binary, types.ObjectID, @@ -180,8 +180,8 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro } if !inclusionField && positionalProjection { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrExclusionPositionalProjection, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrExclusionPositionalProjection, "positional projection cannot be used with exclusion", "projection", ) @@ -203,15 +203,15 @@ func ValidateProjection(projection *types.Document) (*types.Document, bool, erro continue } if *inclusion { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrProjectionExIn, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrProjectionExIn, fmt.Sprintf("Cannot do exclusion on field %s in inclusion projection", key), "projection", ) } - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrProjectionInEx, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrProjectionInEx, fmt.Sprintf("Cannot do inclusion on field %s in exclusion projection", key), "projection", ) @@ -245,8 +245,8 @@ func ProjectDocument(doc, projection, filter *types.Document, inclusion bool) (* switch idValue := idValue.(type) { case *types.Document: // field: { $elemMatch: { field2: value }} - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrCommandNotFound, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrCommandNotFound, fmt.Sprintf("projection %s is not supported", types.FormatAnyValue(idValue), ), @@ -316,8 +316,8 @@ func projectDocumentWithoutID(doc *types.Document, projection, filter *types.Doc switch value := value.(type) { // found in the projection case *types.Document: // field: { $elemMatch: { field2: value }} - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrCommandNotFound, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrCommandNotFound, fmt.Sprintf("projection %s is not supported", types.FormatAnyValue(value), ), @@ -502,8 +502,8 @@ func includeProjection(path types.Path, curIndex int, source any, projected, fil if path.Suffix() == "$" && arr.Len() == 0 { // positional projection only handles one array at the suffix, // path prefixes cannot contain array. - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrElementMismatchPositionalProjection, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrElementMismatchPositionalProjection, "Executor error during find command :: caused by :: positional operator '.$' element mismatch", "projection", ) diff --git a/internal/handler/common/sort.go b/internal/handler/common/sort.go index 625bd4d78777..c753e36fced0 100644 --- a/internal/handler/common/sort.go +++ b/internal/handler/common/sort.go @@ -20,8 +20,8 @@ import ( "sort" "strings" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -45,8 +45,8 @@ func SortDocuments(docs []*types.Document, sortDoc *types.Document) error { fields := strings.Split(sortKey, ".") for _, field := range fields { if strings.HasPrefix(field, "$") { - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFieldPathInvalidName, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFieldPathInvalidName, "FieldPath field names may not start with '$'. Consider using $getField or $setField.", "sort", ) @@ -141,22 +141,22 @@ func (ds *docsSorter) Less(i, j int) bool { // GetSortType determines SortType from input sort value. func GetSortType(key string, value any) (types.SortType, error) { - sortValue, err := commonparams.GetWholeNumberParam(value) + sortValue, err := handlerparams.GetWholeNumberParam(value) if err != nil { switch { - case errors.Is(err, commonparams.ErrUnexpectedType): + case errors.Is(err, handlerparams.ErrUnexpectedType): if _, ok := value.(types.NullType); ok { value = "null" } - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrSortBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrSortBadValue, fmt.Sprintf(`Illegal key in $sort specification: %v: %v`, key, value), "$sort", ) - case errors.Is(err, commonparams.ErrNotWholeNumber): - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrSortBadOrder, + case errors.Is(err, handlerparams.ErrNotWholeNumber): + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrSortBadOrder, "$sort key ordering must be 1 (for ascending) or -1 (for descending)", "$sort", ) @@ -171,8 +171,8 @@ func GetSortType(key string, value any) (types.SortType, error) { case -1: return types.Descending, nil default: - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrSortBadOrder, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrSortBadOrder, "$sort key ordering must be 1 (for ascending) or -1 (for descending)", "$sort", ) diff --git a/internal/handler/common/unimplemented.go b/internal/handler/common/unimplemented.go index 2fe7343da42d..153a23ae5ea5 100644 --- a/internal/handler/common/unimplemented.go +++ b/internal/handler/common/unimplemented.go @@ -19,11 +19,11 @@ import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" ) -// Unimplemented returns commonerrors.ErrNotImplemented if doc has any of the given fields. +// Unimplemented returns handlererrors.ErrNotImplemented if doc has any of the given fields. func Unimplemented(doc *types.Document, fields ...string) error { for _, field := range fields { if v, err := doc.Get(field); err == nil { @@ -32,14 +32,14 @@ func Unimplemented(doc *types.Document, fields ...string) error { doc.Command(), field, v, ) - return commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrNotImplemented, msg, field) + return handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrNotImplemented, msg, field) } } return nil } -// UnimplementedNonDefault returns commonerrors.ErrNotImplemented if doc has given field, +// UnimplementedNonDefault returns handlererrors.ErrNotImplemented if doc has given field, // and isDefault, called with the actual value, returns false. func UnimplementedNonDefault(doc *types.Document, field string, isDefault func(v any) bool) error { v, err := doc.Get(field) @@ -56,7 +56,7 @@ func UnimplementedNonDefault(doc *types.Document, field string, isDefault func(v doc.Command(), field, v, ) - return commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrNotImplemented, msg, field) + return handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrNotImplemented, msg, field) } // Ignored logs a message if doc has any of the given fields. diff --git a/internal/handler/common/update.go b/internal/handler/common/update.go index 41537ba7899d..b69d6ccc7501 100644 --- a/internal/handler/common/update.go +++ b/internal/handler/common/update.go @@ -23,8 +23,8 @@ import ( "strings" "time" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -166,8 +166,8 @@ func UpdateDocument(command string, doc, update *types.Document) (bool, error) { default: if strings.HasPrefix(updateOp, "$") { - return false, commonerrors.NewCommandErrorMsg( - commonerrors.ErrNotImplemented, + return false, handlererrors.NewCommandErrorMsg( + handlererrors.ErrNotImplemented, fmt.Sprintf("UpdateDocument: unhandled operation %q", updateOp), ) } @@ -234,7 +234,7 @@ func processSetFieldExpression(command string, doc, setDoc *types.Document, setO } if err := doc.SetByPath(path, setValue); err != nil { - return false, newUpdateError(commonerrors.ErrUnsuitableValueType, err.Error(), command) + return false, newUpdateError(handlererrors.ErrUnsuitableValueType, err.Error(), command) } changed = true @@ -255,7 +255,7 @@ func processRenameFieldExpression(command string, doc *types.Document, update *t if key == "" || renameRawValue == "" { return changed, newUpdateError( - commonerrors.ErrEmptyName, + handlererrors.ErrEmptyName, "An empty update path is not valid.", command, ) @@ -269,7 +269,7 @@ func processRenameFieldExpression(command string, doc *types.Document, update *t var pathErr *types.PathError if errors.As(err, &pathErr) && pathErr.Code() == types.ErrPathElementEmpty { return false, newUpdateError( - commonerrors.ErrEmptyName, + handlererrors.ErrEmptyName, fmt.Sprintf( "The update path '%s' contains an empty field name, which is not allowed.", key, @@ -298,13 +298,13 @@ func processRenameFieldExpression(command string, doc *types.Document, update *t if dpe.Code() == types.ErrPathIndexInvalid { return false, newUpdateError( - commonerrors.ErrUnsuitableValueType, + handlererrors.ErrUnsuitableValueType, fmt.Sprintf("cannot use path '%s' to traverse the document", sourcePath), command, ) } - return changed, newUpdateError(commonerrors.ErrUnsuitableValueType, dpe.Error(), command) + return changed, newUpdateError(handlererrors.ErrUnsuitableValueType, dpe.Error(), command) } // Remove old document @@ -337,7 +337,7 @@ func processIncFieldExpression(command string, doc *types.Document, updateV any) case float64, int32, int64: default: return false, newUpdateError( - commonerrors.ErrTypeMismatch, + handlererrors.ErrTypeMismatch, fmt.Sprintf(`Cannot increment with non-numeric argument: {%s: %#v}`, incKey, incValue), command, ) @@ -352,7 +352,7 @@ func processIncFieldExpression(command string, doc *types.Document, updateV any) // $inc sets the field if it does not exist. if err := doc.SetByPath(path, incValue); err != nil { return false, newUpdateError( - commonerrors.ErrUnsuitableValueType, + handlererrors.ErrUnsuitableValueType, err.Error(), command, ) @@ -394,26 +394,26 @@ func processIncFieldExpression(command string, doc *types.Document, updateV any) } switch { - case errors.Is(err, commonparams.ErrUnexpectedRightOpType): + case errors.Is(err, handlerparams.ErrUnexpectedRightOpType): k := incKey if path.Len() > 1 { k = path.Suffix() } return false, newUpdateError( - commonerrors.ErrTypeMismatch, + handlererrors.ErrTypeMismatch, fmt.Sprintf( `Cannot apply $inc to a value of non-numeric type. `+ `{_id: %s} has the field '%s' of non-numeric type %s`, types.FormatAnyValue(must.NotFail(doc.Get("_id"))), k, - commonparams.AliasFromType(docValue), + handlerparams.AliasFromType(docValue), ), command, ) - case errors.Is(err, commonparams.ErrLongExceededPositive), errors.Is(err, commonparams.ErrLongExceededNegative): + case errors.Is(err, handlerparams.ErrLongExceededPositive), errors.Is(err, handlerparams.ErrLongExceededNegative): return false, newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf( `Failed to apply $inc operations to current value ((NumberLong)%d) for document {_id: "%s"}`, docValue, @@ -421,9 +421,9 @@ func processIncFieldExpression(command string, doc *types.Document, updateV any) ), command, ) - case errors.Is(err, commonparams.ErrIntExceeded): + case errors.Is(err, handlerparams.ErrIntExceeded): return false, newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf( `Failed to apply $inc operations to current value ((NumberInt)%d) for document {_id: "%s"}`, docValue, @@ -466,7 +466,7 @@ func processMaxFieldExpression(command string, doc *types.Document, updateV any) if !doc.HasByPath(path) { err = doc.SetByPath(path, maxVal) if err != nil { - return false, newUpdateError(commonerrors.ErrUnsuitableValueType, err.Error(), command) + return false, newUpdateError(handlererrors.ErrUnsuitableValueType, err.Error(), command) } changed = true @@ -489,8 +489,8 @@ func processMaxFieldExpression(command string, doc *types.Document, updateV any) case types.Less: // if document value is less than max value, update the value default: - return changed, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return changed, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "document comparison is not implemented", "$max", ) @@ -534,7 +534,7 @@ func processMinFieldExpression(command string, doc *types.Document, updateV any) if !doc.HasByPath(path) { err = doc.SetByPath(path, minVal) if err != nil { - return false, newUpdateError(commonerrors.ErrUnsuitableValueType, err.Error(), command) + return false, newUpdateError(handlererrors.ErrUnsuitableValueType, err.Error(), command) } changed = true @@ -599,7 +599,7 @@ func processMulFieldExpression(command string, doc *types.Document, updateV any) mulValue = int64(0) default: return false, newUpdateError( - commonerrors.ErrTypeMismatch, + handlererrors.ErrTypeMismatch, fmt.Sprintf(`Cannot multiply with non-numeric argument: {%s: %#v}`, mulKey, mulValue), command, ) @@ -608,7 +608,7 @@ func processMulFieldExpression(command string, doc *types.Document, updateV any) err := doc.SetByPath(path, mulValue) if err != nil { return false, newUpdateError( - commonerrors.ErrUnsuitableValueType, + handlererrors.ErrUnsuitableValueType, err.Error(), command, ) @@ -630,8 +630,8 @@ func processMulFieldExpression(command string, doc *types.Document, updateV any) switch { case err == nil: if multiplied, ok := multiplied.(float64); ok && math.IsInf(multiplied, 0) { - return false, commonerrors.NewCommandErrorMsg( - commonerrors.ErrBadValue, + return false, handlererrors.NewCommandErrorMsg( + handlererrors.ErrBadValue, fmt.Sprintf("update produces invalid value: { %q: %f } "+ "(update operations that produce infinity values are not allowed)", path, multiplied, ), @@ -655,9 +655,9 @@ func processMulFieldExpression(command string, doc *types.Document, updateV any) continue - case errors.Is(err, commonparams.ErrUnexpectedLeftOpType): + case errors.Is(err, handlerparams.ErrUnexpectedLeftOpType): return false, newUpdateError( - commonerrors.ErrTypeMismatch, + handlererrors.ErrTypeMismatch, fmt.Sprintf( `Cannot multiply with non-numeric argument: {%s: %#v}`, mulKey, @@ -665,26 +665,26 @@ func processMulFieldExpression(command string, doc *types.Document, updateV any) ), command, ) - case errors.Is(err, commonparams.ErrUnexpectedRightOpType): + case errors.Is(err, handlerparams.ErrUnexpectedRightOpType): k := mulKey if path.Len() > 1 { k = path.Suffix() } return false, newUpdateError( - commonerrors.ErrTypeMismatch, + handlererrors.ErrTypeMismatch, fmt.Sprintf( `Cannot apply $mul to a value of non-numeric type. `+ `{_id: %s} has the field '%s' of non-numeric type %s`, types.FormatAnyValue(must.NotFail(doc.Get("_id"))), k, - commonparams.AliasFromType(docValue), + handlerparams.AliasFromType(docValue), ), command, ) - case errors.Is(err, commonparams.ErrLongExceededPositive), errors.Is(err, commonparams.ErrLongExceededNegative): + case errors.Is(err, handlerparams.ErrLongExceededPositive), errors.Is(err, handlerparams.ErrLongExceededNegative): return false, newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf( `Failed to apply $mul operations to current value ((NumberLong)%d) for document {_id: "%s"}`, docValue, @@ -692,9 +692,9 @@ func processMulFieldExpression(command string, doc *types.Document, updateV any) ), command, ) - case errors.Is(err, commonparams.ErrIntExceeded): + case errors.Is(err, handlerparams.ErrIntExceeded): return false, newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf( `Failed to apply $mul operations to current value ((NumberInt)%d) for document {_id: "%s"}`, docValue, @@ -763,11 +763,11 @@ func processBitFieldExpression(command string, doc *types.Document, updateV any) nestedDoc, ok := bitValue.(*types.Document) if !ok { return false, newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf( `The $bit modifier is not compatible with a %s. `+ `You must pass in an embedded document: {$bit: {field: {and/or/xor: #}}`, - commonparams.AliasFromType(bitValue), + handlerparams.AliasFromType(bitValue), ), command, ) @@ -775,7 +775,7 @@ func processBitFieldExpression(command string, doc *types.Document, updateV any) if nestedDoc.Len() == 0 { return false, newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf( "You must pass in at least one bitwise operation. "+ `The format is: {$bit: {field: {and/or/xor: #}}`, @@ -803,7 +803,7 @@ func processBitFieldExpression(command string, doc *types.Document, updateV any) switch { case err == nil: if err = doc.SetByPath(path, bitOpResult); err != nil { - return false, newUpdateError(commonerrors.ErrUnsuitableValueType, err.Error(), command) + return false, newUpdateError(handlererrors.ErrUnsuitableValueType, err.Error(), command) } if docValue == bitOpResult && hasPath { @@ -814,34 +814,34 @@ func processBitFieldExpression(command string, doc *types.Document, updateV any) continue - case errors.Is(err, commonparams.ErrUnexpectedLeftOpType): + case errors.Is(err, handlerparams.ErrUnexpectedLeftOpType): return false, newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf( `The $bit modifier field must be an Integer(32/64 bit); a `+ `'%s' is not supported here: {%s: %s}`, - commonparams.AliasFromType(bitOpValue), + handlerparams.AliasFromType(bitOpValue), bitOp, types.FormatAnyValue(bitOpValue), ), command, ) - case errors.Is(err, commonparams.ErrUnexpectedRightOpType): + case errors.Is(err, handlerparams.ErrUnexpectedRightOpType): return false, newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf( `Cannot apply $bit to a value of non-integral type.`+ `_id: %s has the field %s of non-integer type %s`, types.FormatAnyValue(must.NotFail(doc.Get("_id"))), path.Suffix(), - commonparams.AliasFromType(docValue), + handlerparams.AliasFromType(docValue), ), command, ) default: - return false, newUpdateError(commonerrors.ErrBadValue, err.Error(), command) + return false, newUpdateError(handlererrors.ErrBadValue, err.Error(), command) } } } @@ -983,7 +983,7 @@ func HasSupportedUpdateModifiers(command string, update *types.Document) (bool, default: if strings.HasPrefix(updateOp, "$") { return false, newUpdateError( - commonerrors.ErrFailedToParse, + handlererrors.ErrFailedToParse, fmt.Sprintf( "Unknown modifier: %s. Expected a valid update modifier or pipeline-style "+ "update specified as an array", updateOp, @@ -1000,13 +1000,13 @@ func HasSupportedUpdateModifiers(command string, update *types.Document) (bool, } // newUpdateError returns CommandError for findAndModify command, WriteError for other commands. -func newUpdateError(code commonerrors.ErrorCode, msg, command string) error { +func newUpdateError(code handlererrors.ErrorCode, msg, command string) error { // Depending on the driver, the command may be camel case or lower case. if strings.ToLower(command) == "findandmodify" { - return commonerrors.NewCommandErrorMsgWithArgument(code, msg, command) + return handlererrors.NewCommandErrorMsgWithArgument(code, msg, command) } - return commonerrors.NewWriteErrorMsg(code, msg) + return handlererrors.NewWriteErrorMsg(code, msg) } // validateOperatorKeys returns error if any key contains empty path or @@ -1019,7 +1019,7 @@ func validateOperatorKeys(command string, docs ...*types.Document) error { nextPath, err := types.NewPathFromString(key) if err != nil { return newUpdateError( - commonerrors.ErrEmptyName, + handlererrors.ErrEmptyName, fmt.Sprintf( "The update path '%s' contains an empty field name, which is not allowed.", key, @@ -1035,7 +1035,7 @@ func validateOperatorKeys(command string, docs ...*types.Document) error { if pathErr.Code() == types.ErrPathConflictOverwrite || pathErr.Code() == types.ErrPathConflictCollision { return newUpdateError( - commonerrors.ErrConflictingUpdateOperators, + handlererrors.ErrConflictingUpdateOperators, fmt.Sprintf( "Updating the path '%[1]s' would create a conflict at '%[1]s'", key, ), @@ -1079,10 +1079,10 @@ func extractValueFromUpdateOperator(command, op string, update *types.Document) doc, ok := updateExpression.(*types.Document) if !ok { return nil, newUpdateError( - commonerrors.ErrFailedToParse, + handlererrors.ErrFailedToParse, fmt.Sprintf(`Modifiers operate on fields but we found type %[1]s instead. `+ `For example: {$mod: {: ...}} not {%s: %s}`, - commonparams.AliasFromType(updateExpression), + handlerparams.AliasFromType(updateExpression), op, types.FormatAnyValue(updateExpression), ), @@ -1093,7 +1093,7 @@ func extractValueFromUpdateOperator(command, op string, update *types.Document) duplicate, ok := doc.FindDuplicateKey() if ok { return nil, newUpdateError( - commonerrors.ErrConflictingUpdateOperators, + handlererrors.ErrConflictingUpdateOperators, fmt.Sprintf( "Updating the path '%[1]s' would create a conflict at '%[1]s'", duplicate, ), @@ -1135,7 +1135,7 @@ func validateRenameExpression(command string, update *types.Document) error { vStr, ok := v.(string) if !ok { return newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf("The 'to' field for $rename must be a string: %s: %v", k, v), command, ) @@ -1144,7 +1144,7 @@ func validateRenameExpression(command string, update *types.Document) error { // disallow fields where key is equal to the target if k == vStr { return newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf(`The source and target field for $rename must differ: %s: "%[1]s"`, k, vStr), command, ) @@ -1152,7 +1152,7 @@ func validateRenameExpression(command string, update *types.Document) error { if _, ok = keys[k]; ok { return newUpdateError( - commonerrors.ErrConflictingUpdateOperators, + handlererrors.ErrConflictingUpdateOperators, fmt.Sprintf("Updating the path '%s' would create a conflict at '%s'", k, k), command, ) @@ -1162,7 +1162,7 @@ func validateRenameExpression(command string, update *types.Document) error { if _, ok = keys[vStr]; ok { return newUpdateError( - commonerrors.ErrConflictingUpdateOperators, + handlererrors.ErrConflictingUpdateOperators, fmt.Sprintf("Updating the path '%s' would create a conflict at '%s'", vStr, vStr), command, ) @@ -1192,7 +1192,7 @@ func validateCurrentDateExpression(command string, update *types.Document) error for _, k := range setValue.Keys() { if k != "$type" { return newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf("Unrecognized $currentDate option: %s", k), command, ) @@ -1206,7 +1206,7 @@ func validateCurrentDateExpression(command string, update *types.Document) error currentDateTypeString, ok := currentDateType.(string) if !ok || !slices.Contains([]string{"date", "timestamp"}, currentDateTypeString) { return newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, "The '$type' string field is required to be 'date' or 'timestamp': "+ "{$currentDate: {field : {$type: 'date'}}}", command, @@ -1218,9 +1218,9 @@ func validateCurrentDateExpression(command string, update *types.Document) error default: return newUpdateError( - commonerrors.ErrBadValue, + handlererrors.ErrBadValue, fmt.Sprintf("%s is not valid type for $currentDate. Please use a boolean ('true') "+ - "or a $type expression ({$type: 'timestamp/date'}).", commonparams.AliasFromType(setValue), + "or a $type expression ({$type: 'timestamp/date'}).", handlerparams.AliasFromType(setValue), ), command, ) diff --git a/internal/handler/common/update_array_operators.go b/internal/handler/common/update_array_operators.go index 88eaf60a0fa5..70298a3c90a4 100644 --- a/internal/handler/common/update_array_operators.go +++ b/internal/handler/common/update_array_operators.go @@ -18,8 +18,8 @@ import ( "errors" "fmt" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -44,17 +44,17 @@ func processPopArrayUpdateExpression(doc *types.Document, update *types.Document return false, lazyerrors.Error(err) } - popValue, err := commonparams.GetWholeNumberParam(popValueRaw) + popValue, err := handlerparams.GetWholeNumberParam(popValueRaw) if err != nil { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrFailedToParse, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrFailedToParse, fmt.Sprintf(`Expected a number in: %s: "%v"`, key, popValueRaw), ) } if popValue != 1 && popValue != -1 { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrFailedToParse, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrFailedToParse, fmt.Sprintf("$pop expects 1 or -1, found: %d", popValue), ) } @@ -77,9 +77,9 @@ func processPopArrayUpdateExpression(doc *types.Document, update *types.Document array, ok := val.(*types.Array) if !ok { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrTypeMismatch, - fmt.Sprintf("Path '%s' contains an element of non-array type '%s'", key, commonparams.AliasFromType(val)), + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrTypeMismatch, + fmt.Sprintf("Path '%s' contains an element of non-array type '%s'", key, handlerparams.AliasFromType(val)), ) } @@ -126,8 +126,8 @@ func checkUnsuitableValueError(doc *types.Document, key string, path types.Path) } // ErrUnsuitableValueType is returned if the document contains prefix. - return commonerrors.NewWriteErrorMsg( - commonerrors.ErrUnsuitableValueType, + return handlererrors.NewWriteErrorMsg( + handlererrors.ErrUnsuitableValueType, fmt.Sprintf( "Cannot use the part (%s) of (%s) to traverse the element ({%s: %v})", path.Slice()[1], @@ -168,11 +168,11 @@ func processPushArrayUpdateExpression(doc *types.Document, update *types.Documen each, ok = eachRaw.(*types.Array) if !ok { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrBadValue, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrBadValue, fmt.Sprintf( "The argument to $each in $push must be an array but it was of type: %s", - commonparams.AliasFromType(eachRaw), + handlerparams.AliasFromType(eachRaw), ), ) } @@ -189,8 +189,8 @@ func processPushArrayUpdateExpression(doc *types.Document, update *types.Documen changed = true if err = doc.SetByPath(path, types.MakeArray(1)); err != nil { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrUnsuitableValueType, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrUnsuitableValueType, err.Error(), ) } @@ -203,11 +203,11 @@ func processPushArrayUpdateExpression(doc *types.Document, update *types.Documen array, ok := val.(*types.Array) if !ok { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrBadValue, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrBadValue, fmt.Sprintf( "The field '%s' must be an array but is of type '%s' in document {_id: %s}", - key, commonparams.AliasFromType(val), types.FormatAnyValue(must.NotFail(doc.Get("_id"))), + key, handlerparams.AliasFromType(val), types.FormatAnyValue(must.NotFail(doc.Get("_id"))), ), ) } @@ -257,11 +257,11 @@ func processAddToSetArrayUpdateExpression(doc, update *types.Document) (bool, er each, ok = eachRaw.(*types.Array) if !ok { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrTypeMismatch, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "The argument to $each in $addToSet must be an array but it was of type %s", - commonparams.AliasFromType(eachRaw), + handlerparams.AliasFromType(eachRaw), ), ) } @@ -278,8 +278,8 @@ func processAddToSetArrayUpdateExpression(doc, update *types.Document) (bool, er changed = true if err = doc.SetByPath(path, types.MakeArray(1)); err != nil { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrUnsuitableValueType, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrUnsuitableValueType, err.Error(), ) } @@ -292,11 +292,11 @@ func processAddToSetArrayUpdateExpression(doc, update *types.Document) (bool, er array, ok := val.(*types.Array) if !ok { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrBadValue, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrBadValue, fmt.Sprintf( "The field '%s' must be an array but is of type '%s' in document {_id: %s}", - key, commonparams.AliasFromType(val), types.FormatAnyValue(must.NotFail(doc.Get("_id"))), + key, handlerparams.AliasFromType(val), types.FormatAnyValue(must.NotFail(doc.Get("_id"))), ), ) } @@ -352,8 +352,8 @@ func processPullAllArrayUpdateExpression(doc, update *types.Document) (bool, err // If the path does not exist, create a new array and set it. if !doc.HasByPath(path) { if err = doc.SetByPath(path, types.MakeArray(1)); err != nil { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrUnsuitableValueType, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrUnsuitableValueType, err.Error(), ) } @@ -366,22 +366,22 @@ func processPullAllArrayUpdateExpression(doc, update *types.Document) (bool, err array, ok := val.(*types.Array) if !ok { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrBadValue, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrBadValue, fmt.Sprintf( "The field '%s' must be an array but is of type '%s' in document {_id: %s}", - key, commonparams.AliasFromType(val), types.FormatAnyValue(must.NotFail(doc.Get("_id"))), + key, handlerparams.AliasFromType(val), types.FormatAnyValue(must.NotFail(doc.Get("_id"))), ), ) } pullAllArray, ok := pullAllValueRaw.(*types.Array) if !ok { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrBadValue, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrBadValue, fmt.Sprintf( "The field '%s' must be an array but is of type '%s'", - key, commonparams.AliasFromType(pullAllValueRaw), + key, handlerparams.AliasFromType(pullAllValueRaw), ), ) } @@ -447,8 +447,8 @@ func processPullArrayUpdateExpression(doc *types.Document, update *types.Documen // This will deal with cases like $pull on a non-existing field or unset field. if !doc.HasByPath(path) { if err = doc.SetByPath(path, types.MakeArray(1)); err != nil { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrUnsuitableValueType, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrUnsuitableValueType, err.Error(), ) } @@ -461,8 +461,8 @@ func processPullArrayUpdateExpression(doc *types.Document, update *types.Documen array, ok := val.(*types.Array) if !ok { - return false, commonerrors.NewWriteErrorMsg( - commonerrors.ErrBadValue, + return false, handlererrors.NewWriteErrorMsg( + handlererrors.ErrBadValue, "Cannot apply $pull to a non-array value", ) } diff --git a/internal/handler/common/update_params.go b/internal/handler/common/update_params.go index 8d6342b95397..412a41942450 100644 --- a/internal/handler/common/update_params.go +++ b/internal/handler/common/update_params.go @@ -17,7 +17,7 @@ package common import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" ) @@ -60,7 +60,7 @@ type Update struct { func GetUpdateParams(document *types.Document, l *zap.Logger) (*UpdateParams, error) { var params UpdateParams - err := commonparams.ExtractParams(document, "update", ¶ms, l) + err := handlerparams.ExtractParams(document, "update", ¶ms, l) if err != nil { return nil, err } diff --git a/internal/handler/commoncommands/msg_debugerror.go b/internal/handler/commoncommands/msg_debugerror.go index 172fd7bcb27c..de293975aed9 100644 --- a/internal/handler/commoncommands/msg_debugerror.go +++ b/internal/handler/commoncommands/msg_debugerror.go @@ -21,7 +21,7 @@ import ( "strings" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -42,7 +42,7 @@ func MsgDebugError(_ context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { // check if parameter is an error code if n, err := strconv.ParseInt(expected, 10, 32); err == nil { - errCode := commonerrors.ErrorCode(n) + errCode := handlererrors.ErrorCode(n) return nil, errors.New(errCode.String()) } diff --git a/internal/handler/commonerrors/command.go b/internal/handler/handlererrors/command.go similarity index 99% rename from internal/handler/commonerrors/command.go rename to internal/handler/handlererrors/command.go index 671a781799aa..01a9804ff896 100644 --- a/internal/handler/commonerrors/command.go +++ b/internal/handler/handlererrors/command.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package commonerrors +package handlererrors import ( "errors" diff --git a/internal/handler/commonerrors/error.go b/internal/handler/handlererrors/error.go similarity index 99% rename from internal/handler/commonerrors/error.go rename to internal/handler/handlererrors/error.go index f38e0a4d5829..caaec4cda773 100644 --- a/internal/handler/commonerrors/error.go +++ b/internal/handler/handlererrors/error.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package commonerrors provides handlers errors. -package commonerrors +// Package handlererrors provides handlers errors. +package handlererrors import ( "errors" diff --git a/internal/handler/commonerrors/errorcode_string.go b/internal/handler/handlererrors/errorcode_string.go similarity index 99% rename from internal/handler/commonerrors/errorcode_string.go rename to internal/handler/handlererrors/errorcode_string.go index 70300842ae91..e2f8ea663bf3 100644 --- a/internal/handler/commonerrors/errorcode_string.go +++ b/internal/handler/handlererrors/errorcode_string.go @@ -1,6 +1,6 @@ // Code generated by "stringer -linecomment -type ErrorCode"; DO NOT EDIT. -package commonerrors +package handlererrors import "strconv" diff --git a/internal/handler/commonerrors/commonerrors_test.go b/internal/handler/handlererrors/handlererrors_test.go similarity index 97% rename from internal/handler/commonerrors/commonerrors_test.go rename to internal/handler/handlererrors/handlererrors_test.go index d007b2815cf5..65d163f80686 100644 --- a/internal/handler/commonerrors/commonerrors_test.go +++ b/internal/handler/handlererrors/handlererrors_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package commonerrors +package handlererrors import ( "io" diff --git a/internal/handler/commonerrors/write.go b/internal/handler/handlererrors/write.go similarity index 99% rename from internal/handler/commonerrors/write.go rename to internal/handler/handlererrors/write.go index 8057b619ff43..90b6cc0105ae 100644 --- a/internal/handler/commonerrors/write.go +++ b/internal/handler/handlererrors/write.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package commonerrors +package handlererrors import ( "errors" diff --git a/internal/handler/commonparams/extract_params.go b/internal/handler/handlerparams/extract_params.go similarity index 91% rename from internal/handler/commonparams/extract_params.go rename to internal/handler/handlerparams/extract_params.go index cc2e607dfd32..cac6aa9456b3 100644 --- a/internal/handler/commonparams/extract_params.go +++ b/internal/handler/handlerparams/extract_params.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package commonparams +package handlerparams import ( "errors" @@ -23,7 +23,7 @@ import ( "go.uber.org/zap" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -97,8 +97,8 @@ func ExtractParams(doc *types.Document, command string, value any, l *zap.Logger } if fieldIndex == nil { - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, fmt.Sprintf("%s: unknown field %q", command, key), command, ) @@ -119,7 +119,7 @@ func ExtractParams(doc *types.Document, command string, value any, l *zap.Logger doc.Command(), key, val, ) - return commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrNotImplemented, msg, key) + return handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrNotImplemented, msg, key) } if options.nonDefault { @@ -131,7 +131,7 @@ func ExtractParams(doc *types.Document, command string, value any, l *zap.Logger doc.Command(), key, val, ) - return commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrNotImplemented, msg, key) + return handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrNotImplemented, msg, key) } } @@ -283,8 +283,8 @@ func setStructField(elem *reflect.Value, o *tagOptions, i int, command, key stri numeric, err = GetWholeNumberParam(val) if err != nil || numeric < 0 || numeric > 1 { - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, fmt.Sprintf("The '%s.%s' field must be 0 or 1. Got %v", command, key, types.FormatAnyValue(val)), command, ) @@ -299,8 +299,8 @@ func setStructField(elem *reflect.Value, o *tagOptions, i int, command, key stri case reflect.Slice: array, ok := val.(*types.Array) if !ok { - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( `BSON field '%s.%s' is the wrong type '%s', expected type '%s'`, command, key, AliasFromType(val), AliasFromType(fv.Interface()), @@ -326,8 +326,8 @@ func setStructField(elem *reflect.Value, o *tagOptions, i int, command, key stri doc, ok := arrayDoc.(*types.Document) if !ok { - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( `BSON field '%s.%s' is the wrong type '%s', expected type '%s'`, command, key, AliasFromType(val), AliasFromType(fv.Interface()), @@ -360,8 +360,8 @@ func setStructField(elem *reflect.Value, o *tagOptions, i int, command, key stri if v.Type() != fv.Type() { if key == command { - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidNamespace, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidNamespace, fmt.Sprintf("collection name has invalid type %s", AliasFromType(settable)), command, ) @@ -372,8 +372,8 @@ func setStructField(elem *reflect.Value, o *tagOptions, i int, command, key stri return nil } - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( `BSON field '%s.%s' is the wrong type '%s', expected type '%s'`, command, key, AliasFromType(val), AliasFromType(fv.Interface()), @@ -408,7 +408,7 @@ func checkAllRequiredFieldsPopulated(v *reflect.Value, command string, keys []st if to.collection { if v.Field(i).IsZero() { - return commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, + return handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, fmt.Sprintf("Invalid namespace specified '%s.'", v.FieldByName("DB")), command, ) @@ -424,8 +424,8 @@ func checkAllRequiredFieldsPopulated(v *reflect.Value, command string, keys []st // Depending on the driver, the key may be camel case or lower case for a collection name. if !slices.Contains(keys, key) && !slices.Contains(keys, strings.ToLower(key)) { - return commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrMissingField, + return handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrMissingField, fmt.Sprintf("BSON field '%s.%s' is missing but a required field", command, key), command, ) diff --git a/internal/handler/commonparams/extract_params_test.go b/internal/handler/handlerparams/extract_params_test.go similarity index 99% rename from internal/handler/commonparams/extract_params_test.go rename to internal/handler/handlerparams/extract_params_test.go index 0d1bbe1099f0..12c7f6660ee9 100644 --- a/internal/handler/commonparams/extract_params_test.go +++ b/internal/handler/handlerparams/extract_params_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package commonparams +package handlerparams import ( "regexp" diff --git a/internal/handler/commonparams/commonparams.go b/internal/handler/handlerparams/handlerparams.go similarity index 83% rename from internal/handler/commonparams/commonparams.go rename to internal/handler/handlerparams/handlerparams.go index 6d2b98d2e67f..1e955db9d229 100644 --- a/internal/handler/commonparams/commonparams.go +++ b/internal/handler/handlerparams/handlerparams.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package commonparams provides functions for parsing handlers parameters. -package commonparams +// Package handlerparams provides functions for parsing handlers parameters. +package handlerparams import ( "errors" @@ -21,7 +21,7 @@ import ( "math" "strings" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" ) @@ -78,7 +78,7 @@ func GetWholeNumberParam(value any) (int64, error) { // The function checks the type, ensures it can be represented as a whole number, // isn't negative and falls within a given minimum value and the limit of a 32-bit integer. // -// It returns the processed integer value, or a commonerrors.CommandError error if the value fails validation. +// It returns the processed integer value, or a handlererrors.CommandError error if the value fails validation. // Error codes list: // - ErrTypeMismatch - if the value is not a number; // - ErrValueNegative - if the value is negative of lower than the minimum value. @@ -91,8 +91,8 @@ func GetValidatedNumberParamWithMinValue(command string, param string, value any return int64(minValue), nil } - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( `BSON field '%s.%s' is the wrong type '%s', expected types '[long, int, decimal, double]'`, command, param, AliasFromType(value), @@ -101,8 +101,8 @@ func GetValidatedNumberParamWithMinValue(command string, param string, value any ) case errors.Is(err, ErrNotWholeNumber): if math.Signbit(value.(float64)) { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrValueNegative, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrValueNegative, fmt.Sprintf( "BSON field '%s' value must be >= %d, actual value '%d'", param, minValue, int(math.Ceil(value.(float64))), @@ -118,8 +118,8 @@ func GetValidatedNumberParamWithMinValue(command string, param string, value any return math.MaxInt32, nil case errors.Is(err, ErrLongExceededNegative): - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrValueNegative, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrValueNegative, fmt.Sprintf( "BSON field '%s' value must be >= %d, actual value '%d'", param, minValue, int(math.Ceil(value.(float64))), @@ -133,8 +133,8 @@ func GetValidatedNumberParamWithMinValue(command string, param string, value any } if whole < int64(minValue) { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrValueNegative, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrValueNegative, fmt.Sprintf("BSON field '%s' value must be >= %d, actual value '%d'", param, minValue, whole), command, ) @@ -153,22 +153,22 @@ func getOptionalPositiveNumber(key string, value any) (int64, error) { if err != nil { switch { case errors.Is(err, ErrUnexpectedType): - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%s must be a number", key), key, ) case errors.Is(err, ErrNotWholeNumber): if _, ok := value.(float64); ok { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%v has non-integral value", key), key, ) } - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%s must be a whole number", key), key, ) @@ -178,16 +178,16 @@ func getOptionalPositiveNumber(key string, value any) (int64, error) { } if whole > math.MaxInt32 || whole < math.MinInt32 { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%v value for %s is out of range", whole, key), key, ) } if whole < 0 { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%v value for %s is out of range", value, key), key, ) @@ -219,7 +219,7 @@ func GetBoolOptionalParam(key string, v any) (bool, error) { AliasFromType(v), ) - return false, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrTypeMismatch, msg, key) + return false, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrTypeMismatch, msg, key) } } @@ -228,8 +228,8 @@ func SplitNamespace(ns, argument string) (string, string, error) { parts := strings.Split(ns, ".") if len(parts) != 2 || parts[0] == "" || parts[1] == "" { - return "", "", commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidNamespace, + return "", "", handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidNamespace, fmt.Sprintf("Invalid namespace specified '%s'", ns), argument, ) diff --git a/internal/handler/commonparams/typecode.go b/internal/handler/handlerparams/typecode.go similarity index 94% rename from internal/handler/commonparams/typecode.go rename to internal/handler/handlerparams/typecode.go index 866b42497a1a..6c5faf557199 100644 --- a/internal/handler/commonparams/typecode.go +++ b/internal/handler/handlerparams/typecode.go @@ -12,14 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -package commonparams +package handlerparams import ( "fmt" "math" "time" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/must" ) @@ -84,14 +84,14 @@ func NewTypeCode(code int32) (TypeCode, error) { TypeCodeNull, TypeCodeRegex, TypeCodeInt, TypeCodeTimestamp, TypeCodeLong, TypeCodeNumber: return c, nil case TypeCodeDecimal, TypeCodeMinKey, TypeCodeMaxKey: - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, fmt.Sprintf(`Type code %v not implemented`, code), "$type", ) default: - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Invalid numerical type code: %d`, code), "$type", ) @@ -192,8 +192,8 @@ func isWholeNumber(v any) bool { func ParseTypeCode(alias string) (TypeCode, error) { code, ok := aliasToTypeCode[alias] if !ok { - return 0, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return 0, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf(`Unknown type name alias: %s`, alias), "$type", ) diff --git a/internal/handler/commonparams/typecode_string.go b/internal/handler/handlerparams/typecode_string.go similarity index 98% rename from internal/handler/commonparams/typecode_string.go rename to internal/handler/handlerparams/typecode_string.go index 456a8eec008f..84e8aab7b9d4 100644 --- a/internal/handler/commonparams/typecode_string.go +++ b/internal/handler/handlerparams/typecode_string.go @@ -1,6 +1,6 @@ // Code generated by "stringer -linecomment -type TypeCode"; DO NOT EDIT. -package commonparams +package handlerparams import "strconv" diff --git a/internal/handler/commonparams/typecode_test.go b/internal/handler/handlerparams/typecode_test.go similarity index 98% rename from internal/handler/commonparams/typecode_test.go rename to internal/handler/handlerparams/typecode_test.go index e5d6119bf51a..b57a60487cca 100644 --- a/internal/handler/commonparams/typecode_test.go +++ b/internal/handler/handlerparams/typecode_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package commonparams +package handlerparams import ( "testing" diff --git a/internal/handler/msg_aggregate.go b/internal/handler/msg_aggregate.go index fc70d0672b54..af3a4066e2ae 100644 --- a/internal/handler/msg_aggregate.go +++ b/internal/handler/msg_aggregate.go @@ -30,8 +30,8 @@ import ( "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations/stages" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -74,8 +74,8 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs var cName string if cName, ok = collectionParam.(string); !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, "Invalid command format: the 'aggregate' field must specify a collection name or 1", document.Command(), ) @@ -85,7 +85,7 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", dbName, cName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) @@ -95,7 +95,7 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", cName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) @@ -108,42 +108,42 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs v = int64(0) } - // cannot use other existing commonparams function, they return different error codes - maxTimeMS, err := commonparams.GetWholeNumberParam(v) + // cannot use other existing handlerparams function, they return different error codes + maxTimeMS, err := handlerparams.GetWholeNumberParam(v) if err != nil { switch { - case errors.Is(err, commonparams.ErrUnexpectedType): + case errors.Is(err, handlerparams.ErrUnexpectedType): if _, ok = v.(types.NullType); ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "maxTimeMS must be a number", document.Command(), ) } - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( `BSON field 'aggregate.maxTimeMS' is the wrong type '%s', expected types '[long, int, decimal, double]'`, - commonparams.AliasFromType(v), + handlerparams.AliasFromType(v), ), document.Command(), ) - case errors.Is(err, commonparams.ErrNotWholeNumber): - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + case errors.Is(err, handlerparams.ErrNotWholeNumber): + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "maxTimeMS has non-integral value", document.Command(), ) - case errors.Is(err, commonparams.ErrLongExceededPositive): - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + case errors.Is(err, handlerparams.ErrLongExceededPositive): + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%s value for maxTimeMS is out of range", types.FormatAnyValue(v)), document.Command(), ) - case errors.Is(err, commonparams.ErrLongExceededNegative): - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrValueNegative, + case errors.Is(err, handlerparams.ErrLongExceededNegative): + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrValueNegative, fmt.Sprintf("BSON field 'maxTimeMS' value must be >= 0, actual value '%s'", types.FormatAnyValue(v)), document.Command(), ) @@ -153,16 +153,16 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs } if maxTimeMS < int64(0) { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrValueNegative, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrValueNegative, fmt.Sprintf("BSON field 'maxTimeMS' value must be >= 0, actual value '%s'", types.FormatAnyValue(v)), document.Command(), ) } if maxTimeMS > math.MaxInt32 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%v value for maxTimeMS is out of range", v), document.Command(), ) @@ -170,8 +170,8 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs pipeline, err := common.GetRequiredParam[*types.Array](document, "pipeline") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, "'pipeline' option must be specified as an array", document.Command(), ) @@ -185,8 +185,8 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs var d *types.Document if d, ok = v.(*types.Document); !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, "Each element of the 'pipeline' array must be an object", document.Command(), ) @@ -201,8 +201,8 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs switch d.Command() { case "$collStats": if i > 0 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrCollStatsIsNotFirstStage, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrCollStatsIsNotFirstStage, "$collStats is only valid as the first stage in a pipeline", document.Command(), ) @@ -218,8 +218,8 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs // validate cursor after validating pipeline stages to keep compatibility v, _ = document.Get("cursor") if v == nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, "The 'cursor' option is required, except for aggregate with the explain argument", document.Command(), ) @@ -227,11 +227,11 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs cursorDoc, ok := v.(*types.Document) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( `BSON field 'cursor' is the wrong type '%s', expected type 'object'`, - commonparams.AliasFromType(v), + handlerparams.AliasFromType(v), ), document.Command(), ) @@ -242,7 +242,7 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs v = int32(101) } - batchSize, err := commonparams.GetValidatedNumberParamWithMinValue(document.Command(), "batchSize", v, 0) + batchSize, err := handlerparams.GetValidatedNumberParamWithMinValue(document.Command(), "batchSize", v, 0) if err != nil { return nil, err } @@ -395,8 +395,8 @@ func processStagesStats(ctx context.Context, closer *iterator.MultiCloser, p *st if hasCount || hasStorage { collStats, err = p.c.Stats(ctx, new(backends.CollectionStatsParams)) if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionDoesNotExist) { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNamespaceNotFound, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNamespaceNotFound, fmt.Sprintf("ns not found: %s.%s", p.dbName, p.cName), "aggregate", ) diff --git a/internal/handler/msg_collmod.go b/internal/handler/msg_collmod.go index 9e992d0ae4ca..b6e426974f4a 100644 --- a/internal/handler/msg_collmod.go +++ b/internal/handler/msg_collmod.go @@ -17,14 +17,14 @@ package handler import ( "context" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/wire" ) // MsgCollMod implements `collMod` command. func (h *Handler) MsgCollMod(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrNotImplemented, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrNotImplemented, "`collMod` command is not implemented yet", ) } diff --git a/internal/handler/msg_collstats.go b/internal/handler/msg_collstats.go index 263ecafb0ba6..708023cebacc 100644 --- a/internal/handler/msg_collstats.go +++ b/internal/handler/msg_collstats.go @@ -22,8 +22,8 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -53,7 +53,7 @@ func (h *Handler) MsgCollStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs var s any if s, err = document.Get("scale"); err == nil { - if scale, err = commonparams.GetValidatedNumberParamWithMinValue(command, "scale", s, 1); err != nil { + if scale, err = handlerparams.GetValidatedNumberParamWithMinValue(command, "scale", s, 1); err != nil { return nil, err } } @@ -62,7 +62,7 @@ func (h *Handler) MsgCollStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid database specified '%s'", dbName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) @@ -72,7 +72,7 @@ func (h *Handler) MsgCollStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_compact.go b/internal/handler/msg_compact.go index 0a9975a9ea14..601d526a1eb5 100644 --- a/internal/handler/msg_compact.go +++ b/internal/handler/msg_compact.go @@ -20,8 +20,8 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -52,8 +52,8 @@ func (h *Handler) MsgCompact(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, db, err := h.b.Database(dbName) if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidNamespace, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidNamespace, fmt.Sprintf("Invalid namespace specified '%s.%s'", dbName, collection), command, ) @@ -65,8 +65,8 @@ func (h *Handler) MsgCompact(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, c, err := db.Collection(collection) if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidNamespace, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidNamespace, fmt.Sprintf("Invalid namespace specified '%s.%s'", dbName, collection), command, ) @@ -78,15 +78,15 @@ func (h *Handler) MsgCompact(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, var force bool if v, _ := document.Get("force"); v != nil { - if force, err = commonparams.GetBoolOptionalParam("force", v); err != nil { + if force, err = handlerparams.GetBoolOptionalParam("force", v); err != nil { return nil, err } } statsBefore, err := c.Stats(ctx, new(backends.CollectionStatsParams)) if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionDoesNotExist) { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNamespaceNotFound, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNamespaceNotFound, fmt.Sprintf("Invalid namespace specified '%s.%s'", dbName, collection), command, ) diff --git a/internal/handler/msg_count.go b/internal/handler/msg_count.go index 994302ea8891..418fce9277da 100644 --- a/internal/handler/msg_count.go +++ b/internal/handler/msg_count.go @@ -21,7 +21,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -45,7 +45,7 @@ func (h *Handler) MsgCount(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, e if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", params.DB, params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "count") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "count") } return nil, lazyerrors.Error(err) @@ -55,7 +55,7 @@ func (h *Handler) MsgCount(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, e if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "count") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "count") } return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_create.go b/internal/handler/msg_create.go index 0452927d0852..4392e84c5187 100644 --- a/internal/handler/msg_create.go +++ b/internal/handler/msg_create.go @@ -20,8 +20,8 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -76,7 +76,7 @@ func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, var capped bool if v, _ := document.Get("capped"); v != nil { - capped, err = commonparams.GetBoolOptionalParam("capped", v) + capped, err = handlerparams.GetBoolOptionalParam("capped", v) if err != nil { return nil, err } @@ -90,10 +90,10 @@ func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, size, _ := document.Get("size") if _, ok := size.(types.NullType); size == nil || ok { msg := "the 'size' field is required when 'capped' is true" - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidOptions, msg, "create") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidOptions, msg, "create") } - params.CappedSize, err = commonparams.GetValidatedNumberParamWithMinValue(document.Command(), "size", size, 1) + params.CappedSize, err = handlerparams.GetValidatedNumberParamWithMinValue(document.Command(), "size", size, 1) if err != nil { return nil, err } @@ -103,7 +103,7 @@ func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, } if max, _ := document.Get("max"); max != nil { - params.CappedDocuments, err = commonparams.GetValidatedNumberParamWithMinValue(document.Command(), "max", max, 0) + params.CappedDocuments, err = handlerparams.GetValidatedNumberParamWithMinValue(document.Command(), "max", max, 0) if err != nil { return nil, err } @@ -114,7 +114,7 @@ func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", dbName, collectionName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "create") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "create") } return nil, lazyerrors.Error(err) @@ -135,11 +135,11 @@ func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, case backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid): msg := fmt.Sprintf("Invalid collection name: %s", collectionName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "create") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "create") case backends.ErrorCodeIs(err, backends.ErrorCodeCollectionAlreadyExists): msg := fmt.Sprintf("Collection %s.%s already exists.", dbName, collectionName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrNamespaceExists, msg, "create") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrNamespaceExists, msg, "create") default: return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_createindexes.go b/internal/handler/msg_createindexes.go index 2952d00916ae..8caa063ae0b0 100644 --- a/internal/handler/msg_createindexes.go +++ b/internal/handler/msg_createindexes.go @@ -23,8 +23,8 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -54,8 +54,8 @@ func (h *Handler) MsgCreateIndexes(ctx context.Context, msg *wire.OpMsg) (*wire. db, err := h.b.Database(dbName) if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidNamespace, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidNamespace, fmt.Sprintf("Invalid namespace specified '%s.%s'", dbName, collection), command, ) @@ -67,8 +67,8 @@ func (h *Handler) MsgCreateIndexes(ctx context.Context, msg *wire.OpMsg) (*wire. c, err := db.Collection(collection) if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidNamespace, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidNamespace, fmt.Sprintf("Invalid namespace specified '%s.%s'", dbName, collection), command, ) @@ -79,8 +79,8 @@ func (h *Handler) MsgCreateIndexes(ctx context.Context, msg *wire.OpMsg) (*wire. v, _ := document.Get("indexes") if v == nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrMissingField, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrMissingField, "BSON field 'createIndexes.indexes' is missing but a required field", document.Command(), ) @@ -89,26 +89,26 @@ func (h *Handler) MsgCreateIndexes(ctx context.Context, msg *wire.OpMsg) (*wire. idxArr, ok := v.(*types.Array) if !ok { if _, ok = v.(types.NullType); ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrIndexesWrongType, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrIndexesWrongType, "invalid parameter: expected an object (indexes)", document.Command(), ) } - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "BSON field 'createIndexes.indexes' is the wrong type '%s', expected type 'array'", - commonparams.AliasFromType(v), + handlerparams.AliasFromType(v), ), document.Command(), ) } if idxArr.Len() == 0 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "Must specify at least one index to create", document.Command(), ) @@ -195,12 +195,12 @@ func processIndexesArray(command string, indexesArray *types.Array) ([]backends. indexDoc, ok := val.(*types.Document) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "BSON field 'createIndexes.indexes.%d' is the wrong type '%s', expected type 'object'", key, - commonparams.AliasFromType(val), + handlerparams.AliasFromType(val), ), command, ) @@ -232,8 +232,8 @@ func processIndex(command string, indexDoc *types.Document) (*backends.IndexInfo // do nothing case errors.Is(err, iterator.ErrIteratorDone): if !hasValue { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, fmt.Sprintf( "Error in specification {} :: caused by :: "+ "The 'key' field is a required property of an index specification", @@ -254,16 +254,16 @@ func processIndex(command string, indexDoc *types.Document) (*backends.IndexInfo keyDoc, err = common.GetRequiredParam[*types.Document](indexDoc, "key") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, "'key' option must be specified as an object", "createIndexes", ) } if keyDoc.Len() == 0 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrCannotCreateIndex, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrCannotCreateIndex, "Must specify at least one field for the index key", "createIndexes", ) @@ -275,9 +275,9 @@ func processIndex(command string, indexDoc *types.Document) (*backends.IndexInfo var order int64 if val, err = keyDoc.Get("_id"); err == nil { - if order, err = commonparams.GetWholeNumberParam(val); err == nil && order == -1 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + if order, err = handlerparams.GetWholeNumberParam(val); err == nil && order == -1 { + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "The field 'key' for an _id index must be {_id: 1}, but got { _id: -1 }", "createIndexes", ) @@ -292,8 +292,8 @@ func processIndex(command string, indexDoc *types.Document) (*backends.IndexInfo v, _ := indexDoc.Get("name") if v == nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFailedToParse, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFailedToParse, fmt.Sprintf( "Error in specification { key: %s } :: caused by :: "+ "The 'name' field is a required property of an index specification", @@ -307,8 +307,8 @@ func processIndex(command string, indexDoc *types.Document) (*backends.IndexInfo index.Name, ok = v.(string) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, "'name' option must be specified as a string", "createIndexes", ) @@ -323,8 +323,8 @@ func processIndex(command string, indexDoc *types.Document) (*backends.IndexInfo unique, ok := v.(bool) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "Error in specification { key: %s, name: %q, unique: %s } "+ ":: caused by :: "+ @@ -337,8 +337,8 @@ func processIndex(command string, indexDoc *types.Document) (*backends.IndexInfo } if len(index.Key) == 1 && index.Key[0].Field == "_id" { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidIndexSpecificationOption, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidIndexSpecificationOption, fmt.Sprintf("The field 'unique' is not valid for an _id index specification. "+ "Specification: { key: %s, name: %q, unique: true, v: 2 }", types.FormatAnyValue(must.NotFail(indexDoc.Get("key"))), index.Name, @@ -357,15 +357,15 @@ func processIndex(command string, indexDoc *types.Document) (*backends.IndexInfo case "sparse", "partialFilterExpression", "expireAfterSeconds", "hidden", "storageEngine", "weights", "default_language", "language_override", "textIndexVersion", "2dsphereIndexVersion", "bits", "min", "max", "bucketSize", "collation", "wildcardProjection": - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, fmt.Sprintf("Index option %q is not implemented yet", opt), command, ) default: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("Index option %q is unknown", opt), command, ) @@ -395,8 +395,8 @@ func processIndexKey(command string, keyDoc *types.Document) ([]backends.IndexKe } if _, ok := duplicateChecker[field]; ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf( "Error in specification %s, the field %q appears multiple times", types.FormatAnyValue(keyDoc), field, @@ -409,9 +409,9 @@ func processIndexKey(command string, keyDoc *types.Document) ([]backends.IndexKe var orderParam int64 - if orderParam, err = commonparams.GetWholeNumberParam(order); err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrIndexNotFound, + if orderParam, err = handlerparams.GetWholeNumberParam(order); err != nil { + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrIndexNotFound, fmt.Sprintf("can't find index with key: { %s: %q }", field, order), command, ) @@ -425,8 +425,8 @@ func processIndexKey(command string, keyDoc *types.Document) ([]backends.IndexKe case -1: descending = true default: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, fmt.Sprintf("Index key value %q is not implemented yet", orderParam), command, ) @@ -471,7 +471,7 @@ func validateIndexesForCreation(command string, existing, toCreate []backends.In newKey, newIdx.Name, ) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrCannotCreateIndex, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrCannotCreateIndex, msg, command) } if newIdx.Name == backends.DefaultIndexName && newKey != "_id: 1" { @@ -481,7 +481,7 @@ func validateIndexesForCreation(command string, existing, toCreate []backends.In newKey, ) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrBadValue, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrBadValue, msg, command) } // Iterate backwards to check if the current index is a duplicate of any other index provided in the list earlier. @@ -492,7 +492,7 @@ func validateIndexesForCreation(command string, existing, toCreate []backends.In if otherName == newIdx.Name && otherKey == newKey { msg := fmt.Sprintf("Identical index already exists: %s", otherName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrIndexAlreadyExists, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrIndexAlreadyExists, msg, command) } if newIdx.Name == otherName { @@ -504,7 +504,7 @@ func validateIndexesForCreation(command string, existing, toCreate []backends.In newKey, newIdx.Name, otherKey, otherName, ) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrIndexKeySpecsConflict, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrIndexKeySpecsConflict, msg, command) } if newKey == otherKey { @@ -512,7 +512,7 @@ func validateIndexesForCreation(command string, existing, toCreate []backends.In "Index already exists with a different name: %s", otherName, ) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrIndexOptionsConflict, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrIndexOptionsConflict, msg, command) } } @@ -535,12 +535,12 @@ func validateIndexesForCreation(command string, existing, toCreate []backends.In newKey, newIdx.Name, existingKey, existingIdx.Name, ) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrIndexKeySpecsConflict, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrIndexKeySpecsConflict, msg, command) } if newKey == existingKey { msg := fmt.Sprintf("Index already exists with a different name: %s", existingIdx.Name) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrIndexOptionsConflict, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrIndexOptionsConflict, msg, command) } } } diff --git a/internal/handler/msg_datasize.go b/internal/handler/msg_datasize.go index 999f6584e5e5..214515a7f684 100644 --- a/internal/handler/msg_datasize.go +++ b/internal/handler/msg_datasize.go @@ -21,8 +21,8 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -50,14 +50,14 @@ func (h *Handler) MsgDataSize(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg namespace, ok := namespaceParam.(string) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, - fmt.Sprintf("collection name has invalid type %s", commonparams.AliasFromType(namespaceParam)), + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, + fmt.Sprintf("collection name has invalid type %s", handlerparams.AliasFromType(namespaceParam)), document.Command(), ) } - dbName, cName, err := commonparams.SplitNamespace(namespace, document.Command()) + dbName, cName, err := handlerparams.SplitNamespace(namespace, document.Command()) if err != nil { return nil, err } @@ -66,7 +66,7 @@ func (h *Handler) MsgDataSize(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid database specified '%s'", dbName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) @@ -76,7 +76,7 @@ func (h *Handler) MsgDataSize(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", cName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_dbstats.go b/internal/handler/msg_dbstats.go index 00ed3c79d77c..dba0cd03b38f 100644 --- a/internal/handler/msg_dbstats.go +++ b/internal/handler/msg_dbstats.go @@ -20,8 +20,8 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -46,7 +46,7 @@ func (h *Handler) MsgDBStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, var s any if s, err = document.Get("scale"); err == nil { - if scale, err = commonparams.GetValidatedNumberParamWithMinValue(command, "scale", s, 1); err != nil { + if scale, err = handlerparams.GetValidatedNumberParamWithMinValue(command, "scale", s, 1); err != nil { return nil, err } } @@ -54,7 +54,7 @@ func (h *Handler) MsgDBStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, var freeStorage bool if v, _ := document.Get("freeStorage"); v != nil { - if freeStorage, err = commonparams.GetBoolOptionalParam("freeStorage", v); err != nil { + if freeStorage, err = handlerparams.GetBoolOptionalParam("freeStorage", v); err != nil { return nil, err } } @@ -63,7 +63,7 @@ func (h *Handler) MsgDBStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid database specified '%s'", dbName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_delete.go b/internal/handler/msg_delete.go index f74b21caed61..7920604a0586 100644 --- a/internal/handler/msg_delete.go +++ b/internal/handler/msg_delete.go @@ -23,7 +23,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -47,7 +47,7 @@ func (h *Handler) MsgDelete(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", params.DB, params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "delete") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "delete") } return nil, lazyerrors.Error(err) @@ -57,7 +57,7 @@ func (h *Handler) MsgDelete(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "delete") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "delete") } return nil, lazyerrors.Error(err) @@ -72,7 +72,7 @@ func (h *Handler) MsgDelete(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, deleted += d if err != nil { - var ce *commonerrors.CommandError + var ce *handlererrors.CommandError if errors.As(err, &ce) { we := &mongo.WriteError{ Index: i, @@ -114,7 +114,7 @@ func (h *Handler) MsgDelete(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, // execDelete performs a single delete operation. // // It returns a number of deleted documents or error. -// The error is either a (wrapped) *commonerrors.CommandError or something fatal. +// The error is either a (wrapped) *handlererrors.CommandError or something fatal. func (h *Handler) execDelete(ctx context.Context, c backends.Collection, p *common.Delete) (int32, error) { var qp backends.QueryParams if !h.DisableFilterPushdown { diff --git a/internal/handler/msg_distinct.go b/internal/handler/msg_distinct.go index b29789dd3250..f0c4375db78d 100644 --- a/internal/handler/msg_distinct.go +++ b/internal/handler/msg_distinct.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -44,7 +44,7 @@ func (h *Handler) MsgDistinct(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", params.DB, params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) @@ -54,7 +54,7 @@ func (h *Handler) MsgDistinct(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_drop.go b/internal/handler/msg_drop.go index 11abacbd134e..8bb98229bb9a 100644 --- a/internal/handler/msg_drop.go +++ b/internal/handler/msg_drop.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -63,7 +63,7 @@ func (h *Handler) MsgDrop(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s'", dbName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "drop") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "drop") } return nil, lazyerrors.Error(err) @@ -88,10 +88,10 @@ func (h *Handler) MsgDrop(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er case backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid): msg := fmt.Sprintf("Invalid collection name: %s", collectionName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "drop") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "drop") case backends.ErrorCodeIs(err, backends.ErrorCodeCollectionDoesNotExist): - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrNamespaceNotFound, "ns not found", "drop") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrNamespaceNotFound, "ns not found", "drop") default: return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_dropindexes.go b/internal/handler/msg_dropindexes.go index c2a5c74a1895..0d698afcdd47 100644 --- a/internal/handler/msg_dropindexes.go +++ b/internal/handler/msg_dropindexes.go @@ -21,8 +21,8 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -53,7 +53,7 @@ func (h *Handler) MsgDropIndexes(ctx context.Context, msg *wire.OpMsg) (*wire.Op if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", dbName, collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, command) } return nil, lazyerrors.Error(err) @@ -63,7 +63,7 @@ func (h *Handler) MsgDropIndexes(ctx context.Context, msg *wire.OpMsg) (*wire.Op if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", dbName, collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, command) } return nil, lazyerrors.Error(err) @@ -71,8 +71,8 @@ func (h *Handler) MsgDropIndexes(ctx context.Context, msg *wire.OpMsg) (*wire.Op indexValue, err := document.Get("index") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrMissingField, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrMissingField, "BSON field 'dropIndexes.index' is missing but a required field", command, ) @@ -135,14 +135,14 @@ func processDropIndexOptions(command, ns string, v any, existing []backends.Inde } if len(spec) == 1 && spec[0].Field == "_id" && !spec[0].Descending { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidOptions, "cannot drop _id index", command, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidOptions, "cannot drop _id index", command, ) } if len(existing) == 0 { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNamespaceNotFound, fmt.Sprintf("ns not found %s", ns), command, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNamespaceNotFound, fmt.Sprintf("ns not found %s", ns), command, ) } @@ -163,7 +163,7 @@ func processDropIndexOptions(command, ns string, v any, existing []backends.Inde msg := fmt.Sprintf("can't find index with key: { %v }", formatIndexKey(spec)) - return nil, false, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrIndexNotFound, msg, command) + return nil, false, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrIndexNotFound, msg, command) case *types.Array: // List of index names is provided to drop multiple indexes. @@ -187,25 +187,25 @@ func processDropIndexOptions(command, ns string, v any, existing []backends.Inde index, ok := val.(string) if !ok { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "BSON field 'dropIndexes.index' is the wrong type '%s', expected types '[string, object]'", - commonparams.AliasFromType(v), + handlerparams.AliasFromType(v), ), command, ) } if len(existing) == 0 { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNamespaceNotFound, fmt.Sprintf("ns not found %s", ns), command, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNamespaceNotFound, fmt.Sprintf("ns not found %s", ns), command, ) } if index == backends.DefaultIndexName { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidOptions, "cannot drop _id index", command, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidOptions, "cannot drop _id index", command, ) } @@ -221,8 +221,8 @@ func processDropIndexOptions(command, ns string, v any, existing []backends.Inde } if !found { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrIndexNotFound, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrIndexNotFound, fmt.Sprintf("index not found with name [%s]", index), command, ) @@ -246,14 +246,14 @@ func processDropIndexOptions(command, ns string, v any, existing []backends.Inde } if len(existing) == 0 { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNamespaceNotFound, fmt.Sprintf("ns not found %s", ns), command, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNamespaceNotFound, fmt.Sprintf("ns not found %s", ns), command, ) } if v == backends.DefaultIndexName { - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidOptions, "cannot drop _id index", command, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidOptions, "cannot drop _id index", command, ) } @@ -263,18 +263,18 @@ func processDropIndexOptions(command, ns string, v any, existing []backends.Inde } } - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrIndexNotFound, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrIndexNotFound, fmt.Sprintf("index not found with name [%s]", v), command, ) } - return nil, false, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, false, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "BSON field 'dropIndexes.index' is the wrong type '%s', expected types '[string, object]'", - commonparams.AliasFromType(v), + handlerparams.AliasFromType(v), ), command, ) diff --git a/internal/handler/msg_explain.go b/internal/handler/msg_explain.go index be9b75c342af..02f2aea9c571 100644 --- a/internal/handler/msg_explain.go +++ b/internal/handler/msg_explain.go @@ -23,7 +23,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" "github.com/FerretDB/FerretDB/internal/handler/common/aggregations" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -63,7 +63,7 @@ func (h *Handler) MsgExplain(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", params.DB, params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) @@ -73,7 +73,7 @@ func (h *Handler) MsgExplain(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_find.go b/internal/handler/msg_find.go index acc77949880b..fb9534337864 100644 --- a/internal/handler/msg_find.go +++ b/internal/handler/msg_find.go @@ -26,7 +26,7 @@ import ( "github.com/FerretDB/FerretDB/internal/clientconn/conninfo" "github.com/FerretDB/FerretDB/internal/clientconn/cursor" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -52,7 +52,7 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", params.DB, params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "find") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "find") } return nil, lazyerrors.Error(err) @@ -62,7 +62,7 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "find") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "find") } return nil, lazyerrors.Error(err) @@ -86,8 +86,8 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er } if !cInfo.Capped() { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "tailable cursor requested on non capped collection", "tailable", ) @@ -145,8 +145,8 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er var pathErr *types.PathError if errors.As(err, &pathErr) && pathErr.Code() == types.ErrPathElementEmpty { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrPathContainsEmptyElement, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrPathContainsEmptyElement, "Empty field names in path are not allowed", document.Command(), ) diff --git a/internal/handler/msg_findandmodify.go b/internal/handler/msg_findandmodify.go index 0099c2a59d98..ad804f85c44b 100644 --- a/internal/handler/msg_findandmodify.go +++ b/internal/handler/msg_findandmodify.go @@ -24,7 +24,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -105,7 +105,7 @@ func (h *Handler) findAndModifyDocument(ctx context.Context, params *common.Find // TODO https://github.com/FerretDB/FerretDB/issues/2168 if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", params.DB, params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "findAndModify") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "findAndModify") } return nil, lazyerrors.Error(err) @@ -116,7 +116,7 @@ func (h *Handler) findAndModifyDocument(ctx context.Context, params *common.Find // TODO https://github.com/FerretDB/FerretDB/issues/2168 if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "findAndModify") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "findAndModify") } return nil, lazyerrors.Error(err) @@ -150,8 +150,8 @@ func (h *Handler) findAndModifyDocument(ctx context.Context, params *common.Find if err != nil { var pathErr *types.PathError if errors.As(err, &pathErr) && pathErr.Code() == types.ErrPathElementEmpty { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrPathContainsEmptyElement, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrPathContainsEmptyElement, "FieldPath field names may not be empty strings.", "findAndModify", ) @@ -236,7 +236,7 @@ func (h *Handler) findAndModifyDocument(ctx context.Context, params *common.Find // TODO https://github.com/FerretDB/FerretDB/issues/2168 we := &mongo.WriteError{ Index: 0, - Code: int(commonerrors.ErrDuplicateKeyInsert), + Code: int(handlererrors.ErrDuplicateKeyInsert), Message: fmt.Sprintf(`E11000 duplicate key error collection: %s.%s`, params.DB, params.Collection), } writeErrors.Append(WriteErrorDocument(we)) @@ -293,8 +293,8 @@ func (h *Handler) findAndModifyDocument(ctx context.Context, params *common.Find } if updateID != nil && updateID != id { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrImmutableField, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrImmutableField, fmt.Sprintf( `Plan executor error during findAndModify :: caused `+ `by :: After applying the update, the (immutable) field `+ @@ -346,13 +346,13 @@ func handleValidationError(err error) (*mongo.WriteError, error) { return nil, lazyerrors.Error(err) } - var code commonerrors.ErrorCode + var code handlererrors.ErrorCode switch ve.Code() { case types.ErrValidation, types.ErrIDNotFound: - code = commonerrors.ErrBadValue + code = handlererrors.ErrBadValue case types.ErrWrongIDType: - code = commonerrors.ErrInvalidID + code = handlererrors.ErrInvalidID default: panic(fmt.Sprintf("Unknown error code: %v", ve.Code())) } diff --git a/internal/handler/msg_getlog.go b/internal/handler/msg_getlog.go index 82d6dceb7a71..160bd7b18b57 100644 --- a/internal/handler/msg_getlog.go +++ b/internal/handler/msg_getlog.go @@ -24,8 +24,8 @@ import ( "go.uber.org/zap" "github.com/FerretDB/FerretDB/build/version" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/debugbuild" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -49,18 +49,18 @@ func (h *Handler) MsgGetLog(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, } if _, ok := getLog.(types.NullType); ok { - return nil, commonerrors.NewCommandErrorMsg( - commonerrors.ErrMissingField, + return nil, handlererrors.NewCommandErrorMsg( + handlererrors.ErrMissingField, `BSON field 'getLog.getLog' is missing but a required field`, ) } if _, ok := getLog.(string); !ok { - return nil, commonerrors.NewCommandError( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandError( + handlererrors.ErrTypeMismatch, fmt.Errorf( "BSON field 'getLog.getLog' is the wrong type '%s', expected type 'string'", - commonparams.AliasFromType(getLog), + handlerparams.AliasFromType(getLog), ), ) } @@ -150,8 +150,8 @@ func (h *Handler) MsgGetLog(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, )) default: - return nil, commonerrors.NewCommandError( - commonerrors.ErrOperationFailed, + return nil, handlererrors.NewCommandError( + handlererrors.ErrOperationFailed, fmt.Errorf("no RecentEntries named: %s", getLog), ) } diff --git a/internal/handler/msg_getmore.go b/internal/handler/msg_getmore.go index d5869c0c1111..2a10545e6d6d 100644 --- a/internal/handler/msg_getmore.go +++ b/internal/handler/msg_getmore.go @@ -22,8 +22,8 @@ import ( "github.com/FerretDB/FerretDB/internal/clientconn/conninfo" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -47,8 +47,8 @@ func (h *Handler) MsgGetMore(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, // TODO https://github.com/FerretDB/FerretDB/issues/2859 v, _ := document.Get("collection") if v == nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrMissingField, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrMissingField, "BSON field 'getMore.collection' is missing but a required field", document.Command(), ) @@ -56,19 +56,19 @@ func (h *Handler) MsgGetMore(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, collection, ok := v.(string) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "BSON field 'getMore.collection' is the wrong type '%s', expected type 'string'", - commonparams.AliasFromType(v), + handlerparams.AliasFromType(v), ), document.Command(), ) } if collection == "" { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidNamespace, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidNamespace, "Collection names cannot be empty", document.Command(), ) @@ -76,8 +76,8 @@ func (h *Handler) MsgGetMore(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, cursorID, err := common.GetRequiredParam[int64](document, document.Command()) if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, "BSON field 'getMore.getMore' is the wrong type, expected type 'long'", document.Command(), ) @@ -89,36 +89,36 @@ func (h *Handler) MsgGetMore(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, v = int64(0) } - // cannot use other existing commonparams function, they return different error codes - maxTimeMS, err := commonparams.GetWholeNumberParam(v) + // cannot use other existing handlerparams function, they return different error codes + maxTimeMS, err := handlerparams.GetWholeNumberParam(v) if err != nil { switch { - case errors.Is(err, commonparams.ErrUnexpectedType): + case errors.Is(err, handlerparams.ErrUnexpectedType): if _, ok = v.(types.NullType); ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "maxTimeMS must be a number", document.Command(), ) } - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( `BSON field 'getMore.maxTimeMS' is the wrong type '%s', expected types '[long, int, decimal, double]'`, - commonparams.AliasFromType(v), + handlerparams.AliasFromType(v), ), document.Command(), ) - case errors.Is(err, commonparams.ErrNotWholeNumber): - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + case errors.Is(err, handlerparams.ErrNotWholeNumber): + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "maxTimeMS has non-integral value", document.Command(), ) - case errors.Is(err, commonparams.ErrLongExceededPositive) || errors.Is(err, commonparams.ErrLongExceededNegative): - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + case errors.Is(err, handlerparams.ErrLongExceededPositive) || errors.Is(err, handlerparams.ErrLongExceededNegative): + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%s value for maxTimeMS is out of range", types.FormatAnyValue(v)), document.Command(), ) @@ -128,8 +128,8 @@ func (h *Handler) MsgGetMore(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, } if maxTimeMS < int64(0) || maxTimeMS > math.MaxInt32 { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("%v value for maxTimeMS is out of range", v), document.Command(), ) @@ -144,8 +144,8 @@ func (h *Handler) MsgGetMore(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, // TODO https://github.com/FerretDB/FerretDB/issues/2859 cursor := h.cursors.Get(cursorID) if cursor == nil || cursor.Username != username { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrCursorNotFound, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrCursorNotFound, fmt.Sprintf("cursor id %d not found", cursorID), document.Command(), ) @@ -160,14 +160,14 @@ func (h *Handler) MsgGetMore(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, v = int32(250) } - batchSize, err := commonparams.GetValidatedNumberParamWithMinValue(document.Command(), "batchSize", v, 0) + batchSize, err := handlerparams.GetValidatedNumberParamWithMinValue(document.Command(), "batchSize", v, 0) if err != nil { return nil, err } if cursor.DB != db || cursor.Collection != collection { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrUnauthorized, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrUnauthorized, fmt.Sprintf( "Requested getMore on namespace '%s.%s', but cursor belongs to a different namespace %s.%s", db, diff --git a/internal/handler/msg_insert.go b/internal/handler/msg_insert.go index 8bef9a53f126..d439f9a11d75 100644 --- a/internal/handler/msg_insert.go +++ b/internal/handler/msg_insert.go @@ -25,7 +25,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -61,7 +61,7 @@ func (h *Handler) MsgInsert(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", params.DB, params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "insert") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "insert") } return nil, lazyerrors.Error(err) @@ -71,7 +71,7 @@ func (h *Handler) MsgInsert(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", params.Collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "insert") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "insert") } return nil, lazyerrors.Error(err) @@ -124,12 +124,12 @@ func (h *Handler) MsgInsert(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, return nil, lazyerrors.Error(err) } - var code commonerrors.ErrorCode + var code handlererrors.ErrorCode switch ve.Code() { case types.ErrValidation, types.ErrIDNotFound: - code = commonerrors.ErrBadValue + code = handlererrors.ErrBadValue case types.ErrWrongIDType: - code = commonerrors.ErrInvalidID + code = handlererrors.ErrInvalidID default: panic(fmt.Sprintf("Unknown error code: %v", ve.Code())) } @@ -171,7 +171,7 @@ func (h *Handler) MsgInsert(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, writeErrors = append(writeErrors, &mongo.WriteError{ Index: docsIndexes[j], - Code: int(commonerrors.ErrDuplicateKeyInsert), + Code: int(handlererrors.ErrDuplicateKeyInsert), Message: fmt.Sprintf(`E11000 duplicate key error collection: %s.%s`, params.DB, params.Collection), }) diff --git a/internal/handler/msg_killcursors.go b/internal/handler/msg_killcursors.go index 4aed0f6d6234..0029eb846eed 100644 --- a/internal/handler/msg_killcursors.go +++ b/internal/handler/msg_killcursors.go @@ -21,8 +21,8 @@ import ( "github.com/FerretDB/FerretDB/internal/clientconn/conninfo" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -77,12 +77,12 @@ func (h *Handler) MsgKillCursors(ctx context.Context, msg *wire.OpMsg) (*wire.Op id, ok := v.(int64) if !ok { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf( "BSON field 'killCursors.cursors.%d' is the wrong type '%s', expected type 'long'", i, - commonparams.AliasFromType(v), + handlerparams.AliasFromType(v), ), command, ) diff --git a/internal/handler/msg_listcollections.go b/internal/handler/msg_listcollections.go index 874caa3ad458..1904ebaac20f 100644 --- a/internal/handler/msg_listcollections.go +++ b/internal/handler/msg_listcollections.go @@ -20,8 +20,8 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -53,7 +53,7 @@ func (h *Handler) MsgListCollections(ctx context.Context, msg *wire.OpMsg) (*wir var nameOnly bool if v, _ := document.Get("nameOnly"); v != nil { - if nameOnly, err = commonparams.GetBoolOptionalParam("nameOnly", v); err != nil { + if nameOnly, err = handlerparams.GetBoolOptionalParam("nameOnly", v); err != nil { return nil, err } } @@ -62,7 +62,7 @@ func (h *Handler) MsgListCollections(ctx context.Context, msg *wire.OpMsg) (*wir if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s'", dbName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "listCollections") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "listCollections") } return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_listdatabases.go b/internal/handler/msg_listdatabases.go index f9e1916da519..74254db75ba9 100644 --- a/internal/handler/msg_listdatabases.go +++ b/internal/handler/msg_listdatabases.go @@ -20,7 +20,7 @@ import ( "go.uber.org/zap" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -47,7 +47,7 @@ func (h *Handler) MsgListDatabases(ctx context.Context, msg *wire.OpMsg) (*wire. var nameOnly bool if v, _ := document.Get("nameOnly"); v != nil { - if nameOnly, err = commonparams.GetBoolOptionalParam("nameOnly", v); err != nil { + if nameOnly, err = handlerparams.GetBoolOptionalParam("nameOnly", v); err != nil { return nil, err } } diff --git a/internal/handler/msg_listindexes.go b/internal/handler/msg_listindexes.go index 8a9875b1c65c..9c1aadf9d4c9 100644 --- a/internal/handler/msg_listindexes.go +++ b/internal/handler/msg_listindexes.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -50,7 +50,7 @@ func (h *Handler) MsgListIndexes(ctx context.Context, msg *wire.OpMsg) (*wire.Op if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid database specified '%s'", dbName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) @@ -60,7 +60,7 @@ func (h *Handler) MsgListIndexes(ctx context.Context, msg *wire.OpMsg) (*wire.Op if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command()) } return nil, lazyerrors.Error(err) @@ -70,7 +70,7 @@ func (h *Handler) MsgListIndexes(ctx context.Context, msg *wire.OpMsg) (*wire.Op if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionDoesNotExist) { msg := fmt.Sprintf("ns does not exist: %s.%s", dbName, collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrNamespaceNotFound, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrNamespaceNotFound, msg, document.Command()) } return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_ping.go b/internal/handler/msg_ping.go index da475cc952e5..888865395c35 100644 --- a/internal/handler/msg_ping.go +++ b/internal/handler/msg_ping.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -42,7 +42,7 @@ func (h *Handler) MsgPing(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er if _, err = h.b.Database(dbName); err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s'", dbName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "ping") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "ping") } return nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_renamecollection.go b/internal/handler/msg_renamecollection.go index 3d1b9378b2d8..e281709f2d0b 100644 --- a/internal/handler/msg_renamecollection.go +++ b/internal/handler/msg_renamecollection.go @@ -20,8 +20,8 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" - "github.com/FerretDB/FerretDB/internal/handler/commonparams" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" + "github.com/FerretDB/FerretDB/internal/handler/handlerparams" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -58,31 +58,31 @@ func (h *Handler) MsgRenameCollection(ctx context.Context, msg *wire.OpMsg) (*wi if err != nil { from, _ := document.Get(command) - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, - fmt.Sprintf("collection name has invalid type %s", commonparams.AliasFromType(from)), + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, + fmt.Sprintf("collection name has invalid type %s", handlerparams.AliasFromType(from)), command, ) } newName, err := common.GetRequiredParam[string](document, "to") if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, "'to' must be of type String", command, ) } - oldDBName, oldCName, err := commonparams.SplitNamespace(oldName, command) + oldDBName, oldCName, err := handlerparams.SplitNamespace(oldName, command) if err != nil { return nil, err } - newDBName, newCName, err := commonparams.SplitNamespace(newName, command) + newDBName, newCName, err := handlerparams.SplitNamespace(newName, command) if err != nil { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrInvalidNamespace, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrInvalidNamespace, fmt.Sprintf("Invalid target namespace: %s", newName), command, ) @@ -91,16 +91,16 @@ func (h *Handler) MsgRenameCollection(ctx context.Context, msg *wire.OpMsg) (*wi // support cross-database rename // TODO https://github.com/FerretDB/FerretDB/issues/2563 if oldDBName != newDBName { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNotImplemented, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNotImplemented, "Command renameCollection does not support cross-database rename", command, ) } if oldCName == newCName { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrIllegalOperation, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrIllegalOperation, "Can't rename a collection to itself", command, ) @@ -110,7 +110,7 @@ func (h *Handler) MsgRenameCollection(ctx context.Context, msg *wire.OpMsg) (*wi if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s'", oldName) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, command) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, command) } return nil, lazyerrors.Error(err) @@ -125,20 +125,20 @@ func (h *Handler) MsgRenameCollection(ctx context.Context, msg *wire.OpMsg) (*wi case err == nil: // do nothing case backends.ErrorCodeIs(err, backends.ErrorCodeCollectionAlreadyExists): - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNamespaceExists, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNamespaceExists, "target namespace exists", command, ) case backends.ErrorCodeIs(err, backends.ErrorCodeCollectionDoesNotExist): - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrNamespaceNotFound, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrNamespaceNotFound, fmt.Sprintf("Source collection %s does not exist", oldName), command, ) case backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid): - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrIllegalOperation, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrIllegalOperation, fmt.Sprintf("error with target namespace: Invalid collection name: %s", newCName), command, ) diff --git a/internal/handler/msg_saslstart.go b/internal/handler/msg_saslstart.go index 07d4cfd2654d..9836cf863482 100644 --- a/internal/handler/msg_saslstart.go +++ b/internal/handler/msg_saslstart.go @@ -22,7 +22,7 @@ import ( "github.com/FerretDB/FerretDB/internal/clientconn/conninfo" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -64,7 +64,7 @@ func (h *Handler) MsgSASLStart(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs default: msg := fmt.Sprintf("Unsupported authentication mechanism %q.\n", mechanism) + "See https://docs.ferretdb.io/security/authentication/ for more details." - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrAuthenticationFailed, msg, "mechanism") + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrAuthenticationFailed, msg, "mechanism") } conninfo.Get(ctx).SetAuth(username, password) @@ -91,8 +91,8 @@ func saslStartPlain(doc *types.Document) (string, string, error) { stringPayload, err := common.GetRequiredParam[string](doc, "payload") if err == nil { if payload, err = base64.StdEncoding.DecodeString(stringPayload); err != nil { - return "", "", commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return "", "", handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf("Invalid payload: %v", err), "payload", ) @@ -112,8 +112,8 @@ func saslStartPlain(doc *types.Document) (string, string, error) { parts := bytes.Split(payload, []byte{0}) if l := len(parts); l != 3 { - return "", "", commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + return "", "", handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, fmt.Sprintf("Invalid payload: expected 3 parts, got %d", l), "payload", ) diff --git a/internal/handler/msg_saslstart_plain_test.go b/internal/handler/msg_saslstart_plain_test.go index 5ae39262efc6..4d4750c31878 100644 --- a/internal/handler/msg_saslstart_plain_test.go +++ b/internal/handler/msg_saslstart_plain_test.go @@ -20,7 +20,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/must" ) @@ -43,32 +43,32 @@ func TestSaslStartPlain(t *testing.T) { }{ "emptyPayload": { doc: types.MakeDocument(0), - err: commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + err: handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `required parameter "payload" is missing`, "payload", ), }, "wrongTypePayload": { doc: must.NotFail(types.NewDocument("payload", int32(42))), - err: commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + err: handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, `required parameter "payload" has type int32 (expected types.Binary)`, "payload", ), }, "stringPayloadInvalid": { doc: must.NotFail(types.NewDocument("payload", "ABC")), - err: commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + err: handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, "Invalid payload: illegal base64 data at input byte 0", "payload", ), }, "binaryPayloadInvalid": { doc: must.NotFail(types.NewDocument("payload", types.Binary{B: []byte("ABC")})), - err: commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrTypeMismatch, + err: handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrTypeMismatch, "Invalid payload: expected 3 parts, got 1", "payload", ), diff --git a/internal/handler/msg_setfreemonitoring.go b/internal/handler/msg_setfreemonitoring.go index 0e9df5393bde..a81a3b286d99 100644 --- a/internal/handler/msg_setfreemonitoring.go +++ b/internal/handler/msg_setfreemonitoring.go @@ -19,7 +19,7 @@ import ( "fmt" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -47,8 +47,8 @@ func (h *Handler) MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*w case "disable": telemetryState = false default: - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrBadValue, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrBadValue, fmt.Sprintf( "Enumeration value '%s' for field '%s' is not a valid value.", action, @@ -59,8 +59,8 @@ func (h *Handler) MsgSetFreeMonitoring(ctx context.Context, msg *wire.OpMsg) (*w } if h.StateProvider.Get().TelemetryLocked { - return nil, commonerrors.NewCommandErrorMsgWithArgument( - commonerrors.ErrFreeMonitoringDisabled, + return nil, handlererrors.NewCommandErrorMsgWithArgument( + handlererrors.ErrFreeMonitoringDisabled, "Free Monitoring has been disabled via the command-line and/or config file", action, ) diff --git a/internal/handler/msg_update.go b/internal/handler/msg_update.go index 9e381dc6016e..21cc0db148a5 100644 --- a/internal/handler/msg_update.go +++ b/internal/handler/msg_update.go @@ -23,7 +23,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/iterator" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -55,7 +55,7 @@ func (h *Handler) MsgUpdate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, // TODO https://github.com/FerretDB/FerretDB/issues/3263 we = &mongo.WriteError{ Index: 0, - Code: int(commonerrors.ErrDuplicateKeyInsert), + Code: int(handlererrors.ErrDuplicateKeyInsert), Message: fmt.Sprintf(`E11000 duplicate key error collection: %s.%s`, params.DB, params.Collection), } @@ -98,7 +98,7 @@ func (h *Handler) updateDocument(ctx context.Context, params *common.UpdateParam if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeDatabaseNameIsInvalid) { msg := fmt.Sprintf("Invalid namespace specified '%s.%s'", params.DB, params.Collection) - return 0, 0, nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "update") + return 0, 0, nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "update") } return 0, 0, nil, lazyerrors.Error(err) @@ -113,7 +113,7 @@ func (h *Handler) updateDocument(ctx context.Context, params *common.UpdateParam // nothing case backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid): msg := fmt.Sprintf("Invalid collection name: %s", params.Collection) - return 0, 0, nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "insert") + return 0, 0, nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "insert") default: return 0, 0, nil, lazyerrors.Error(err) } @@ -123,7 +123,7 @@ func (h *Handler) updateDocument(ctx context.Context, params *common.UpdateParam if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionNameIsInvalid) { msg := fmt.Sprintf("Invalid collection name: %s", params.Collection) - return 0, 0, nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrInvalidNamespace, msg, "insert") + return 0, 0, nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, "insert") } return 0, 0, nil, lazyerrors.Error(err) diff --git a/internal/handler/msg_validate.go b/internal/handler/msg_validate.go index 0e207d95cdea..09e15829e3e6 100644 --- a/internal/handler/msg_validate.go +++ b/internal/handler/msg_validate.go @@ -20,7 +20,7 @@ import ( "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/handler/common" - "github.com/FerretDB/FerretDB/internal/handler/commonerrors" + "github.com/FerretDB/FerretDB/internal/handler/handlererrors" "github.com/FerretDB/FerretDB/internal/types" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" "github.com/FerretDB/FerretDB/internal/util/must" @@ -62,7 +62,7 @@ func (h *Handler) MsgValidate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg if err != nil { if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionDoesNotExist) { msg := fmt.Sprintf("Collection '%s.%s' does not exist to validate.", dbName, collection) - return nil, commonerrors.NewCommandErrorMsgWithArgument(commonerrors.ErrNamespaceNotFound, msg, document.Command()) + return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrNamespaceNotFound, msg, document.Command()) } return nil, lazyerrors.Error(err)