Merge pull request #129 from fanzhangio/issue114

Enhancement for format error message
This commit is contained in:
Jingfang Liu
2018-06-25 10:33:36 -07:00
committed by GitHub
4 changed files with 116 additions and 2 deletions

View File

@@ -43,6 +43,7 @@ func newCmdBuild(out io.Writer, fs fs.FileSystem) *cobra.Command {
Short: "Print current configuration per contents of " + constants.KustomizationFileName,
Example: "Use the file somedir/" + constants.KustomizationFileName +
" to generate a set of api resources:\nbuild somedir/",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
err := o.Validate(args)
if err != nil {

View File

@@ -0,0 +1,45 @@
/*
Copyright 2018 The Kubernetes 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 error has contextual error types.
package error
import (
"fmt"
yaml "k8s.io/apimachinery/pkg/util/yaml"
)
// YamlFormatError represents error with yaml file name where json/yaml format error happens.
type YamlFormatError struct {
Path string
ErrorMsg string
}
func (e YamlFormatError) Error() string {
return fmt.Sprintf("YAML file [%s] encounters a format error.\n%s\n", e.Path, e.ErrorMsg)
}
// ErrorHandler handles YamlFormatError
func ErrorHandler(e error, path string) error {
if err, ok := e.(yaml.YAMLSyntaxError); ok {
return YamlFormatError{
Path: path,
ErrorMsg: err.Error(),
}
}
return e
}

View File

@@ -0,0 +1,67 @@
/*
Copyright 2018 The Kubernetes 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 error
import (
"bytes"
"fmt"
"testing"
"github.com/kubernetes-sigs/kustomize/pkg/constants"
yaml "k8s.io/apimachinery/pkg/util/yaml"
)
var (
filepath = "/path/to/" + constants.KustomizationFileName
expected = "YAML file [/path/to/kustomization.yaml] encounters a format error.\n" +
"error converting YAML to JSON: yaml: line 2: found character that cannot start any token\n"
doc = `
foo:
- fiz
- fu
`
)
func TestYamlFormatError_Error(t *testing.T) {
testErr := YamlFormatError{
Path: filepath,
ErrorMsg: "error converting YAML to JSON: yaml: line 2: found character that cannot start any token",
}
if testErr.Error() != expected {
t.Errorf("Expected : %s\n, but found : %s\n", expected, testErr.Error())
}
}
func TestErrorHandler(t *testing.T) {
f := foo{}
err := yaml.NewYAMLToJSONDecoder(bytes.NewReader([]byte(doc))).Decode(&f)
testErr := ErrorHandler(err, filepath)
expectedErr := fmt.Errorf("Format error message")
fmtErr := ErrorHandler(expectedErr, filepath)
if fmtErr.Error() != expectedErr.Error() {
t.Errorf("Expected returning fmt.Error, but found %T", fmtErr)
}
if _, ok := testErr.(YamlFormatError); !ok {
t.Errorf("Expected returning YamlFormatError, but found %T", testErr)
}
if testErr == nil || testErr.Error() != expected {
t.Errorf("Expected : %s\n, but found : %s\n", expected, testErr.Error())
}
}
//type foo struct
type foo struct{}

View File

@@ -26,6 +26,7 @@ import (
"github.com/ghodss/yaml"
"github.com/golang/glog"
internal "github.com/kubernetes-sigs/kustomize/pkg/internal/error"
"github.com/kubernetes-sigs/kustomize/pkg/loader"
"github.com/kubernetes-sigs/kustomize/pkg/resource"
"github.com/pkg/errors"
@@ -119,7 +120,7 @@ func NewResourceSliceFromPatches(
res, err := newResourceSliceFromBytes(content)
if err != nil {
return nil, err
return nil, internal.ErrorHandler(err, path)
}
result = append(result, res...)
}
@@ -136,7 +137,7 @@ func NewResMapFromFiles(loader loader.Loader, paths []string) (ResMap, error) {
}
res, err := newResMapFromBytes(content)
if err != nil {
return nil, err
return nil, internal.ErrorHandler(err, path)
}
result = append(result, res)
}