From 816ae7bf44d8556b77e0d22f139b6e4dd13d698d Mon Sep 17 00:00:00 2001 From: Niko Hobart Date: Thu, 14 Nov 2024 16:53:27 -0800 Subject: [PATCH] fix for uncomparable error --- pkg/errors/errors.go | 2 +- pkg/errors/errors_test.go | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index cf356895..b44829fe 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -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 } diff --git a/pkg/errors/errors_test.go b/pkg/errors/errors_test.go index 89bab9c8..4207aaf0 100644 --- a/pkg/errors/errors_test.go +++ b/pkg/errors/errors_test.go @@ -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 @@ -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