dedup local bundle err handling

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2025-02-06 12:37:23 -07:00
parent 756ba75c9d
commit 5d6ffa3207
2 changed files with 22 additions and 28 deletions

View file

@ -29,34 +29,28 @@ type FetchRemoteAttestationsParams struct {
// GetLocalAttestations returns a slice of attestations read from a local bundle file.
func GetLocalAttestations(path string) ([]*api.Attestation, error) {
var attestations []*api.Attestation
var err error
fileExt := filepath.Ext(path)
switch fileExt {
case ".json":
attestations, err := loadBundleFromJSONFile(path)
if err != nil {
var pathErr *os.PathError
if errors.As(err, &pathErr) {
return nil, fmt.Errorf("bundle could not be loaded from JSON file at %s", path)
} else if errors.Is(err, bundle.ErrValidation) {
return nil, err
}
return nil, fmt.Errorf("bundle content could not be parsed")
}
return attestations, nil
case ".jsonl":
attestations, err := loadBundlesFromJSONLinesFile(path)
if err != nil {
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)
} else if errors.Is(err, bundle.ErrValidation) {
return nil, err
}
return nil, fmt.Errorf("bundle content could not be parsed")
}
return attestations, nil
if fileExt == ".json" {
attestations, err = loadBundleFromJSONFile(path)
} else if fileExt == ".jsonl" {
attestations, err = loadBundlesFromJSONLinesFile(path)
} else {
return nil, ErrUnrecognisedBundleExtension
}
return nil, ErrUnrecognisedBundleExtension
if err != nil {
var pathErr *os.PathError
if errors.As(err, &pathErr) {
return nil, fmt.Errorf("could not load content from file path %s", path)
} else if errors.Is(err, bundle.ErrValidation) {
return nil, err
}
return nil, fmt.Errorf("bundle content could not be parsed")
}
return attestations, nil
}
func loadBundleFromJSONFile(path string) ([]*api.Attestation, error) {

View file

@ -92,7 +92,7 @@ func TestGetLocalAttestations(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.ErrorContains(t, err, "could not load content from file path")
require.Nil(t, attestations)
})
@ -100,7 +100,7 @@ func TestGetLocalAttestations(t *testing.T) {
path := "../test/data/not-found-bundle.jsonl"
attestations, err := GetLocalAttestations(path)
require.ErrorContains(t, err, "bundles could not be loaded from JSON lines file")
require.ErrorContains(t, err, "could not load content from file path")
require.Nil(t, attestations)
})