Skip to content

Commit

Permalink
Merge pull request #174 from instrumenta/improve-error-output
Browse files Browse the repository at this point in the history
Improve error output
  • Loading branch information
garethr authored Aug 31, 2019
2 parents 120888f + 2175d57 commit ff3f46a
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions kubeval/kubeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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")
}

0 comments on commit ff3f46a

Please sign in to comment.