Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify and deprecate commonerrors.WriteErrors #3258

Merged
merged 8 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
AlekSi committed Aug 28, 2023
commit 426b969950599347d20c0f794b80f77a8bcdc15d
11 changes: 6 additions & 5 deletions internal/handlers/commonerrors/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,19 @@ func NewCommandErrorMsgWithArgument(code ErrorCode, msg string, argument string)
}
}

// Error implements error interface.
func (e *CommandError) Error() string {
return fmt.Sprintf("%[1]s (%[1]d): %[2]v", e.code, e.err)
}

// Err returns original error.
//
// It is not called Unwrap to prevent unwrapping by errors.Is and errors.As.
// CommandError should not be unwrappable.
func (e *CommandError) Err() error {
return e.err
}

// Error implements error interface.
func (e *CommandError) Error() string {
return fmt.Sprintf("%[1]s (%[1]d): %[2]v", e.code, e.err)
}

// Code implements ProtoErr interface.
func (e *CommandError) Code() ErrorCode {
return e.code
Expand Down
12 changes: 9 additions & 3 deletions internal/handlers/commonerrors/commonerrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@

package commonerrors

import "testing"
import (
"io"
"testing"

func TestDummy(t *testing.T) {
// we need at least one test per package to correctly calculate coverage
"github.com/stretchr/testify/assert"
)

func TestNoWrapping(t *testing.T) {
err := NewCommandError(errInternalError, io.EOF)
assert.NotErrorIs(t, err, io.EOF)
}
5 changes: 4 additions & 1 deletion internal/handlers/commonerrors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,13 @@ type ErrInfo struct {
type ProtoErr interface {
// Error returns error representation for logging and debugging.
error

// Code returns error's code.
Code() ErrorCode
// Document returns a document representation of the error.

// Document returns error representation for returning to the client.
Document() *types.Document

// Info returns additional error information, or nil.
Info() *ErrInfo
}
Expand Down
21 changes: 14 additions & 7 deletions internal/handlers/commonerrors/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ import (
"github.com/FerretDB/FerretDB/internal/util/must"
)

// writeError represents protocol write error.
// It required to build the correct write error result.
// The index field is optional and won't be used if it's nil.
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
type writeError struct {
index int32
errmsg string
code ErrorCode
}

// WriteErrors represents a slice of protocol write errors.
// It could be returned for Update, Insert, Delete, and Replace operations.
type WriteErrors struct {
Expand Down Expand Up @@ -68,9 +77,7 @@ func (we *WriteErrors) Document() *types.Document {
for _, e := range we.errs {
doc := must.NotFail(types.NewDocument())

if e.index != nil {
doc.Set("index", *e.index)
}
doc.Set("index", e.index)

// Fields "code" and "errmsg" must always be filled in so that clients can parse the error message.
// Otherwise, the mongo client would parse it as a CommandError.
Expand Down Expand Up @@ -101,21 +108,21 @@ func (we *WriteErrors) Append(err error, index int32) {

switch {
case errors.As(err, &writeErr):
writeErr.index = &index
writeErr.index = index
we.errs = append(we.errs, *writeErr)

case errors.As(err, &cmdErr):
we.errs = append(we.errs, writeError{
code: cmdErr.code,
errmsg: cmdErr.err.Error(),
index: &index,
index: index,
})

default:
we.errs = append(we.errs, writeError{
code: errInternalError,
errmsg: err.Error(),
index: &index,
index: index,
})
}
}
Expand All @@ -128,7 +135,7 @@ func (we *WriteErrors) Len() int {
// Merge merges the given WriteErrors with the current one and sets the given index.
func (we *WriteErrors) Merge(we2 *WriteErrors, index int32) {
for _, e := range we2.errs {
e.index = &index
e.index = index
we.errs = append(we.errs, e)
}
}
Expand Down
34 changes: 0 additions & 34 deletions internal/handlers/commonerrors/write_error.go

This file was deleted.