From 8e3c19775522378864416f4e93d4d05dd4651ae7 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Thu, 14 Mar 2024 12:23:34 -0600 Subject: [PATCH] look for err specific to file write Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/download/download_test.go | 1 + pkg/cmd/attestation/download/metadata.go | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/attestation/download/download_test.go b/pkg/cmd/attestation/download/download_test.go index a1d621c45..2b152b937 100644 --- a/pkg/cmd/attestation/download/download_test.go +++ b/pkg/cmd/attestation/download/download_test.go @@ -310,5 +310,6 @@ func TestRunDownload(t *testing.T) { err := runDownload(&opts) require.Error(t, err) + require.ErrorAs(t, err, &ErrAttestationFileCreation) }) } diff --git a/pkg/cmd/attestation/download/metadata.go b/pkg/cmd/attestation/download/metadata.go index 6635aa943..668d2fd27 100644 --- a/pkg/cmd/attestation/download/metadata.go +++ b/pkg/cmd/attestation/download/metadata.go @@ -2,12 +2,15 @@ package download import ( "encoding/json" + "errors" "fmt" "os" "github.com/cli/cli/v2/pkg/cmd/attestation/api" ) +var ErrAttestationFileCreation = fmt.Errorf("failed to write attestations to file") + type MetadataStore interface { createMetadataFile(artifactDigest string, attestationsResp []*api.Attestation) (string, error) } @@ -29,29 +32,29 @@ func (s *LiveStore) createMetadataFile(artifactDigest string, attestationsResp [ f, err := os.Create(metadataFilePath) if err != nil { - return "", fmt.Errorf("failed to create trusted metadata file: %w", err) + return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to create file: %w", err)) } for _, resp := range attestationsResp { bundle := resp.Bundle attBytes, err := json.Marshal(bundle) if err != nil { - return "", fmt.Errorf("failed to marshall attestation to JSON: %w", err) + return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to marshall attestation to JSON while writing to file: %w", err)) } withNewline := fmt.Sprintf("%s\n", attBytes) _, err = f.Write([]byte(withNewline)) if err != nil { if err = f.Close(); err != nil { - return "", fmt.Errorf("failed to close file while handling write error: %w", err) + return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file while handling write error: %w", err)) } - return "", fmt.Errorf("failed to write trusted metadata: %w", err) + return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to write attestations: %w", err)) } } if err = f.Close(); err != nil { - return "", fmt.Errorf("failed to close file after writing metadata: %w", err) + return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file after writing attestations: %w", err)) } return metadataFilePath, nil