mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 18:10:59 +00:00
remove apimachinery dependency from internal/error package
This commit is contained in:
@@ -19,8 +19,7 @@ package error
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"k8s.io/apimachinery/pkg/util/yaml"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// YamlFormatError represents error with yaml file name where json/yaml format error happens.
|
// 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
|
// Handler handles YamlFormatError
|
||||||
func Handler(e error, path string) error {
|
func Handler(e error, path string) error {
|
||||||
if err, ok := e.(yaml.YAMLSyntaxError); ok {
|
if isYAMLSyntaxError(e) {
|
||||||
return YamlFormatError{
|
return YamlFormatError{
|
||||||
Path: path,
|
Path: path,
|
||||||
ErrorMsg: err.Error(),
|
ErrorMsg: e.Error(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isYAMLSyntaxError(e error) bool {
|
||||||
|
return strings.Contains(e.Error(), "error converting YAML to JSON") || strings.Contains(e.Error(), "error unmarshaling JSON")
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,11 +17,9 @@ limitations under the License.
|
|||||||
package error
|
package error
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/yaml"
|
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,11 +27,6 @@ var (
|
|||||||
filepath = "/path/to/" + constants.KustomizationFileName
|
filepath = "/path/to/" + constants.KustomizationFileName
|
||||||
expected = "YAML file [/path/to/kustomization.yaml] encounters a format error.\n" +
|
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"
|
"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) {
|
func TestYamlFormatError_Error(t *testing.T) {
|
||||||
@@ -47,8 +40,7 @@ func TestYamlFormatError_Error(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorHandler(t *testing.T) {
|
func TestErrorHandler(t *testing.T) {
|
||||||
f := foo{}
|
err := fmt.Errorf("error converting YAML to JSON: yaml: line 2: found character that cannot start any token")
|
||||||
err := yaml.NewYAMLToJSONDecoder(bytes.NewReader([]byte(doc))).Decode(&f)
|
|
||||||
testErr := Handler(err, filepath)
|
testErr := Handler(err, filepath)
|
||||||
expectedErr := fmt.Errorf("format error message")
|
expectedErr := fmt.Errorf("format error message")
|
||||||
fmtErr := Handler(expectedErr, filepath)
|
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())
|
t.Errorf("Expected : %s\n, but found : %s\n", expected, testErr.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//type foo struct
|
|
||||||
type foo struct{}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user