From 6a67183ed7265e7c2233b0c891135a8ea592957b Mon Sep 17 00:00:00 2001 From: fanzhangio Date: Wed, 20 Jun 2018 19:19:32 -0700 Subject: [PATCH] Enhancement for format error message - add yaml format error handler - silent usage when build command fails --- pkg/commands/build.go | 1 + pkg/internal/error/yamlformaterror.go | 45 +++++++++++++++ pkg/internal/error/yamlformaterror_test.go | 67 ++++++++++++++++++++++ pkg/resmap/resmap.go | 5 +- 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 pkg/internal/error/yamlformaterror.go create mode 100644 pkg/internal/error/yamlformaterror_test.go diff --git a/pkg/commands/build.go b/pkg/commands/build.go index 4e76b9e98..8a40e2d42 100644 --- a/pkg/commands/build.go +++ b/pkg/commands/build.go @@ -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 { diff --git a/pkg/internal/error/yamlformaterror.go b/pkg/internal/error/yamlformaterror.go new file mode 100644 index 000000000..0db431448 --- /dev/null +++ b/pkg/internal/error/yamlformaterror.go @@ -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 +} diff --git a/pkg/internal/error/yamlformaterror_test.go b/pkg/internal/error/yamlformaterror_test.go new file mode 100644 index 000000000..625826f2a --- /dev/null +++ b/pkg/internal/error/yamlformaterror_test.go @@ -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{} diff --git a/pkg/resmap/resmap.go b/pkg/resmap/resmap.go index 81c597a2f..9af59fc94 100644 --- a/pkg/resmap/resmap.go +++ b/pkg/resmap/resmap.go @@ -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) }