Skip to content

Commit a2702f2

Browse files
authored
Merge pull request vmware-tanzu#912 from TaoZou1/errortype
Add error type in NSXApiError
2 parents 07f4851 + c5a1331 commit a2702f2

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

pkg/nsx/services/nsxserviceaccount/cluster.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ func (s *NSXServiceAccountService) RestoreRealizedNSXServiceAccount(ctx context.
196196
if err == nil {
197197
return fmt.Errorf("CCP store is not synchronized")
198198
}
199-
switch err.(type) {
200-
case vapierrors.NotFound:
201-
default:
199+
if nsxErr, ok := err.(*nsxutil.NSXApiError); ok {
200+
if nsxErr.Type() != vapierrors.ErrorType_NOT_FOUND {
201+
return err
202+
}
203+
} else {
202204
return err
203205
}
204206

pkg/nsx/services/nsxserviceaccount/cluster_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
"github.com/stretchr/testify/require"
1818
vapierrors "github.com/vmware/vsphere-automation-sdk-go/lib/vapi/std/errors"
19+
"github.com/vmware/vsphere-automation-sdk-go/runtime/data"
1920
mpmodel "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
2021
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
2122
v1 "k8s.io/api/core/v1"
@@ -538,6 +539,8 @@ func TestNSXServiceAccountService_RestoreRealizedNSXServiceAccount(t *testing.T)
538539
type args struct {
539540
obj *v1alpha1.NSXServiceAccount
540541
}
542+
notFoundErr := vapierrors.NewNotFound()
543+
notFoundErr.Data = data.NewStructValue("test", nil)
541544
tests := []struct {
542545
name string
543546
prepareFunc func(*testing.T, *NSXServiceAccountService, context.Context, *v1alpha1.NSXServiceAccount) *gomonkey.Patches
@@ -764,7 +767,7 @@ func TestNSXServiceAccountService_RestoreRealizedNSXServiceAccount(t *testing.T)
764767
name: "GetSecretError",
765768
prepareFunc: func(t *testing.T, s *NSXServiceAccountService, ctx context.Context, obj *v1alpha1.NSXServiceAccount) *gomonkey.Patches {
766769
patches := gomonkey.ApplyMethodSeq(s.NSXClient.ClusterControlPlanesClient, "Get", []gomonkey.OutputCell{{
767-
Values: gomonkey.Params{model.ClusterControlPlane{}, vapierrors.NotFound{}},
770+
Values: gomonkey.Params{model.ClusterControlPlane{}, *notFoundErr},
768771
Times: 1,
769772
}})
770773
return patches
@@ -801,7 +804,7 @@ func TestNSXServiceAccountService_RestoreRealizedNSXServiceAccount(t *testing.T)
801804
piId := "Id1"
802805
uid := "00000000-0000-0000-0000-000000000001"
803806
patches := gomonkey.ApplyMethodSeq(s.NSXClient.ClusterControlPlanesClient, "Get", []gomonkey.OutputCell{{
804-
Values: gomonkey.Params{model.ClusterControlPlane{}, vapierrors.NotFound{}},
807+
Values: gomonkey.Params{model.ClusterControlPlane{}, *notFoundErr},
805808
Times: 1,
806809
}})
807810
secretName := obj.Status.Secrets[0].Name

pkg/nsx/util/utils.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,13 @@ func DumpHttpRequest(request *http.Request) {
327327

328328
type NSXApiError struct {
329329
*model.ApiError
330+
apierrors.ErrorTypeEnum
330331
}
331332

332-
func NewNSXApiError(apiError *model.ApiError) *NSXApiError {
333+
func NewNSXApiError(apiError *model.ApiError, errorType apierrors.ErrorTypeEnum) *NSXApiError {
333334
return &NSXApiError{
334-
ApiError: apiError,
335+
ApiError: apiError,
336+
ErrorTypeEnum: errorType,
335337
}
336338
}
337339

@@ -345,17 +347,21 @@ func (e *NSXApiError) Error() string {
345347
return "SDKError: unknown error"
346348
}
347349

350+
func (e *NSXApiError) Type() apierrors.ErrorTypeEnum {
351+
return e.ErrorTypeEnum
352+
}
353+
348354
// TransNSXApiError processes an error and returns a formatted NSX API error message if applicable.
349355
// If the processed API error is nil, return the original error
350356
func TransNSXApiError(err error) error {
351357
if err == nil {
352358
return err
353359
}
354-
apierror, _ := DumpAPIError(err)
360+
apierror, errorType := DumpAPIError(err)
355361
if apierror == nil {
356362
return err
357363
}
358-
return NewNSXApiError(apierror)
364+
return NewNSXApiError(apierror, *errorType)
359365
}
360366

361367
func relatedErrorToString(err *model.RelatedApiError) string {

pkg/nsx/util/utils_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"testing"
2121

2222
"github.com/stretchr/testify/assert"
23+
apierrors "github.com/vmware/vsphere-automation-sdk-go/lib/vapi/std/errors"
24+
"github.com/vmware/vsphere-automation-sdk-go/runtime/data"
2325
mpmodel "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
2426
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
2527
)
@@ -609,3 +611,47 @@ func TestMergeArraysWithoutDuplicate(t *testing.T) {
609611
})
610612
}
611613
}
614+
615+
func TestTransNSXApiError(t *testing.T) {
616+
tests := []struct {
617+
name string
618+
err error
619+
wantErr bool
620+
}{
621+
{
622+
name: "Nil error",
623+
err: nil,
624+
wantErr: false,
625+
},
626+
{
627+
name: "Valid NSX API error",
628+
err: apierrors.NewNotFound(),
629+
wantErr: true,
630+
},
631+
{
632+
name: "Unknown error type",
633+
err: errors.New("unknown error"),
634+
wantErr: true,
635+
},
636+
}
637+
638+
for _, tt := range tests {
639+
t.Run(tt.name, func(t *testing.T) {
640+
got := TransNSXApiError(tt.err)
641+
if (got != nil) != tt.wantErr {
642+
t.Errorf("TransNSXApiError() error = %v, wantErr %v", got, tt.wantErr)
643+
}
644+
})
645+
}
646+
647+
err := apierrors.NewNotFound()
648+
err.Data = data.NewStructValue("test", nil)
649+
t.Log(err)
650+
got := TransNSXApiError(*err)
651+
t.Log(got)
652+
gotErr, ok := got.(*NSXApiError)
653+
t.Log(gotErr)
654+
655+
assert.Equal(t, ok, true)
656+
assert.Equal(t, gotErr.Type(), apierrors.ErrorType_NOT_FOUND)
657+
}

0 commit comments

Comments
 (0)