From 08da2455dd2d045ea29cebdfca8fcf45769888ac Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Mon, 8 Oct 2018 14:12:00 -0700 Subject: [PATCH] remove apimachinery dependency from internal/error package --- pkg/internal/error/yamlformaterror.go | 11 +++++++---- pkg/internal/error/yamlformaterror_test.go | 13 +------------ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/pkg/internal/error/yamlformaterror.go b/pkg/internal/error/yamlformaterror.go index 5edc03d99..4c27d30d7 100644 --- a/pkg/internal/error/yamlformaterror.go +++ b/pkg/internal/error/yamlformaterror.go @@ -19,8 +19,7 @@ package error import ( "fmt" - - "k8s.io/apimachinery/pkg/util/yaml" + "strings" ) // YamlFormatError represents error with yaml file name where json/yaml format error happens. @@ -35,11 +34,15 @@ func (e YamlFormatError) Error() string { // Handler handles YamlFormatError func Handler(e error, path string) error { - if err, ok := e.(yaml.YAMLSyntaxError); ok { + if isYAMLSyntaxError(e) { return YamlFormatError{ Path: path, - ErrorMsg: err.Error(), + ErrorMsg: e.Error(), } } return e } + +func isYAMLSyntaxError(e error) bool { + return strings.Contains(e.Error(), "error converting YAML to JSON") || strings.Contains(e.Error(), "error unmarshaling JSON") +} diff --git a/pkg/internal/error/yamlformaterror_test.go b/pkg/internal/error/yamlformaterror_test.go index 68624bf00..ae8550354 100644 --- a/pkg/internal/error/yamlformaterror_test.go +++ b/pkg/internal/error/yamlformaterror_test.go @@ -17,11 +17,9 @@ limitations under the License. package error import ( - "bytes" "fmt" "testing" - "k8s.io/apimachinery/pkg/util/yaml" "sigs.k8s.io/kustomize/pkg/constants" ) @@ -29,11 +27,6 @@ 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) { @@ -47,8 +40,7 @@ func TestYamlFormatError_Error(t *testing.T) { } func TestErrorHandler(t *testing.T) { - f := foo{} - err := yaml.NewYAMLToJSONDecoder(bytes.NewReader([]byte(doc))).Decode(&f) + err := fmt.Errorf("error converting YAML to JSON: yaml: line 2: found character that cannot start any token") testErr := Handler(err, filepath) expectedErr := fmt.Errorf("format error message") fmtErr := Handler(expectedErr, filepath) @@ -62,6 +54,3 @@ func TestErrorHandler(t *testing.T) { t.Errorf("Expected : %s\n, but found : %s\n", expected, testErr.Error()) } } - -//type foo struct -type foo struct{}