Skip to content

Commit

Permalink
Don't fatal error when filter cannot iterate
Browse files Browse the repository at this point in the history
A jq filter may expect to iterate over a list of results, but it can
happen that no result is returned.
  • Loading branch information
yuumasato committed Apr 26, 2024
1 parent 8a8d4b7 commit 9a73e58
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/manager/scap.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (

var (
MoreThanOneObjErr = errors.New("more than one object returned from the filter")
CannotIterateOverNull = errors.New("cannot iterate over: null")
)

// resourceFetcherClients just gathers several needed structs together so we can
Expand Down Expand Up @@ -520,6 +521,8 @@ func fetch(ctx context.Context, streamDispatcher streamerDispatcherFn, rfClients
filteredBody, filterErr := filter(ctx, body, rpath.Filter)
if errors.Is(filterErr, MoreThanOneObjErr) {
warnings = append(warnings, filterErr.Error())
} else if errors.Is(filterErr, CannotIterateOverNull) {
warnings = append(warnings, filterErr.Error())
} else if filterErr != nil {
return fmt.Errorf("couldn't filter '%s': %w", body, filterErr)
}
Expand Down Expand Up @@ -554,6 +557,9 @@ func filter(ctx context.Context, rawobj []byte, filter string) ([]byte, error) {
}
if err, ok := v.(error); ok {
DBG("Error while filtering: %s", err)
if errors.Is(err, CannotIterateOverNull) {
return nil, fmt.Errorf("Failed to iterate over results from filter '%s': %w", filter, CannotIterateOverNull)
}
return nil, err
}

Expand Down

0 comments on commit 9a73e58

Please sign in to comment.