Skip to content

Commit

Permalink
fix for uncomparable error
Browse files Browse the repository at this point in the history
  • Loading branch information
nikohobart committed Nov 15, 2024
1 parent fa086c8 commit 816ae7b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func getSingleMocErrorCode(err error) moccodes.MocCode {

// Get the cause of the error
cerr := perrors.Cause(err)
if cerr == nil || cerr == err {
if cerr == nil {
return moccodes.Unknown
}

Expand Down
46 changes: 46 additions & 0 deletions pkg/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import (
"github.com/microsoft/moc/rpc/common"
)

type NotComparableError struct {
msg string
data []byte // Slices make structs not comparable
}

func (e NotComparableError) Error() string {
return e.msg
}

func TestNewMocErrorWithError(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -159,6 +168,43 @@ func TestGetMocErrorCode(t *testing.T) {
}
}

func TestGetSingleMocErrorCodeAvoidsPanicOnUncomparable(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("unexpected panic occurred: %v", r)
}
}()

// Create a NotComparableError
detailedErr := NotComparableError{
msg: "detailed error",
}

// Call getSingleMocErrorCode with the NotComparableError
code := getSingleMocErrorCode(detailedErr)

// Check the result
if code != moccodes.Unknown {
t.Errorf("expected %v, got %v", moccodes.Unknown, code)
}
}

func TestCheckErrorAvoidsPanicOnUncomparable(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("unexpected panic occurred: %v", r)
}
}()

// Create a NotComparableError
detailedErr := NotComparableError{
msg: "detailed error",
}

// Make sure checkError doesn't panic
checkError(detailedErr, NotFound)
}

func TestCheckError(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 816ae7b

Please sign in to comment.