Unverified Commit 01f298ef authored by YangKeao's avatar YangKeao Committed by GitHub
Browse files

Use `github.com/pkg/errors` to replace `fmt.Errorf` and `"errors"` (#2780)


* turn to use pkg/errors rather than fmt or errors
Signed-off-by: default avatarYangKeao <yangkeao@chunibyo.icu>

* use wrap to wrap the error
Signed-off-by: default avatarYangKeao <yangkeao@chunibyo.icu>

* fix CI and test
Signed-off-by: default avatarYangKeao <yangkeao@chunibyo.icu>

* restore yarn.lock
Signed-off-by: default avatarYangKeao <yangkeao@chunibyo.icu>

* replace "is empty" to "is required" in webhook message
Signed-off-by: default avatarYangKeao <yangkeao@chunibyo.icu>

* make check!
Signed-off-by: default avatarYangKeao <yangkeao@chunibyo.icu>

* use container with id
Signed-off-by: default avatarYangKeao <yangkeao@chunibyo.icu>

* replace errors.Errorf
Signed-off-by: default avatarYangKeao <yangkeao@chunibyo.icu>
Co-authored-by: default avatarSTRRL <str_ruiling@outlook.com>
parent 214c098f
Showing with 102 additions and 41 deletions
+102 -41
......@@ -50,6 +50,7 @@ header:
- '**/Dockerfile'
# UI
- 'ui/.husky/pre-commit'
- 'ui/.husky/_/husky.sh'
- 'ui/.prettierignore'
- 'ui/.prettierrc'
- 'ui/.yarnclean'
......@@ -57,5 +58,7 @@ header:
- 'ui/app/src/setupProxy.js'
- 'ui/**/*.svg'
- 'ui/node_modules'
- 'ui/app/node_modules'
- 'ui/packages/mui-extends/node_modules'
# codecov configuration file
- codecov.yml
......@@ -16,11 +16,12 @@
package v1alpha1
import (
"fmt"
"reflect"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/pkg/errors"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1/genericwebhook"
)
......@@ -33,7 +34,7 @@ func (in *EbsVolume) Validate(root interface{}, path *field.Path) field.ErrorLis
awsChaos := root.(*AWSChaos)
if awsChaos.Spec.Action == DetachVolume {
if in == nil {
err := fmt.Errorf("the ID of EBS volume should not be empty on %s action", awsChaos.Spec.Action)
err := errors.Wrapf(errInvalidValue, "the ID of EBS volume is required on %s action", awsChaos.Spec.Action)
allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
}
}
......@@ -47,7 +48,7 @@ func (in *AWSDeviceName) Validate(root interface{}, path *field.Path) field.Erro
awsChaos := root.(*AWSChaos)
if awsChaos.Spec.Action == DetachVolume {
if in == nil {
err := fmt.Errorf("the name of device should not be empty on %s action", awsChaos.Spec.Action)
err := errors.Wrapf(errInvalidValue, "the name of device is required on %s action", awsChaos.Spec.Action)
allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
}
}
......@@ -64,7 +65,7 @@ func (in *AWSChaosAction) Validate(root interface{}, path *field.Path) field.Err
case Ec2Stop, DetachVolume:
case Ec2Restart:
default:
err := fmt.Errorf("awschaos have unknown action type")
err := errors.WithStack(errUnknownAction)
log.Error(err, "Wrong AWSChaos Action type")
allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
......
// Copyright 2022 Chaos Mesh 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 v1alpha1
import (
"github.com/pkg/errors"
)
var (
errUnknownAction = errors.New("unknown action")
errInvalidValue = errors.New("invalid value")
)
......@@ -16,9 +16,10 @@
package v1alpha1
import (
"fmt"
"reflect"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1/genericwebhook"
......@@ -32,7 +33,7 @@ func (in GCPChaosAction) Validate(root interface{}, path *field.Path) field.Erro
case NodeStop, DiskLoss:
case NodeReset:
default:
err := fmt.Errorf("gcpchaos have unknown action type")
err := errors.WithStack(errUnknownAction)
log.Error(err, "Wrong GCPChaos Action type")
allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
......@@ -48,7 +49,7 @@ func (in *GCPDeviceNames) Validate(root interface{}, path *field.Path) field.Err
obj := root.(*GCPChaos)
if obj.Spec.Action == DiskLoss {
if *in == nil {
err := fmt.Errorf("at least one device name is required on %s action", obj.Spec.Action)
err := errors.Errorf("at least one device name is required on %s action", obj.Spec.Action)
allErrs = append(allErrs, field.Invalid(path, *in, err.Error()))
}
}
......
......@@ -16,10 +16,11 @@
package genericwebhook
import (
"fmt"
"reflect"
"strings"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
)
......@@ -67,11 +68,11 @@ func Validate(obj interface{}) field.ErrorList {
return errorList
}
func Aggregate(errors field.ErrorList) error {
if errors == nil || len(errors) == 0 {
func Aggregate(errs field.ErrorList) error {
if errs == nil || len(errs) == 0 {
return nil
}
return fmt.Errorf(errors.ToAggregate().Error())
return errors.New(errs.ToAggregate().Error())
}
func getValidator(obj interface{}, webhook string, nilable bool) FieldValidator {
......
......@@ -16,12 +16,13 @@
package v1alpha1
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1/genericwebhook"
......
......@@ -67,7 +67,7 @@ func (in *PhysicalMachineChaosSpec) Validate(root interface{}, path *field.Path)
skipConfigCheck = true
allErrs = append(allErrs,
field.Invalid(path.Child("spec"), in,
"the configuration corresponding to action is empty"))
"the configuration corresponding to action is required"))
}
if len(in.Address) == 0 && in.Selector.Empty() {
......@@ -82,7 +82,7 @@ func (in *PhysicalMachineChaosSpec) Validate(root interface{}, path *field.Path)
for _, address := range in.Address {
if len(address) == 0 {
allErrs = append(allErrs,
field.Invalid(path.Child("address"), in.Address, "the address is empty"))
field.Invalid(path.Child("address"), in.Address, "the address is required"))
}
}
......@@ -274,7 +274,7 @@ func validateNetworkPartitionAction(spec *NetworkPartitionSpec) error {
}
if len(spec.AcceptTCPFlags) > 0 && spec.IPProtocol != "tcp" {
return errors.Errorf("protocol should be 'tcp' when set accept-tcp-flags")
return errors.New("protocol should be 'tcp' when set accept-tcp-flags")
}
return nil
......@@ -462,7 +462,7 @@ func ParseUnit(s string) (uint64, error) {
if n, err := units.ParseUnit(s, decimalUnitMap); err == nil {
return uint64(n), nil
}
return 0, fmt.Errorf("units: unknown unit %s", s)
return 0, errors.Wrapf(errInvalidValue, "unknown unit %s", s)
}
func (in *NetworkBandwidthSpec) Validate(root interface{}, path *field.Path) field.ErrorList {
......
......@@ -70,7 +70,7 @@ var _ = Describe("physicalmachinechaos_webhook", func() {
ExpInfo: ExpInfo{},
},
},
"the configuration corresponding to action is empty",
"the configuration corresponding to action is required",
},
}
......
......@@ -16,8 +16,7 @@
package v1alpha1
import (
"fmt"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
)
......@@ -26,7 +25,7 @@ func (in *PodChaosSpec) Validate(root interface{}, path *field.Path) field.Error
allErrs := field.ErrorList{}
if in.Action == ContainerKillAction {
if len(in.ContainerSelector.ContainerNames) == 0 {
err := fmt.Errorf("the name of container should not be empty on %s action", in.Action)
err := errors.Wrapf(errInvalidValue, "the name of container is required on %s action", in.Action)
allErrs = append(allErrs, field.Invalid(path.Child("containerNames"), in.ContainerNames, err.Error()))
}
}
......
......@@ -18,6 +18,7 @@ package v1alpha1
import (
"fmt"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
logf "sigs.k8s.io/controller-runtime/pkg/log"
......@@ -62,7 +63,7 @@ func (in *Schedule) ValidateDelete() error {
func (in *Schedule) Validate() error {
allErrs := in.Spec.Validate()
if len(allErrs) > 0 {
return fmt.Errorf(allErrs.ToAggregate().Error())
return errors.New(allErrs.ToAggregate().Error())
}
return nil
}
......
......@@ -16,11 +16,12 @@
package v1alpha1
import (
"errors"
"fmt"
"reflect"
"strconv"
"github.com/pkg/errors"
"github.com/docker/go-units"
"k8s.io/apimachinery/pkg/util/validation/field"
......
......@@ -16,8 +16,7 @@
package v1alpha1
import (
"fmt"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
......@@ -189,5 +188,5 @@ func FetchChaosByTemplateType(templateType TemplateType) (runtime.Object, error)
if kind, ok := all.kinds[string(templateType)]; ok {
return kind.SpawnObject(), nil
}
return nil, fmt.Errorf("no such kind refers to template type %s", templateType)
return nil, errors.Wrapf(errInvalidValue, "unknown template type %s", templateType)
}
......@@ -20,6 +20,7 @@ import (
"reflect"
"sort"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
......@@ -38,7 +39,7 @@ func (in *Workflow) ValidateCreate() error {
allErrs = append(allErrs, entryMustExists(specPath.Child("entry"), in.Spec.Entry, in.Spec.Templates)...)
allErrs = append(allErrs, validateTemplates(specPath.Child("templates"), in.Spec.Templates)...)
if len(allErrs) > 0 {
return fmt.Errorf(allErrs.ToAggregate().Error())
return errors.New(allErrs.ToAggregate().Error())
}
return nil
}
......
......@@ -21,7 +21,8 @@ import (
"encoding/json"
"reflect"
"time"
"fmt"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/webhook"
......@@ -32,7 +33,7 @@ import (
)
// updating spec of a chaos will have no effect, we'd better reject it
var ErrCanNotUpdateChaos = fmt.Errorf("Cannot update chaos spec")
var ErrCanNotUpdateChaos = errors.New("Cannot update chaos spec")
const KindAWSChaos = "AWSChaos"
......
......@@ -19,7 +19,7 @@ package v1alpha1
import (
"fmt"
"github.com/pkg/errors"
)
......@@ -113,7 +113,7 @@ func (it *ScheduleItem) SpawnNewObject(templateType ScheduleTemplateType) (Gener
return &result, nil
default:
return nil, fmt.Errorf("unsupported template type %s", templateType)
return nil, errors.Wrapf(errInvalidValue, "unknown template type %s", templateType)
}
}
......@@ -160,6 +160,6 @@ func (it *ScheduleItem) RestoreChaosSpec(root interface{}) error {
return nil
default:
return fmt.Errorf("unsupported chaos %#v", root)
return errors.Wrapf(errInvalidValue, "unknown chaos %#v", root)
}
}
......@@ -19,7 +19,7 @@ package v1alpha1
import (
"fmt"
"github.com/pkg/errors"
)
......@@ -136,7 +136,7 @@ func (it *EmbedChaos) SpawnNewObject(templateType TemplateType) (GenericChaos, e
return &result, nil
default:
return nil, fmt.Errorf("unsupported template type %s", templateType)
return nil, errors.Wrapf(errInvalidValue, "unknown template type %s", templateType)
}
}
......@@ -180,7 +180,7 @@ func (it *EmbedChaos) RestoreChaosSpec(root interface{}) error {
return nil
default:
return fmt.Errorf("unsupported chaos %#v", root)
return errors.Wrapf(errInvalidValue, "unknown chaos %#v", root)
}
}
......@@ -224,7 +224,7 @@ func (it *EmbedChaos) SpawnNewList(templateType TemplateType) (GenericChaosList,
return &result, nil
default:
return nil, fmt.Errorf("unsupported template type %s", templateType)
return nil, errors.Wrapf(errInvalidValue, "unknown template type %s", templateType)
}
}
......
// Copyright 2021 Chaos Mesh 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 webhook
import (
"github.com/pkg/errors"
)
var (
errInvalidValue error = errors.New("invalid value")
)
......@@ -27,6 +27,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/pkg/errors"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
)
......@@ -85,7 +87,7 @@ func (v *AuthValidator) Handle(ctx context.Context, req admission.Request) admis
kind, ok := v1alpha1.AllKindsIncludeScheduleAndWorkflow()[requestKind]
if !ok {
err := fmt.Errorf("kind %s is not support", requestKind)
err := errors.Wrapf(errInvalidValue, "kind %s is not support", requestKind)
return admission.Errored(http.StatusBadRequest, err)
}
chaos := kind.SpawnObject()
......
......@@ -25,7 +25,8 @@ import (
"encoding/json"
"reflect"
"time"
"fmt"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/webhook"
......@@ -36,7 +37,7 @@ import (
)
// updating spec of a chaos will have no effect, we'd better reject it
var ErrCanNotUpdateChaos = fmt.Errorf("Cannot update chaos spec")
var ErrCanNotUpdateChaos = errors.New("Cannot update chaos spec")
`
const implTemplate = `
......
......@@ -194,20 +194,20 @@ func getType(fset *token.FileSet, node ast.Node, comment *ast.Comment) (*ast.Typ
log.Info("build", "pos", fset.Position(comment.Pos()))
decl, ok := node.(*ast.GenDecl)
if !ok {
err := errors.Errorf("node is not a *ast.GenDecl")
err := errors.New("node is not a *ast.GenDecl")
log.Error(err, "fail to get type")
return nil, err
}
if decl.Tok != token.TYPE {
err := errors.Errorf("node.Tok is not token.TYPE")
err := errors.New("node.Tok is not token.TYPE")
log.Error(err, "fail to get type")
return nil, err
}
baseType, ok := decl.Specs[0].(*ast.TypeSpec)
if !ok {
err := errors.Errorf("node is not a *ast.TypeSpec")
err := errors.New("node is not a *ast.TypeSpec")
log.Error(err, "fail to get type")
return nil, err
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment