Skip to content

Commit 258eca3

Browse files
committed
Adding convertAzureError to handle azure errors and return os.ErrNotExist
1 parent 86c7f96 commit 258eca3

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

backends/azure/azure.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,9 @@ func (b *Backend) List(ctx context.Context, prefix string) (blobList simpleblob.
250250
// So we're using the raw marker name here.
251251
m, err := b.Load(ctx, UpdateMarkerFilename)
252252

253-
var notFound bool
253+
notFound := errors.Is(err, os.ErrNotExist)
254254

255-
if err != nil {
256-
notFound = bloberror.HasCode(err, bloberror.BlobNotFound)
257-
}
258-
259-
if err != nil && notFound {
255+
if err != nil && !notFound {
260256
return nil, err
261257
}
262258

@@ -288,6 +284,23 @@ func (b *Backend) List(ctx context.Context, prefix string) (blobList simpleblob.
288284
return blobs.WithPrefix(prefix), nil
289285
}
290286

287+
// convertAzureError takes an error from Azure SDK and converts it to
288+
// os.ErrNotExist when appropriate (BlobNotFound, ContainerNotFound, ResourceNotFound).
289+
// If the error is not a "not found" error, it is returned as is.
290+
func convertAzureError(err error) error {
291+
if err == nil {
292+
return nil
293+
}
294+
if bloberror.HasCode(err,
295+
bloberror.BlobNotFound,
296+
bloberror.ContainerNotFound,
297+
bloberror.ResourceNotFound,
298+
) {
299+
return fmt.Errorf("%w: %s", os.ErrNotExist, err.Error())
300+
}
301+
return err
302+
}
303+
291304
func (b *Backend) doList(ctx context.Context, prefix string) (simpleblob.BlobList, error) {
292305
var blobs simpleblob.BlobList
293306

@@ -364,7 +377,8 @@ func (b *Backend) doLoadReader(ctx context.Context, name string) (io.ReadCloser,
364377

365378
// Download the blob's contents and ensure that the download worked properly
366379
blobDownloadResponse, err := b.client.DownloadStream(ctx, b.opt.Container, name, nil)
367-
if err != nil {
380+
if err = convertAzureError(err); err != nil {
381+
metricCallErrors.WithLabelValues("load").Inc()
368382
return nil, err
369383
}
370384

@@ -435,12 +449,16 @@ func (b *Backend) doDelete(ctx context.Context, name string) error {
435449

436450
_, err := b.client.DeleteBlob(ctx, b.opt.Container, name, nil)
437451

438-
if err != nil {
452+
if err = convertAzureError(err); err != nil {
453+
// Delete is idempotent - if blob doesn't exist, that's fine
454+
if errors.Is(err, os.ErrNotExist) {
455+
return nil
456+
}
439457
metricCallErrors.WithLabelValues("delete").Inc()
440458
return err
441459
}
442460

443-
return err
461+
return nil
444462
}
445463

446464
// setGlobalPrefix updates the global prefix in b and the cached marker name,

backends/azure/azure_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestBackend(t *testing.T) {
112112
defer tearDown()
113113

114114
b := getBackend(ctx, t)
115-
tester.DoBackendTestsAzure(t, b)
115+
tester.DoBackendTests(t, b)
116116
assert.Len(t, b.lastMarker, 0)
117117
}
118118

@@ -124,7 +124,7 @@ func TestBackend_marker(t *testing.T) {
124124
b := getBackend(ctx, t)
125125
b.opt.UseUpdateMarker = true
126126

127-
tester.DoBackendTestsAzure(t, b)
127+
tester.DoBackendTests(t, b)
128128
assert.Regexp(t, "^foo-1:[A-Za-z0-9]*:[0-9]+:true$", b.lastMarker)
129129
// ^ reflects last write operation of tester.DoBackendTestsAzure
130130
// i.e. deleting "foo-1"
@@ -143,7 +143,7 @@ func TestBackend_globalprefix(t *testing.T) {
143143
b := getBackend(ctx, t)
144144
b.setGlobalPrefix("v5/")
145145

146-
tester.DoBackendTestsAzure(t, b)
146+
tester.DoBackendTests(t, b)
147147
assert.Empty(t, b.lastMarker)
148148
}
149149

@@ -157,6 +157,6 @@ func TestBackend_globalPrefixAndMarker(t *testing.T) {
157157
b.setGlobalPrefix("v6/")
158158
b.opt.UseUpdateMarker = true
159159

160-
tester.DoBackendTestsAzure(t, b)
160+
tester.DoBackendTests(t, b)
161161
assert.NotEmpty(t, b.lastMarker)
162162
}

backends/azure/stream_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package azure
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"io"
8+
"os"
79
"testing"
810
"time"
911

@@ -21,7 +23,7 @@ func TestStreamReader(t *testing.T) {
2123
reader, err := b.NewReader(ctx, "does-not-exist")
2224
assert.Error(t, err)
2325
assert.Nil(t, reader)
24-
assert.ErrorContains(t, err, "BlobNotFound")
26+
assert.True(t, errors.Is(err, os.ErrNotExist), "expected os.ErrNotExist")
2527

2628
// Store test data
2729
testData := []byte("test data for streaming")
@@ -73,7 +75,7 @@ func TestStreamWriter(t *testing.T) {
7375
// File should not exist before closing
7476
_, err = b.Load(ctx, "test-stream-write")
7577
assert.Error(t, err)
76-
assert.ErrorContains(t, err, "BlobNotFound")
78+
assert.True(t, errors.Is(err, os.ErrNotExist), "expected os.ErrNotExist")
7779

7880
// Close to finalize the upload
7981
err = writer.Close()

0 commit comments

Comments
 (0)