Skip to content

Commit

Permalink
refactor: Refactor document decoding to fail fast and return errors i…
Browse files Browse the repository at this point in the history
…mmediately (#167)

With this change, the function terminates execution upon encountering a decoding error, providing more accurate error handling. This ensures that errors are not accumulated and allows the caller to handle the error appropriately.

See: #195 (comment)
  • Loading branch information
art-tapin committed Jun 12, 2023
1 parent 96aca5d commit e23868e
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions pkg/common/yaml_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func DecodeFile(ctx context.Context, fileData []byte, scheme *runtime.Scheme, cb
buf := make([]byte, 5*1024*1024)
docDecoder := yaml.NewDocumentDecoder(io.NopCloser(bytes.NewReader(fileData)))

hasError := false
for {
n, err := docDecoder.Read(buf)
if err != nil {
Expand All @@ -49,17 +48,13 @@ func DecodeFile(ctx context.Context, fileData []byte, scheme *runtime.Scheme, cb
obj, _, err := decoder.Decode(docData, nil, nil)
if err != nil {
logger.Info("Document decode error", "error", err)
hasError = true
continue
return fmt.Errorf("failed to decode document: %w", err)
}

err = cb(obj)
if err != nil {
return err
}
}
if hasError {
return fmt.Errorf("one or more document decoding errors occurred. See the logs for more details")
}
return nil
}

0 comments on commit e23868e

Please sign in to comment.