Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading