Skip to content

Commit 5b67b58

Browse files
authored
Merge pull request #129 from fanzhangio/issue114
Enhancement for format error message
2 parents a38befd + 6a67183 commit 5b67b58

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.\n" +
31+
"error converting YAML to JSON: yaml: line 2: found character that cannot start any token\n"
32+
doc = `
33+
foo:
34+
- fiz
35+
- fu
36+
`
37+
)
38+
39+
func TestYamlFormatError_Error(t *testing.T) {
40+
testErr := YamlFormatError{
41+
Path: filepath,
42+
ErrorMsg: "error converting YAML to JSON: yaml: line 2: found character that cannot start any token",
43+
}
44+
if testErr.Error() != expected {
45+
t.Errorf("Expected : %s\n, but found : %s\n", expected, testErr.Error())
46+
}
47+
}
48+
49+
func TestErrorHandler(t *testing.T) {
50+
f := foo{}
51+
err := yaml.NewYAMLToJSONDecoder(bytes.NewReader([]byte(doc))).Decode(&f)
52+
testErr := ErrorHandler(err, filepath)
53+
expectedErr := fmt.Errorf("Format error message")
54+
fmtErr := ErrorHandler(expectedErr, filepath)
55+
if fmtErr.Error() != expectedErr.Error() {
56+
t.Errorf("Expected returning fmt.Error, but found %T", fmtErr)
57+
}
58+
if _, ok := testErr.(YamlFormatError); !ok {
59+
t.Errorf("Expected returning YamlFormatError, but found %T", testErr)
60+
}
61+
if testErr == nil || testErr.Error() != expected {
62+
t.Errorf("Expected : %s\n, but found : %s\n", expected, testErr.Error())
63+
}
64+
}
65+
66+
//type foo struct
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)