From 368a3672f9177db60495ecfb4447122708f43ad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20H=C4=83loiu?= Date: Fri, 25 May 2018 12:14:16 +0200 Subject: [PATCH] Handle GCS object-not-found error --- gcsstore/gcsstore.go | 4 ++++ gcsstore/gcsstore_test.go | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/gcsstore/gcsstore.go b/gcsstore/gcsstore.go index b60a73d..a52bba6 100644 --- a/gcsstore/gcsstore.go +++ b/gcsstore/gcsstore.go @@ -18,6 +18,7 @@ import ( "strconv" "strings" + "cloud.google.com/go/storage" "github.com/tus/tusd" "github.com/tus/tusd/uid" ) @@ -113,6 +114,9 @@ func (store GCSStore) GetInfo(id string) (tusd.FileInfo, error) { r, err := store.Service.ReadObject(params) if err != nil { + if err == storage.ErrObjectNotExist { + return info, tusd.ErrNotFound + } return info, err } diff --git a/gcsstore/gcsstore_test.go b/gcsstore/gcsstore_test.go index aed4ff8..45c0545 100644 --- a/gcsstore/gcsstore_test.go +++ b/gcsstore/gcsstore_test.go @@ -6,6 +6,7 @@ import ( "fmt" "testing" + "cloud.google.com/go/storage" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" @@ -143,6 +144,27 @@ func TestGetInfo(t *testing.T) { assert.Equal(mockTusdInfo, info) } +func TestGetInfoNotFound(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + assert := assert.New(t) + + service := NewMockGCSAPI(mockCtrl) + store := gcsstore.New(mockBucket, service) + + params := gcsstore.GCSObjectParams{ + Bucket: store.Bucket, + ID: fmt.Sprintf("%s.info", mockID), + } + + gomock.InOrder( + service.EXPECT().ReadObject(params).Return(nil, storage.ErrObjectNotExist), + ) + + _, err := store.GetInfo(mockID) + assert.Equal(tusd.ErrNotFound, err) +} + type MockGetReader struct{} func (r MockGetReader) Close() error {