gcsstore: Do not ignore errors when writing an object

Previously, any error returned from w.Close() would be ignored. This is
a problem since this function is responsible for returning any error
that occurred during the actual GCS transaction.

This fix also uncovered an issue in the corresponding test routine.

Furthermore, a "404 Not Found" error when writing an object is now
interpreted as if the bucket does not exist to ease debugging
(see https://github.com/tus/tusd/issues/241).

/cc @tab1293
This commit is contained in:
Acconut 2019-03-10 17:01:29 +01:00
parent 5e06fc54b0
commit c0651e77fc
2 changed files with 17 additions and 3 deletions

View File

@ -10,6 +10,7 @@ import (
"cloud.google.com/go/storage" "cloud.google.com/go/storage"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/api/googleapi"
"google.golang.org/api/iterator" "google.golang.org/api/iterator"
"google.golang.org/api/option" "google.golang.org/api/option"
@ -290,13 +291,19 @@ func (service *GCSService) WriteObject(ctx context.Context, params GCSObjectPara
w := obj.NewWriter(ctx) w := obj.NewWriter(ctx)
defer w.Close()
n, err := io.Copy(w, r) n, err := io.Copy(w, r)
if err != nil { if err != nil {
return 0, err return 0, err
} }
err = w.Close()
if err != nil {
if gErr, ok := err.(*googleapi.Error); ok && gErr.Code == 404 {
return 0, fmt.Errorf("gcsstore: the bucket %s could not be found while trying to write an object", params.Bucket)
}
return 0, err
}
return n, err return n, err
} }

View File

@ -360,6 +360,13 @@ func TestWriteObject(t *testing.T) {
"expiry_date": "1425333671141", "expiry_date": "1425333671141",
}) })
gock.New("https://googleapis.com").
Post("/upload/storage/v1/b/test-bucket/o").
MatchParam("alt", "json").
MatchParam("key", "foo").
Reply(200).
JSON(map[string]string{})
ctx := context.Background() ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithAPIKey("foo")) client, err := storage.NewClient(ctx, option.WithAPIKey("foo"))
if err != nil { if err != nil {
@ -379,7 +386,7 @@ func TestWriteObject(t *testing.T) {
}, reader) }, reader)
if err != nil { if err != nil {
t.Errorf("Error deleting object: %+v", err) t.Errorf("Error writing object: %+v", err)
} }
if size != 1 { if size != 1 {