Skip to content

Commit f1a9bf4

Browse files
author
fanzhangio
committed
Enhancement for format error message
- add yaml format error handler - silent usage when build command fails
1 parent dec5109 commit f1a9bf4

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-2
lines changed

pkg/commands/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func newCmdBuild(out io.Writer, fs fs.FileSystem) *cobra.Command {
4343
Short: "Print current configuration per contents of " + constants.KustomizationFileName,
4444
Example: "Use the file somedir/" + constants.KustomizationFileName +
4545
" to generate a set of api resources:\nbuild somedir/",
46+
SilenceUsage: true,
4647
RunE: func(cmd *cobra.Command, args []string) error {
4748
err := o.Validate(args)
4849
if err != nil {

pkg/internal/error/yamlformaterror.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package error has contextual error types.
18+
package error
19+
20+
import (
21+
"fmt"
22+
23+
yaml "k8s.io/apimachinery/pkg/util/yaml"
24+
)
25+
26+
// YamlFormatError represents error with yaml file name where json/yaml format error happens.
27+
type YamlFormatError struct {
28+
Path string
29+
ErrorMsg string
30+
}
31+
32+
func (e YamlFormatError) Error() string {
33+
return fmt.Sprintf("YAML file [%s] encounters a format error.\n%s\n", e.Path, e.ErrorMsg)
34+
}
35+
36+
// ErrorHandler handles YamlFormatError
37+
func ErrorHandler(e error, path string) error {
38+
if err, ok := e.(yaml.YAMLSyntaxError); ok {
39+
return YamlFormatError{
40+
Path: path,
41+
ErrorMsg: err.Error(),
42+
}
43+
}
44+
return e
45+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package error
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/kubernetes-sigs/kustomize/pkg/constants"
25+
yaml "k8s.io/apimachinery/pkg/util/yaml"
26+
)
27+
28+
var (
29+
filepath = "/path/to/" + constants.KustomizationFileName
30+
expected = "YAML file [/path/to/kustomization.yaml] encounters a format error.\nerror converting YAML to JSON: yaml: line 2: found character that cannot start any token\n"
31+
doc = `
32+
foo:
33+
- fiz
34+
- fu
35+
`
36+
)
37+
38+
//type foo struct
39+
40+
func TestYamlFormatError_Error(t *testing.T) {
41+
testErr := YamlFormatError{
42+
Path: filepath,
43+
ErrorMsg: "error converting YAML to JSON: yaml: line 2: found character that cannot start any token",
44+
}
45+
if testErr.Error() != expected {
46+
t.Errorf("Expected : %s\n, but found : %s\n", expected, testErr.Error())
47+
}
48+
}
49+
50+
func TestErrorHandler(t *testing.T) {
51+
f := foo{}
52+
err := yaml.NewYAMLToJSONDecoder(bytes.NewReader([]byte(doc))).Decode(&f)
53+
testErr := ErrorHandler(err, filepath)
54+
expectedErr := fmt.Errorf("Format error message")
55+
fmtErr := ErrorHandler(expectedErr, filepath)
56+
if fmtErr.Error() != expectedErr.Error() {
57+
t.Errorf("Expected returning fmt.Error, but found %T", fmtErr)
58+
}
59+
if _, ok := testErr.(YamlFormatError); !ok {
60+
t.Errorf("Expected returning YamlFormatError, but found %T", testErr)
61+
}
62+
if testErr == nil || testErr.Error() != expected {
63+
t.Errorf("Expected : %s\n, but found : %s\n", expected, testErr.Error())
64+
}
65+
}
66+
67+
type foo struct{}

pkg/resmap/resmap.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/ghodss/yaml"
2828
"github.com/golang/glog"
29+
internal "github.com/kubernetes-sigs/kustomize/pkg/internal/error"
2930
"github.com/kubernetes-sigs/kustomize/pkg/loader"
3031
"github.com/kubernetes-sigs/kustomize/pkg/resource"
3132
"github.com/pkg/errors"
@@ -119,7 +120,7 @@ func NewResourceSliceFromPatches(
119120

120121
res, err := newResourceSliceFromBytes(content)
121122
if err != nil {
122-
return nil, err
123+
return nil, internal.ErrorHandler(err, path)
123124
}
124125
result = append(result, res...)
125126
}
@@ -136,7 +137,7 @@ func NewResMapFromFiles(loader loader.Loader, paths []string) (ResMap, error) {
136137
}
137138
res, err := newResMapFromBytes(content)
138139
if err != nil {
139-
return nil, err
140+
return nil, internal.ErrorHandler(err, path)
140141
}
141142
result = append(result, res)
142143
}

0 commit comments

Comments
 (0)