Skip to content

Commit

Permalink
Add "ErrInvalid" to indicate validation errors
Browse files Browse the repository at this point in the history
Fixes #7.
  • Loading branch information
djjuhasz committed Jul 11, 2024
1 parent 5df4225 commit 04cda10
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions bagit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"github.com/kluctl/go-embed-python/python"
)

// ErrInvalid indicates that bag validation failed. If there is a validation
// error message, ErrInvalid will be wrapped so make sure to use
// `errors.Is(err, ErrInvalid)` to test equivalency.
var ErrInvalid = errors.New("invalid")

// BagIt is an abstraction to work with BagIt packages that embeds Python and
// the bagit-python.
type BagIt struct {
Expand Down Expand Up @@ -138,10 +143,10 @@ func (b *BagIt) Validate(path string) error {
return fmt.Errorf("decode response: %v", err)
}
if r.Err != "" {
return fmt.Errorf("invalid: %s", r.Err)
return fmt.Errorf("%w: %s", ErrInvalid, r.Err)
}
if !r.Valid {
return fmt.Errorf("invalid: %s", r.Err)
return ErrInvalid
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions bagit_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bagit_test

import (
"errors"
"testing"

"github.com/artefactual-labs/bagit-gython"
Expand Down Expand Up @@ -33,6 +34,7 @@ func TestValidateBag(t *testing.T) {

err := b.Validate("/tmp/691b8e7f-e6b7-41dd-bc47-868e2ff69333")
assert.Error(t, err, "invalid: Expected bagit.txt does not exist: /tmp/691b8e7f-e6b7-41dd-bc47-868e2ff69333/bagit.txt")
assert.Assert(t, errors.Is(err, bagit.ErrInvalid))
})

t.Run("Validates bag", func(t *testing.T) {
Expand Down

0 comments on commit 04cda10

Please sign in to comment.