From bdea1ff49eee124365191897b95bc763a257c254 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang <103478229+wangxiaoxuan273@users.noreply.github.com> Date: Wed, 25 Oct 2023 14:09:28 +0800 Subject: [PATCH] fix: error handling when delete non-existent target (#627) resolves #626 Signed-off-by: Xiaoxuan Wang --- content/oci/storage.go | 10 +++++++++- content/oci/storage_test.go | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/content/oci/storage.go b/content/oci/storage.go index 6acf5b34..efb9f3d8 100644 --- a/content/oci/storage.go +++ b/content/oci/storage.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "io" + "io/fs" "os" "path/filepath" "sync" @@ -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. diff --git a/content/oci/storage_test.go b/content/oci/storage_test.go index 514f354c..244acb21 100644 --- a/content/oci/storage_test.go +++ b/content/oci/storage_test.go @@ -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) + } }