Skip to content

Commit

Permalink
fix: error handling when delete non-existent target (#627)
Browse files Browse the repository at this point in the history
resolves #626
Signed-off-by: Xiaoxuan Wang <wangxiaoxuan119@gmail.com>
  • Loading branch information
wangxiaoxuan273 authored Oct 25, 2023
1 parent 0f1dc30 commit bdea1ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion content/oci/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -113,7 +114,14 @@ func (s *Storage) Delete(ctx context.Context, target ocispec.Descriptor) error {
return fmt.Errorf("%s: %s: %w", target.Digest, target.MediaType, errdef.ErrInvalidDigest)
}
targetPath := filepath.Join(s.root, path)
return os.Remove(targetPath)
err = os.Remove(targetPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("%s: %s: %w", target.Digest, target.MediaType, errdef.ErrNotFound)
}
return err
}
return nil
}

// ingest write the content into a temporary ingest file.
Expand Down
4 changes: 4 additions & 0 deletions content/oci/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,8 @@ func TestStorage_Delete(t *testing.T) {
if exists {
t.Errorf("Storage.Exists() = %v, want %v", exists, false)
}
err = s.Delete(ctx, desc)
if !errors.Is(err, errdef.ErrNotFound) {
t.Fatalf("got error = %v, want %v", err, errdef.ErrNotFound)
}
}

0 comments on commit bdea1ff

Please sign in to comment.