Skip to content

Commit

Permalink
Migrate error wrapping to use go native logic
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Franssen <marco.franssen@philips.com>
  • Loading branch information
marcofranssen committed Nov 25, 2021
1 parent a93f24e commit 0fc6b93
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
16 changes: 8 additions & 8 deletions cmd/slsa-provenance/cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
Expand All @@ -11,7 +12,6 @@ import (
"strings"

"github.com/peterbourgon/ff/v3/ffcli"
"github.com/pkg/errors"

"github.com/philips-labs/slsa-provenance-action/lib/github"
"github.com/philips-labs/slsa-provenance-action/lib/intoto"
Expand Down Expand Up @@ -66,12 +66,12 @@ func Generate(w io.Writer) *ffcli.Command {

var gh github.Context
if err := json.Unmarshal([]byte(*githubContext), &gh); err != nil {
return errors.Wrap(err, "failed to unmarshal github context json")
return fmt.Errorf("failed to unmarshal github context json: %w", err)
}

var runner github.RunnerContext
if err := json.Unmarshal([]byte(*runnerContext), &runner); err != nil {
return errors.Wrap(err, "failed to unmarshal runner context json")
return fmt.Errorf("failed to unmarshal runner context json: %w", err)
}

var env intoto.Provenancer
Expand All @@ -92,24 +92,24 @@ func Generate(w io.Writer) *ffcli.Command {

stmt, err := env.GenerateProvenanceStatement(ctx, *artifactPath)
if err != nil {
return errors.Wrap(err, "failed to generate provenance")
return fmt.Errorf("failed to generate provenance: %w", err)
}

for _, extra := range extraMaterials {
content, err := ioutil.ReadFile(extra)
if err != nil {
return errors.Wrapf(err, "Could not load extra materials from %s", extra)
return fmt.Errorf("could not load extra materials from %s: %w", extra, err)
}
var materials []intoto.Item
if err = json.Unmarshal(content, &materials); err != nil {
return errors.Wrapf(err, "Invalid JSON in extra materials file %s", extra)
return fmt.Errorf("invalid JSON in extra materials file %s: %w", extra, err)
}
for _, material := range materials {
if material.URI == "" {
return errors.Errorf("Empty or missing \"uri\" field in %s", extra)
return fmt.Errorf("empty or missing \"uri\" field in %s", extra)
}
if len(material.Digest) == 0 {
return errors.Errorf("Empty or missing \"digest\" in %s", extra)
return fmt.Errorf("empty or missing \"digest\" in %s", extra)
}
}
stmt.Predicate.Materials = append(stmt.Predicate.Materials, materials...)
Expand Down
8 changes: 4 additions & 4 deletions cmd/slsa-provenance/cli/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func TestGenerateCliOptions(t *testing.T) {
},
{
name: "With broken extra materials",
err: fmt.Errorf("Invalid JSON in extra materials file %s: unexpected end of JSON input", path.Join(rootDir, "test-data/materials-broken.not-json")),
err: fmt.Errorf("invalid JSON in extra materials file %s: unexpected end of JSON input", path.Join(rootDir, "test-data/materials-broken.not-json")),
arguments: []string{
"-artifact_path",
path.Join(rootDir, "bin/slsa-provenance"),
Expand All @@ -360,7 +360,7 @@ func TestGenerateCliOptions(t *testing.T) {
},
{
name: "With non-existent extra materials",
err: fmt.Errorf("Could not load extra materials from non-existing-folder/unknown-file: open non-existing-folder/unknown-file: no such file or directory"),
err: fmt.Errorf("could not load extra materials from non-existing-folder/unknown-file: open non-existing-folder/unknown-file: no such file or directory"),
arguments: []string{
"-artifact_path",
path.Join(rootDir, "bin/slsa-provenance"),
Expand All @@ -376,7 +376,7 @@ func TestGenerateCliOptions(t *testing.T) {
},
{
name: "With broken extra materials (no uri)",
err: fmt.Errorf("Empty or missing \"uri\" field in %s", path.Join(rootDir, "test-data/materials-no-uri.json")),
err: fmt.Errorf("empty or missing \"uri\" field in %s", path.Join(rootDir, "test-data/materials-no-uri.json")),
arguments: []string{
"-artifact_path",
path.Join(rootDir, "bin/slsa-provenance"),
Expand All @@ -392,7 +392,7 @@ func TestGenerateCliOptions(t *testing.T) {
},
{
name: "With broken extra materials (no digest)",
err: fmt.Errorf("Empty or missing \"digest\" in %s", path.Join(rootDir, "test-data/materials-no-digest.json")),
err: fmt.Errorf("empty or missing \"digest\" in %s", path.Join(rootDir, "test-data/materials-no-digest.json")),
arguments: []string{
"-artifact_path",
path.Join(rootDir, "bin/slsa-provenance"),
Expand Down
3 changes: 1 addition & 2 deletions cmd/slsa-provenance/cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"text/tabwriter"

"github.com/peterbourgon/ff/v3/ffcli"
"github.com/pkg/errors"
)

// Base version information.
Expand Down Expand Up @@ -50,7 +49,7 @@ func Version(w io.Writer) *ffcli.Command {
if *outJSON {
j, err := v.JSONString()
if err != nil {
return errors.Wrap(err, "unable to generate JSON from version info")
return fmt.Errorf("unable to generate JSON from version info: %w", err)
}
res = j
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.17
require (
github.com/google/go-github/v39 v39.2.0
github.com/peterbourgon/ff/v3 v3.1.2
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/peterbourgon/ff/v3 v3.1.2 h1:0GNhbRhO9yHA4CC27ymskOsuRpmX0YQxwxM9UPiP6JM=
github.com/peterbourgon/ff/v3 v3.1.2/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
5 changes: 2 additions & 3 deletions lib/github/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package github
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/pkg/errors"

"github.com/philips-labs/slsa-provenance-action/lib/intoto"
)

Expand All @@ -28,7 +27,7 @@ func (e *Environment) GenerateProvenanceStatement(ctx context.Context, artifactP

event := AnyEvent{}
if err := json.Unmarshal(e.Context.Event, &event); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal github context event json")
return nil, fmt.Errorf("failed to unmarshal github context event json: %w", err)
}

stmt := intoto.SLSAProvenanceStatement(
Expand Down

0 comments on commit 0fc6b93

Please sign in to comment.