mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 18:10:59 +00:00
Improve error message when namespace transformer is given invalid fieldspecs
Also remove invalid+ignored fieldspecs from the defaults
This commit is contained in:
@@ -31,6 +31,16 @@ func Errorf(msg string, args ...interface{}) error {
|
||||
return goerrors.Wrap(fmt.Errorf(msg, args...), 1)
|
||||
}
|
||||
|
||||
// As finds the targeted error in any wrapped error.
|
||||
func As(err error, target interface{}) bool {
|
||||
return goerrors.As(err, target)
|
||||
}
|
||||
|
||||
// Is detects whether the error is equal to a given error.
|
||||
func Is(err error, target error) bool {
|
||||
return goerrors.Is(err, target)
|
||||
}
|
||||
|
||||
// GetStack returns a stack trace for the error if it has one
|
||||
func GetStack(err error) string {
|
||||
if e, ok := err.(*goerrors.Error); ok {
|
||||
|
||||
@@ -491,7 +491,9 @@ another`),
|
||||
wantErr: `failed to parse rendered patch template into a resource:
|
||||
001 aString
|
||||
002 another
|
||||
: wrong Node Kind for expected: MappingNode was ScalarNode: value: {aString another}`,
|
||||
: wrong node kind: expected MappingNode but got ScalarNode: node contents:
|
||||
aString another
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "ResourcePatchTemplate is invalid template",
|
||||
@@ -515,7 +517,9 @@ another`),
|
||||
wantErr: `failed to parse rendered patch template into a resource:
|
||||
001 aString
|
||||
002 another
|
||||
: wrong Node Kind for expected: MappingNode was ScalarNode: value: {aString another}`,
|
||||
: wrong node kind: expected MappingNode but got ScalarNode: node contents:
|
||||
aString another
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "ContainerPatchTemplate is invalid template",
|
||||
@@ -538,7 +542,9 @@ another`),
|
||||
wantErr: `failed to parse rendered template into a resource:
|
||||
001 aString
|
||||
002 another
|
||||
: wrong Node Kind for expected: MappingNode was ScalarNode: value: {aString another}`,
|
||||
: wrong node kind: expected MappingNode but got ScalarNode: node contents:
|
||||
aString another
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "ResourceTemplate is invalid template",
|
||||
|
||||
@@ -4,7 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/go-errors/errors v1.0.1
|
||||
github.com/go-errors/errors v1.4.2
|
||||
github.com/google/gnostic v0.5.7-v3refs
|
||||
github.com/google/go-cmp v0.5.5
|
||||
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00
|
||||
|
||||
@@ -663,7 +663,7 @@ func TestByteReadWriter_WrapBareSeqNode(t *testing.T) {
|
||||
- foo
|
||||
- bar
|
||||
`,
|
||||
readerErr: "wrong Node Kind for expected: MappingNode was SequenceNode",
|
||||
readerErr: "wrong node kind: expected MappingNode but got SequenceNode",
|
||||
},
|
||||
{
|
||||
name: "error round_trip bare seq node",
|
||||
@@ -686,7 +686,7 @@ func TestByteReadWriter_WrapBareSeqNode(t *testing.T) {
|
||||
served: true
|
||||
storage: false
|
||||
`,
|
||||
readerErr: "wrong Node Kind for expected: MappingNode was SequenceNode",
|
||||
readerErr: "wrong node kind: expected MappingNode but got SequenceNode",
|
||||
},
|
||||
{
|
||||
name: "round_trip bare seq node json",
|
||||
@@ -698,7 +698,7 @@ func TestByteReadWriter_WrapBareSeqNode(t *testing.T) {
|
||||
name: "error round_trip invalid yaml node",
|
||||
wrapBareSeqNode: false,
|
||||
input: "I am not valid",
|
||||
readerErr: "wrong Node Kind for expected: MappingNode was ScalarNode",
|
||||
readerErr: "wrong node kind: expected MappingNode but got ScalarNode",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -87,6 +87,16 @@ var MappingNode yaml.Kind = yaml.MappingNode
|
||||
var ScalarNode yaml.Kind = yaml.ScalarNode
|
||||
var SequenceNode yaml.Kind = yaml.SequenceNode
|
||||
|
||||
func nodeKindString(k yaml.Kind) string {
|
||||
return map[yaml.Kind]string{
|
||||
yaml.SequenceNode: "SequenceNode",
|
||||
yaml.MappingNode: "MappingNode",
|
||||
yaml.ScalarNode: "ScalarNode",
|
||||
yaml.DocumentNode: "DocumentNode",
|
||||
yaml.AliasNode: "AliasNode",
|
||||
}[k]
|
||||
}
|
||||
|
||||
var DoubleQuotedStyle yaml.Style = yaml.DoubleQuotedStyle
|
||||
var FlowStyle yaml.Style = yaml.FlowStyle
|
||||
var FoldedStyle yaml.Style = yaml.FoldedStyle
|
||||
|
||||
@@ -794,12 +794,22 @@ func ErrorIfAnyInvalidAndNonNull(kind yaml.Kind, rn ...*RNode) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var nodeTypeIndex = map[yaml.Kind]string{
|
||||
yaml.SequenceNode: "SequenceNode",
|
||||
yaml.MappingNode: "MappingNode",
|
||||
yaml.ScalarNode: "ScalarNode",
|
||||
yaml.DocumentNode: "DocumentNode",
|
||||
yaml.AliasNode: "AliasNode",
|
||||
type InvalidNodeKindError struct {
|
||||
expectedKind yaml.Kind
|
||||
node *RNode
|
||||
}
|
||||
|
||||
func (e *InvalidNodeKindError) Error() string {
|
||||
msg := fmt.Sprintf("wrong node kind: expected %s but got %s",
|
||||
nodeKindString(e.expectedKind), nodeKindString(e.node.YNode().Kind))
|
||||
if content, err := e.node.String(); err == nil {
|
||||
msg += fmt.Sprintf(": node contents:\n%s", content)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func (e *InvalidNodeKindError) ActualNodeKind() Kind {
|
||||
return e.node.YNode().Kind
|
||||
}
|
||||
|
||||
func ErrorIfInvalid(rn *RNode, kind yaml.Kind) error {
|
||||
@@ -809,11 +819,7 @@ func ErrorIfInvalid(rn *RNode, kind yaml.Kind) error {
|
||||
}
|
||||
|
||||
if rn.YNode().Kind != kind {
|
||||
s, _ := rn.String()
|
||||
return errors.Errorf(
|
||||
"wrong Node Kind for %s expected: %v was %v: value: {%s}",
|
||||
strings.Join(rn.FieldPath(), "."),
|
||||
nodeTypeIndex[kind], nodeTypeIndex[rn.YNode().Kind], strings.TrimSpace(s))
|
||||
return &InvalidNodeKindError{node: rn, expectedKind: kind}
|
||||
}
|
||||
|
||||
if kind == yaml.MappingNode {
|
||||
|
||||
@@ -45,7 +45,7 @@ func TestAppend(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
rn, err := node.Pipe(Append(NewScalarRNode("").YNode()))
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "wrong Node Kind")
|
||||
assert.Contains(t, err.Error(), "wrong node kind")
|
||||
}
|
||||
assert.Nil(t, rn)
|
||||
|
||||
@@ -159,7 +159,9 @@ func TestElementSetter(t *testing.T) {
|
||||
// Return error because ElementSetter will assume all elements are scalar when
|
||||
// there is only value provided.
|
||||
_, err = node.Pipe(ElementSetter{Values: []string{"b"}})
|
||||
assert.EqualError(t, err, "wrong Node Kind for expected: ScalarNode was MappingNode: value: {a: b}")
|
||||
assert.EqualError(t, err, `wrong node kind: expected ScalarNode but got MappingNode: node contents:
|
||||
a: b
|
||||
`)
|
||||
|
||||
node = MustParse(`
|
||||
- a
|
||||
@@ -455,7 +457,7 @@ a: b
|
||||
assert.NoError(t, err)
|
||||
rn, err = node.Pipe(FieldClearer{Name: "a"})
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "wrong Node Kind")
|
||||
assert.Contains(t, err.Error(), "wrong node kind")
|
||||
}
|
||||
assert.Nil(t, rn)
|
||||
assert.Equal(t, s, assertNoErrorString(t)(node.String()))
|
||||
@@ -855,7 +857,9 @@ func TestMapEntrySetter(t *testing.T) {
|
||||
Key: NewScalarRNode("foo"),
|
||||
Value: NewScalarRNode("bar"),
|
||||
},
|
||||
expectedErr: errors.Errorf("wrong Node Kind for expected: MappingNode was SequenceNode: value: {- foo: baz}"),
|
||||
expectedErr: errors.Errorf(`wrong node kind: expected MappingNode but got SequenceNode: node contents:
|
||||
- foo: baz
|
||||
`),
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
@@ -950,7 +954,7 @@ foo
|
||||
}
|
||||
k, err = instance.Filter(node)
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "wrong Node Kind")
|
||||
assert.Contains(t, err.Error(), "wrong node kind")
|
||||
}
|
||||
assert.Nil(t, k)
|
||||
}
|
||||
@@ -1009,7 +1013,7 @@ foo: baz
|
||||
if !assert.Error(t, err) {
|
||||
return
|
||||
}
|
||||
assert.Contains(t, err.Error(), "wrong Node Kind")
|
||||
assert.Contains(t, err.Error(), "wrong node kind")
|
||||
assert.Equal(t, `foo: baz
|
||||
`, assertNoErrorString(t)(node.String()))
|
||||
}
|
||||
@@ -1029,15 +1033,15 @@ func TestErrorIfInvalid(t *testing.T) {
|
||||
if !assert.Error(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Contains(t, err.Error(), "wrong Node Kind")
|
||||
assert.Contains(t, err.Error(), "wrong node kind")
|
||||
|
||||
err = ErrorIfInvalid(NewRNode(&yaml.Node{}), yaml.SequenceNode)
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "wrong Node Kind")
|
||||
assert.Contains(t, err.Error(), "wrong node kind")
|
||||
}
|
||||
err = ErrorIfInvalid(NewRNode(&yaml.Node{}), yaml.MappingNode)
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "wrong Node Kind")
|
||||
assert.Contains(t, err.Error(), "wrong node kind")
|
||||
}
|
||||
|
||||
err = ErrorIfInvalid(NewRNode(&yaml.Node{
|
||||
@@ -1048,7 +1052,7 @@ func TestErrorIfInvalid(t *testing.T) {
|
||||
|
||||
err = ErrorIfInvalid(NewRNode(&yaml.Node{}), yaml.SequenceNode)
|
||||
if assert.Error(t, err) {
|
||||
assert.Contains(t, err.Error(), "wrong Node Kind")
|
||||
assert.Contains(t, err.Error(), "wrong node kind")
|
||||
}
|
||||
|
||||
err = ErrorIfInvalid(NewRNode(&yaml.Node{
|
||||
|
||||
@@ -1372,7 +1372,9 @@ spec:
|
||||
{
|
||||
testName: "non mapping node error",
|
||||
input: `apiVersion`,
|
||||
err: "wrong Node Kind for expected: MappingNode was ScalarNode: value: {apiVersion}",
|
||||
err: `wrong node kind: expected MappingNode but got ScalarNode: node contents:
|
||||
apiVersion
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user