Skip to content

Commit

Permalink
remove apimachinery dependency from internal/error package
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed Oct 8, 2018
1 parent 8e0c55f commit 08da245
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
11 changes: 7 additions & 4 deletions pkg/internal/error/yamlformaterror.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
}
13 changes: 1 addition & 12 deletions pkg/internal/error/yamlformaterror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@ limitations under the License.
package error

import (
"bytes"
"fmt"
"testing"

"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/kustomize/pkg/constants"
)

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) {
Expand All @@ -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)
Expand All @@ -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{}

0 comments on commit 08da245

Please sign in to comment.