Skip to content

Commit 2175d57

Browse files
committed
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.
1 parent ecd0802 commit 2175d57

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

kubeval/kubeval.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ func validateResource(data []byte, schemaCache map[string]*gojsonschema.Schema,
110110

111111
kind, err := getString(body, "kind")
112112
if err != nil {
113-
return result, err
113+
return result, fmt.Errorf("Error with %s: %s", result.FileName, err.Error())
114114
}
115115
result.Kind = kind
116116

117117
apiVersion, err := getString(body, "apiVersion")
118118
if err != nil {
119-
return result, err
119+
return result, fmt.Errorf("Error with %s: %s", result.FileName, err.Error())
120120
}
121121
result.APIVersion = apiVersion
122122

@@ -126,7 +126,7 @@ func validateResource(data []byte, schemaCache map[string]*gojsonschema.Schema,
126126

127127
schemaErrors, err := validateAgainstSchema(body, &result, schemaCache, config)
128128
if err != nil {
129-
return result, err
129+
return result, fmt.Errorf("Error with %s: %s", result.FileName, err.Error())
130130
}
131131
result.Errors = schemaErrors
132132
return result, nil
@@ -178,6 +178,7 @@ func downloadSchema(resource *ValidationResult, schemaCache map[string]*gojsonsc
178178
}
179179

180180
var errors *multierror.Error
181+
181182
for _, schemaRef := range schemaRefs {
182183
schemaLoader := gojsonschema.NewReferenceLoader(schemaRef)
183184
schema, err := gojsonschema.NewSchema(schemaLoader)
@@ -191,6 +192,10 @@ func downloadSchema(resource *ValidationResult, schemaCache map[string]*gojsonsc
191192
errors = multierror.Append(errors, wrappedErr)
192193
}
193194

195+
if errors != nil {
196+
errors.ErrorFormat = singleLineErrorFormat
197+
}
198+
194199
// We couldn't find a schema for this resource. Cache it's lack of existence, then stop
195200
schemaCache[resource.VersionKind()] = nil
196201
return nil, errors.ErrorOrNil()
@@ -270,5 +275,17 @@ func ValidateWithCache(input []byte, schemaCache map[string]*gojsonschema.Schema
270275
results = append(results, result)
271276
}
272277
}
278+
279+
if errors != nil {
280+
errors.ErrorFormat = singleLineErrorFormat
281+
}
273282
return results, errors.ErrorOrNil()
274283
}
284+
285+
func singleLineErrorFormat(es []error) string {
286+
messages := make([]string, len(es))
287+
for i, e := range es {
288+
messages[i] = e.Error()
289+
}
290+
return strings.Join(messages, "\n")
291+
}

0 commit comments

Comments
 (0)