check for os.PathError

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-09-05 13:20:13 -06:00
parent 7c405e8b6e
commit 57b20291bd
3 changed files with 19 additions and 3 deletions

View file

@ -53,13 +53,21 @@ func GetLocalAttestations(path string) ([]*api.Attestation, error) {
case ".json":
attestations, err := loadBundleFromJSONFile(path)
if err != nil {
return nil, fmt.Errorf("bundle could not be loaded from JSON file at %s", path)
var pathErr *os.PathError
if errors.As(err, &pathErr) {
return nil, fmt.Errorf("bundle could not be loaded from JSON file at %s", path)
}
return nil, fmt.Errorf("bundle content could not be parsed")
}
return attestations, nil
case ".jsonl":
attestations, err := loadBundlesFromJSONLinesFile(path)
if err != nil {
return nil, fmt.Errorf("bundles could not be loaded from JSON lines file at %s", path)
var pathErr *os.PathError
if errors.As(err, &pathErr) {
return nil, fmt.Errorf("bundles could not be loaded from JSON lines file at %s", path)
}
return nil, fmt.Errorf("bundle content could not be parsed")
}
return attestations, nil
}

View file

@ -87,6 +87,14 @@ func TestGetLocalAttestations(t *testing.T) {
require.ErrorIs(t, err, ErrUnrecognisedBundleExtension)
require.Nil(t, attestations)
})
t.Run("with non-existent bundle file", func(t *testing.T) {
path := "../test/data/not-found-bundle.json"
attestations, err := GetLocalAttestations(path)
require.ErrorContains(t, err, "bundle could not be loaded from JSON file")
require.Nil(t, attestations)
})
}
func TestFilterAttestations(t *testing.T) {

View file

@ -186,7 +186,7 @@ func (v *LiveSigstoreVerifier) Verify(attestations []*api.Attestation, policy ve
verifier, issuer, err := v.chooseVerifier(apr.Attestation.Bundle)
if err != nil {
return &SigstoreResults{
Error: fmt.Errorf("failed to find recognized issuer from bundle content: %v", err),
Error: fmt.Errorf("failed to find recognized issuer from bundle content"),
}
}