replace panics with logging

This commit is contained in:
Alex Goodman
2020-05-21 16:53:17 -04:00
parent b7c7c5556d
commit 09c7ca8f8f
9 changed files with 73 additions and 26 deletions
+1
View File
@@ -7,6 +7,7 @@ import (
)
type Analyzer interface {
Name() string
// TODO: add ID / Name for analyze for uniquely identifying this analyzer type
SelectFiles([]*tree.FileTree) []file.Reference
// NOTE: one of the errors which is returned is "IterationNeeded", which indicates to the driver to
+8 -1
View File
@@ -4,6 +4,7 @@ import (
"github.com/anchore/imgbom/imgbom/analyzer/dpkg"
"github.com/anchore/imgbom/imgbom/pkg"
"github.com/anchore/imgbom/imgbom/scope"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope/pkg/file"
"github.com/hashicorp/go-multierror"
)
@@ -26,6 +27,7 @@ type controller struct {
}
func (c *controller) add(a Analyzer) {
log.Debugf("adding analyzer: %s", a.Name())
c.analyzers = append(c.analyzers, a)
}
@@ -36,6 +38,7 @@ func (c *controller) analyze(s scope.Scope) (pkg.Catalog, error) {
// ask analyzers for files to extract from the image tar
for _, a := range c.analyzers {
fileSelection = append(fileSelection, a.SelectFiles(s.Trees)...)
log.Debugf("analyzer '%s' selected '%d' files", a.Name(), len(fileSelection))
}
// fetch contents for requested selection by analyzers
@@ -47,11 +50,15 @@ func (c *controller) analyze(s scope.Scope) (pkg.Catalog, error) {
// perform analysis, accumulating errors for each failed analysis
var errs error
for _, a := range c.analyzers {
packages, err := a.Analyze(contents)
// TODO: check for multiple rounds of analyses by Iterate error
packages, err := a.Analyze(contents)
if err != nil {
errs = multierror.Append(errs, err)
continue
}
log.Debugf("analyzer '%s' discovered '%d' packages", a.Name(), len(packages))
for _, p := range packages {
catalog.Add(p)
}
+22 -4
View File
@@ -4,6 +4,7 @@ import (
"strings"
"github.com/anchore/imgbom/imgbom/pkg"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/tree"
)
@@ -16,6 +17,10 @@ func NewAnalyzer() *Analyzer {
return &Analyzer{}
}
func (a *Analyzer) Name() string {
return "dpkg-analyzer"
}
func (a *Analyzer) SelectFiles(trees []*tree.FileTree) []file.Reference {
files := make([]file.Reference, 0)
for _, tree := range trees {
@@ -36,14 +41,26 @@ func (a *Analyzer) Analyze(contents map[file.Reference]string) ([]pkg.Package, e
for _, reference := range a.selectedFiles {
content, ok := contents[reference]
if !ok {
// TODO: this needs handling
panic(reference)
// TODO: test case
log.WithFields(map[string]interface{}{
"path": reference.Path,
"id": reference.ID(),
"analyzer": a.Name(),
}).Errorf("analyzer file content missing")
continue
}
entries, err := ParseEntries(strings.NewReader(content))
if err != nil {
// TODO: punt for now, we need to handle this
panic(err)
// TODO: test case
log.WithFields(map[string]interface{}{
"path": reference.Path,
"id": reference.ID(),
"analyzer": a.Name(),
}).Errorf("analyzer failed to parse entries: %w", err)
continue
}
for _, entry := range entries {
packages = append(packages, pkg.Package{
@@ -55,5 +72,6 @@ func (a *Analyzer) Analyze(contents map[file.Reference]string) ([]pkg.Package, e
})
}
}
return packages, nil
}
+2 -2
View File
@@ -54,7 +54,7 @@ func TestSinglePackage(t *testing.T) {
defer func() {
err := file.Close()
if err != nil {
panic(err)
t.Fatal("closing file failed:", err)
}
}()
@@ -123,7 +123,7 @@ func TestMultiplePackages(t *testing.T) {
defer func() {
err := file.Close()
if err != nil {
panic(err)
t.Fatal("closing file failed:", err)
}
}()