From 2175d578cabba796d7afe962653860c87acef116 Mon Sep 17 00:00:00 2001 From: Gareth Rushgrove Date: Mon, 19 Aug 2019 10:38:54 +0100 Subject: [PATCH] Place error messages on a single line and include filename Error messages at the moment don't carry context, which makes finding errors when testing multiple files hard. Error messages also take up lots of space currently. This commit resolves those issues. --- kubeval/kubeval.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/kubeval/kubeval.go b/kubeval/kubeval.go index 6c29c06..8990290 100644 --- a/kubeval/kubeval.go +++ b/kubeval/kubeval.go @@ -110,13 +110,13 @@ func validateResource(data []byte, schemaCache map[string]*gojsonschema.Schema, kind, err := getString(body, "kind") if err != nil { - return result, err + return result, fmt.Errorf("Error with %s: %s", result.FileName, err.Error()) } result.Kind = kind apiVersion, err := getString(body, "apiVersion") if err != nil { - return result, err + return result, fmt.Errorf("Error with %s: %s", result.FileName, err.Error()) } result.APIVersion = apiVersion @@ -126,7 +126,7 @@ func validateResource(data []byte, schemaCache map[string]*gojsonschema.Schema, schemaErrors, err := validateAgainstSchema(body, &result, schemaCache, config) if err != nil { - return result, err + return result, fmt.Errorf("Error with %s: %s", result.FileName, err.Error()) } result.Errors = schemaErrors return result, nil @@ -178,6 +178,7 @@ func downloadSchema(resource *ValidationResult, schemaCache map[string]*gojsonsc } var errors *multierror.Error + for _, schemaRef := range schemaRefs { schemaLoader := gojsonschema.NewReferenceLoader(schemaRef) schema, err := gojsonschema.NewSchema(schemaLoader) @@ -191,6 +192,10 @@ func downloadSchema(resource *ValidationResult, schemaCache map[string]*gojsonsc errors = multierror.Append(errors, wrappedErr) } + if errors != nil { + errors.ErrorFormat = singleLineErrorFormat + } + // We couldn't find a schema for this resource. Cache it's lack of existence, then stop schemaCache[resource.VersionKind()] = nil return nil, errors.ErrorOrNil() @@ -270,5 +275,17 @@ func ValidateWithCache(input []byte, schemaCache map[string]*gojsonschema.Schema results = append(results, result) } } + + if errors != nil { + errors.ErrorFormat = singleLineErrorFormat + } return results, errors.ErrorOrNil() } + +func singleLineErrorFormat(es []error) string { + messages := make([]string, len(es)) + for i, e := range es { + messages[i] = e.Error() + } + return strings.Join(messages, "\n") +}