Просмотр исходного кода

add a way to compare boundary errors (#2151)

Signed-off-by: nickcurie <ncurie@kubecost.com>
Nick Curie 2 лет назад
Родитель
Сommit
61bf489e01
2 измененных файлов с 30 добавлено и 0 удалено
  1. 8 0
      pkg/kubecost/window.go
  2. 22 0
      pkg/kubecost/window_test.go

+ 8 - 0
pkg/kubecost/window.go

@@ -875,3 +875,11 @@ func (be *BoundaryError) Error() string {
 
 	return fmt.Sprintf("boundary error: requested %s; supported %s: %s", be.Requested, be.Supported, be.Message)
 }
+
+func (be *BoundaryError) Is(target error) bool {
+	if _, ok := target.(*BoundaryError); ok {
+		return true
+	}
+
+	return false
+}

+ 22 - 0
pkg/kubecost/window_test.go

@@ -2,6 +2,7 @@ package kubecost
 
 import (
 	"encoding/json"
+	"errors"
 	"fmt"
 	"strings"
 	"testing"
@@ -1219,3 +1220,24 @@ func TestMarshalUnmarshal(t *testing.T) {
 		})
 	}
 }
+
+func TestBoundaryErrorIs(t *testing.T) {
+	baseError := &BoundaryError{Message: "cde"}
+	singleWrapError := fmt.Errorf("wrap: %w", &BoundaryError{Message: "abc"})
+
+	if errors.Is(singleWrapError, baseError) {
+		t.Logf("Single wrap success")
+	} else {
+		t.Errorf("Single wrap failure: %s != %s", baseError, singleWrapError)
+	}
+
+	multiWrapError := fmt.Errorf("wrap: %w", &BoundaryError{Message: "abc"})
+	multiWrapError = fmt.Errorf("wrap x2: %w", multiWrapError)
+	multiWrapError = fmt.Errorf("wrap x3: %w", multiWrapError)
+
+	if errors.Is(multiWrapError, baseError) {
+		t.Logf("Multi wrap success")
+	} else {
+		t.Errorf("Multi wrap failure: %s != %s", baseError, multiWrapError)
+	}
+}