Skip to content

Commit

Permalink
chore: Ensure that we implement Unwrap to support errors.As (#1975)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Feb 8, 2025
1 parent 403034a commit 8e8b99d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pkg/cloudprovider/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cloudprovider_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/samber/lo"

"sigs.k8s.io/karpenter/pkg/cloudprovider"
)

func TestCloudProvider(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "CloudProvider Suite")
}

var _ = Describe("CloudProvider", func() {
It("should support unwrapping for NodeClaimNotFound", func() {
err := cloudprovider.NewNodeClaimNotFoundError(&BaseError{})
_, ok := lo.ErrorsAs[*BaseError](err)
Expect(ok).To(BeTrue())
})
It("should support unwrapping for InsufficientCapacity", func() {
err := cloudprovider.NewInsufficientCapacityError(&BaseError{})
_, ok := lo.ErrorsAs[*BaseError](err)
Expect(ok).To(BeTrue())
})
It("should support unwrapping for NodeClassNotReady", func() {
err := cloudprovider.NewNodeClassNotReadyError(&BaseError{})
_, ok := lo.ErrorsAs[*BaseError](err)
Expect(ok).To(BeTrue())
})
It("should support unwrapping for CreateError", func() {
err := cloudprovider.NewCreateError(&BaseError{}, "", "")
_, ok := lo.ErrorsAs[*BaseError](err)
Expect(ok).To(BeTrue())
})
})

type BaseError struct {
error
}
20 changes: 20 additions & 0 deletions pkg/cloudprovider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ func (e *NodeClaimNotFoundError) Error() string {
return fmt.Sprintf("nodeclaim not found, %s", e.error)
}

func (e *NodeClaimNotFoundError) Unwrap() error {
return e.error
}

func IsNodeClaimNotFoundError(err error) bool {
if err == nil {
return false
Expand Down Expand Up @@ -356,6 +360,10 @@ func (e *InsufficientCapacityError) Error() string {
return fmt.Sprintf("insufficient capacity, %s", e.error)
}

func (e *InsufficientCapacityError) Unwrap() error {
return e.error
}

func IsInsufficientCapacityError(err error) bool {
if err == nil {
return false
Expand All @@ -379,6 +387,10 @@ func (e *NodeClassNotReadyError) Error() string {
return fmt.Sprintf("NodeClassRef not ready, %s", e.error)
}

func (e *NodeClassNotReadyError) Unwrap() error {
return e.error
}

func IsNodeClassNotReadyError(err error) bool {
if err == nil {
return false
Expand All @@ -401,3 +413,11 @@ func NewCreateError(err error, reason, message string) *CreateError {
ConditionMessage: message,
}
}

func (e *CreateError) Error() string {
return fmt.Sprintf("creating nodeclaim, %s", e.error)
}

func (e *CreateError) Unwrap() error {
return e.error
}

0 comments on commit 8e8b99d

Please sign in to comment.