Skip to content

Commit 936686b

Browse files
committed
rewrite error handling in a more idiomatic manner
Signed-off-by: Hiroki Hanada <hiroki-hanada@cybozu.co.jp>
1 parent f6aebcb commit 936686b

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

v2/api/v2/addresspool_webhook.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var _ webhook.CustomDefaulter = &AddressPoolCustomDefaulter{}
3636
func (r *AddressPoolCustomDefaulter) Default(ctx context.Context, obj runtime.Object) error {
3737
addressPool, ok := obj.(*AddressPool)
3838
if !ok {
39-
return fmt.Errorf("expected an AddressPool object but got %T", obj)
39+
return fmt.Errorf("expected an AddressPool object but got a %T", obj)
4040
}
4141

4242
controllerutil.AddFinalizer(addressPool, constants.FinCoil)
@@ -54,28 +54,28 @@ var _ webhook.CustomValidator = &AddressPoolCustomValidator{}
5454
func (r *AddressPoolCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
5555
addressPool, ok := obj.(*AddressPool)
5656
if !ok {
57-
return nil, fmt.Errorf("expected an AddressPool object but got %T", obj)
57+
return nil, fmt.Errorf("expected an AddressPool object but got a %T", obj)
5858
}
59-
errs := addressPool.Spec.validate()
60-
if len(errs) == 0 {
61-
return nil, nil
59+
60+
if errs := addressPool.Spec.validate(); len(errs) != 0 {
61+
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "AddressPool"}, addressPool.Name, errs)
6262
}
6363

64-
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "AddressPool"}, addressPool.Name, errs)
64+
return nil, nil
6565
}
6666

6767
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
6868
func (r *AddressPoolCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
6969
addressPool, ok := newObj.(*AddressPool)
7070
if !ok {
71-
return nil, fmt.Errorf("expected an AddressPool object but got %T", newObj)
71+
return nil, fmt.Errorf("expected an AddressPool object but got a %T", newObj)
7272
}
73-
errs := addressPool.Spec.validateUpdate(oldObj.(*AddressPool).Spec)
74-
if len(errs) == 0 {
75-
return nil, nil
73+
74+
if errs := addressPool.Spec.validateUpdate(oldObj.(*AddressPool).Spec); len(errs) != 0 {
75+
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "AddressPool"}, addressPool.Name, errs)
7676
}
7777

78-
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "AddressPool"}, addressPool.Name, errs)
78+
return nil, nil
7979
}
8080

8181
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type

v2/api/v2/egress_webhook.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var _ webhook.CustomDefaulter = &EgressCustomDefaulter{}
3535
func (r *EgressCustomDefaulter) Default(ctx context.Context, obj runtime.Object) error {
3636
egress, ok := obj.(*Egress)
3737
if !ok {
38-
return fmt.Errorf("expected an Egress object but got %T", obj)
38+
return fmt.Errorf("expected an Egress object but got a %T", obj)
3939
}
4040

4141
tmpl := egress.Spec.Template
@@ -64,30 +64,28 @@ var _ webhook.CustomValidator = &EgressCustomValidator{}
6464
func (r *EgressCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
6565
egress, ok := obj.(*Egress)
6666
if !ok {
67-
return nil, fmt.Errorf("expected an Egress object but got %T", obj)
67+
return nil, fmt.Errorf("expected an Egress object but got a %T", obj)
6868
}
6969

70-
errs := egress.Spec.validate()
71-
if len(errs) == 0 {
72-
return nil, nil
70+
if errs := egress.Spec.validate(); len(errs) != 0 {
71+
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Egress"}, egress.Name, errs)
7372
}
7473

75-
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Egress"}, egress.Name, errs)
74+
return nil, nil
7675
}
7776

7877
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
7978
func (r *EgressCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
8079
egress, ok := newObj.(*Egress)
8180
if !ok {
82-
return nil, fmt.Errorf("expected an Egress object but got %T", newObj)
81+
return nil, fmt.Errorf("expected an Egress object but got a %T", newObj)
8382
}
8483

85-
errs := egress.Spec.validateUpdate()
86-
if len(errs) == 0 {
87-
return nil, nil
84+
if errs := egress.Spec.validateUpdate(); len(errs) != 0 {
85+
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Egress"}, egress.Name, errs)
8886
}
8987

90-
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Egress"}, egress.Name, errs)
88+
return nil, nil
9189
}
9290

9391
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type

0 commit comments

Comments
 (0)